3

I am working on a custom component, where at the backend interface - we want the admin user to be able to upload image and let that image directly save to a specified folder - /images/uploads/variants/ and also to save file name say - 1327171407s_Rubytoys.jpg in the database in column field - v_small_img

What we have done as of now is On path directory /administrator/component/models/form/variant.xml file the field is set along with other fields in variant.xml file

 <field name="v_name" type="text" label="Variant" description="COM_NEWTOY_FORM_DESC_VARIANT_V_NAME" hint="COM_NEWTOY_FORM_LBL_VARIANT_V_NAME"/>
 <field name="v_descr" type="editor" filter="safehtml" label="COM_NEWTOY_FORM_LBL_VARIANT_V_DESCR" description="COM_NEWTOY_FORM_DESC_VARIANT_V_DESCR" hint="COM_NEWTOY_FORM_LBL_VARIANT_V_DESCR"/>
 <field name="v_price" type="text" label="Price" description="COM_NEWTOY_FORM_DESC_VARIANT_V_PRICE" hint="COM_NEWTOY_FORM_LBL_VARIANT_V_PRICE"/>    
 <field name="v_small_img" type="file" label="COM_NEWTOY_FORM_LBL_VARIANT_V_SMALL_IMG" description="COM_NEWTOY_FORM_DESC_VARIANT_V_SMALL_IMG" hint="COM_NEWCAR_FORM_LBL_VARIANT_V_SMALL_IMG"/>

The field v_small_img is showing an upload button. But I don't know:

  1. How to set path of upload file to save in - public_html/images/uploads/variants/

  2. How to save file name only in db - v_small_img (only file name to be saved, not the file as it has to be uploaded to separate directory - /images/uploads/variants/)

I have tried modifying save function in controller/variants.php but it's not helping either.

    public function saveOrderAjax()
{
    // Get the input
    $input = JFactory::getApplication()->input;
    $pks   = $input->post->get('cid', array(), 'array');
    $order = $input->post->get('order', array(), 'array');

    $files = $input->files->get('jform');
    $file = $files['image'];
    $filename = JFile::makeSafe($file['name']);
    $src = $file['tmp_name'];
    $dest = JPATH_SITE."images/uploads/variants/".$filename;
    if (JFile::upload($src, $dest)) {
        $thisID = JRequest::getVar('id');
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $v_small_img = array($db->quoteName('image') . " = " . $db->quote($filename));
        $conditions = array($db->quoteName('id') . " = " . $thisID);
        $query->update($db->quoteName('#__NEWTOY_variants'))->set($v_small_img)->where($conditions);
        $db->setQuery($query);
        $db->execute($query);
    } else {
          //Redirect and throw an error message
    }


    // Sanitize the input
    ArrayHelper::toInteger($pks);
    ArrayHelper::toInteger($order);

    // Get the model
    $model = $this->getModel();

    // Save the ordering
    $return = $model->saveorder($pks, $order);



    if ($return)
    {
        echo "1";
    }

    // Close the application
    JFactory::getApplication()->close();
}
1

To me it seems to be almost a one to one copy of the backend "list items / edit item" views of com_banners. Except you want to upload an image instead of using the internal media manager.

  1. Create those 2 views and the needed tasks and methods with the image field as a normal text field.
  2. When this works as you want it to, you add the image upload.
  3. The harder part is to check the uploaded physical file. ( Do not trust any headers or other information ) - errors, - min and max size in byte and pixel, - allowed mime types. Be aware of any file naming problems (allowed characters and conflicts).
  4. If everything is ok move the file to the correct folder, add the file name to the db record and insert it-
  5. Sanitize the image? Thumbs?
  6. Find out how you want to handle edits and deletions of the db records. Keep or delete the images?
  7. You will probably need some utility programs to check the correlation between db table and physical image files in the near future, if it's about more then a few images.

Do not add any Ajax functionality until this works to 100%.

You can take use of the methods in Joomlas JImage class.

1

You can extend the save() method to get this functionality :

public function save($data = array(), $key = 'id')
{
    // Neccesary libraries and variables
    jimport('joomla.filesystem.file');

    //Debugging 
    ini_set("display_error" , 1);
    error_reporting(E_ALL);

    // Get input object
    $jinput = JFactory::getApplication()->input;

    // Get posted data
    $data  = $jinput->get('jform', null, 'raw');
    $file  = $jinput->files->get('jform');

    // renaming the file 
    $file_ext=strtolower(end(explode('.',JFile::makeSafe($file['pdf_file_path']['name']))));
    $filename = round(microtime(true)) . '.' . $file_ext;
    // Move the uploaded file into a permanent location.
    if ( $filename != '' ) {

        // Make sure that the full file path is safe.
        $filepath = JPath::clean( JPATH_ROOT."/images/uploads/variants/". $filename );

        // Move the uploaded file.
        if (JFile::upload( $file['pdf_file_path']['tmp_name'], $filepath )) {
              //DB save method
        } else {              
              //Error Reporting
        }        
    }

    JRequest::setVar('jform', $data, 'post');
    $return = parent::save($data);
    return $return;
}

hope it will help

  • In this function you are overriding the $data variable passed into the function. Why? – tonypartridge Jan 26 '17 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.