Updating data and modifying it before it is saved

Starting with cms_ApplicationList, which is directly inherited from cms_ApplicationBasic, you can override methods for saving objects:

  • array modifyDataValuesBeforeSaveItem(array $datavalues)
  • array getValidationErrorListBeforeSaveItem(array $datavalues)
  • void onAfterSaveItem(array $datavalues, array $original_datavalues, array $previous_item, array $retval)

The purpose is if you need to modify certain data before it is sent or to perform an action (e.g. moving file to your data directories), then you can override these methods. Be sure to call the parent::the_method_Name() first so it will execute parent class' methods. For example:

    /**
     * Override default modify item data before save
     * @param array $datavalues
     */

    protected function modifyDataValuesBeforeSaveItem($datavalues) {
        $datavalues = parent::modifyDataValuesBeforeSaveItem($datavalues);
        $short_name =  convert_into_sef_friendly_title(strtolower($datavalues['title']));
        $datavalues['virtual_filename'] = $this->preventDuplicateValueInItemTable('virtual_filename', $short_name, $datavalues[$this->field_id]) ;

        $int_thumb_width = $this->getConfig('int_thumb_width',128);
        $int_thumb_height = $this->getConfig('int_thumb_height',128);
        $int_xl_width = $this->getConfig('int_xl_width',1024);
        $int_xl_height = $this->getConfig('int_xl_height',1024);

        $new_image_filename = $datavalues['virtual_filename'];
        $file_icon = $_FILES['image'];

        if (is_uploaded_file($_FILES['image']['tmp_name'])) {
            if ($file_icon['error'] == UPLOAD_ERR_OK) {
                $icon_file_name = $_FILES['image']['name'];
                $new_image_filename =  pathinfo($new_image_filename, PATHINFO_FILENAME) . '.' . pathinfo($icon_file_name, PATHINFO_EXTENSION);

                // name the image the same as the vfname
                $datavalues['image'] = $new_image_filename;
                $img_original_filename = $this->getDataFileFullPath('image_original', $new_image_filename);
                move_uploaded_file($file_icon['tmp_name'], $img_original_filename);
                
                create_image_thumbnail($img_original_filename, $this->getDataFileFullPath('image_thumbs', $new_image_filename), $int_thumb_width, $int_thumb_height);
                create_image_thumbnail($img_original_filename, $this->getDataFileFullPath('image_large', $new_image_filename), $int_xl_width, $int_xl_height, false, 90);
                
                //create_image_thumbnail($this->getDataFileFullPath('icons_original', $new_icon_filename), $this->getDataFileFullPath('icons', $new_icon_filename), 128, 128);
            }
        }
        
        return $datavalues;
    }