Jump to content

Prestashop ObjectModel image upload


belicslavko

Recommended Posts

I have a problem. I need to create module for custom upload image. Render form function:

public function renderForm()
{
// Building the Add/Edit form
$this->fields_form = array(
'legend' => array(
'title' => $this->l('Create/Edit design')
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Name:'),
'name' => 'name',
'size' => 33,
'required' => true,

),
array(
'type' => 'file',
'label' => $this->l('Image:'),
'name' => 'image',
'display_image' => TRUE,
'desc' => $this->l('Upload image from your computer')
), 
),

'submit' => array(
'title' => $this->l(' Save '),
'class' => 'submit'
),
);

return parent::renderForm();
}

In objectmodel I need to add image upload definition. MyObject class:

class MyObject extends ObjectModel
{
/** @var string Name */
public $name; 
public $image; 
public $id_design;

/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'design',
'primary' => 'id_design',
'fields' => array(

'name' => array(
'type' => self::TYPE_STRING,
'validate' => 'isGenericName',
'required' => true,
'size' => 64
), 
'image' => array(
'type' => self::TYPE_STRING,
'required' => true,
), 
),
);
}

After submit this form name is save but image is not upload. Any Help?

Link to comment
Share on other sites

  • 1 month later...
if ($_FILES['your_ref']['name'] != '') {
                $file = Tools::fileAttachment('your_ref');
                $sqlExtension = pathinfo($file['name'], PATHINFO_EXTENSION);
                $mimeType = array('image/png', 'image/x-png');

                if (!$file || empty($file) || $sqlExtension != 'png' || !in_array($file['mime'], $mimeType))
                    return $this->_html .= $this->displayError($this->l('Bad format image file') . ': ' . $this->l('Only png format file accepted'));
                else {
                    move_uploaded_file($file['tmp_name'], $your_rep . $file['name']);
                    $image_name = $file['name'];
                }
                @unlink($file);
            }

An example for a png image only...

Link to comment
Share on other sites

in your helper form add this:

array(
                        'type' => 'file',
                        'label' => $this->l('Upload a image'),
                        'desc' => $this->l('Only png format file accepted'),
                        'name' => 'your_ref',
                        'hint' =>  $this->l('Upload your own image.').' '.$this->l('Only png format file accepted'),
                        'class' => 'fixed-width-xxl',
                    ),
Link to comment
Share on other sites

The ajaxProcessAddAttachment is not called. 

<?php

if (defined('__PS_VERSION_')) {
	exit('Restricted Access!!!');
}

class AdminBlockaboutController extends ModuleAdminController {

	public function __construct() {

		$this->table = 'staff';
		$this->list_simple_header = FALSE;
		$this->className = 'StaffModel';
		$this->position_identifier = 'id_staff';
		$this->lang = TRUE;
		$this->bootstrap = TRUE;

		$this->addRowAction('edit');
		$this->addRowAction('delete');

		$this->bulk_actions = array(
			'delete' => array(
				'text' => $this->l('Delete selected'),
				'confirm' => $this->l('Delete selected items?')
			)
		);

		$this->fields_list = array(
			'id_staff' => array(
				'title' => $this->l('ID'),
				'align' => 'center',
				'width' => 25
			),
			'name' => array(
				'title' => $this->l('Name'),
				'width' => 'auto'
			),
			'function' => array(
				'title' => $this->l('Function'),
				'width' => 'auto'
			),
			'photo' => array(
				'title' => $this->l('Photo'),
				'width' => 'auto',
				 'name' => 'photo',
			),
			'active' => array(
				'title' => $this->l('Status'),
				'width' => 'auto',
				'active' => 'status'
			),
			'position' => array(
				'title' => $this->l('Position'),
				'width' => '70',
				'align' => 'center',
				'position' => 'position',
			),
		);

//		$this->fields_options = array(
//			'contact' => array(
//				'title' =>	$this->l('Contact options'),
//				'fields' =>	array(
//					'PS_CUSTOMER_SERVICE_FILE_UPLOAD' => array(
//						'title' => $this->l('Allow file uploading'),
//						'hint' => $this->l('Allow customers to upload files using the contact page.'),
//						'type' => 'bool'
//					),
//					'PS_CUSTOMER_SERVICE_SIGNATURE' => array(
//						'title' => $this->l('Default message'),
//						'hint' => $this->l('Please fill out the message fields that appear by default when you answer a thread on the customer service page.'),
//						'type' => 'textareaLang',
//						'lang' => true
//					)
//				),
//				'submit' => array('title' => $this->l('Save'))
//			),
//		);

//		$this->fields_options = array(
//			'general' => array(
//				'title' =>	$this->l('Content'),
//				'fields' =>	array(
//					'PS_BLOCKABOUT_CONTENT' => array(
//						'title' => $this->l('Content'),
//						'type' => 'textareaLang',
////						'lang' => TRUE,
//						'autoload_rte' => 'rte'
//					)
//				),
//				'submit' => array('title' => $this->l('Save'))
//			),
//		);

		parent::__construct();
	}

	public function ajaxProcessAddAttachment() {
		if ($_FILES['photo']['name'] != '') {
			$file = Tools::fileAttachment('photo');
			$sqlExtension = pathinfo($file['name'], PATHINFO_EXTENSION);
			$mimeType = array('image/png', 'image/x-png');

			if (!$file || empty($file) || $sqlExtension != 'png' || !in_array($file['mime'], $mimeType))
				return $this->_html .= $this->displayError($this->l('Bad format image file') . ': ' . $this->l('Only png format file accepted'));
			else {
				move_uploaded_file($file['tmp_name'], $your_rep . $file['name']);
				$image_name = $file['name'];
			}
			@unlink($file);
		}
	}

	public function renderForm() {
		$this->fields_form = array(
			'tinymce' => TRUE,
			'legend' => array(
				'title' => $this->l('Staff'),
				'icon' => 'icon-user'
			),
			'input' => array(
				array(
					'type' => 'text',
					'label' => $this->l('Name'),
					'name' => 'name',
					'size' => 33,
					'lang' => FALSE,
					'required' => TRUE,
				),
				array(
					'type' => 'text',
					'label' => $this->l('Function'),
					'name' => 'function',
					'size' => 33,
					'lang' => TRUE,
					'required' => TRUE,
				),
				array(
					'type' => 'file',
					'lang' => FALSE,
					'label' => $this->l('Photo'),
					'name' => 'photo',
//					'display_image' => TRUE,
//					'image' => $image_url ? $image_url : false,
//					'size' => $image_size,
//					'col' => 6,
//					'hint' => $this->l('Upload a maker image from your computer.')
				),
				array(
					'type' => 'switch',
					'label' => $this->l('Active'),
					'name' => 'active',
					'required' => FALSE,
					'is_bool' => TRUE,
					'values' => array(
						array(
							'id' => 'active_on',
							'value' => 1,
							'label' => $this->l('Enabled')
						),
						array(
							'id' => 'active_off',
							'value' => 0,
							'label' => $this->l('Disabled')
						)
					),
				),
			),
			'submit' => array(
				'title' => $this->l('Save')
			)
		);

		return parent::renderForm();
	}
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...