Jump to content

Overriding Suppliers to add gallery


naulacambra

Recommended Posts

Hello,

 

it's been 1 week since I've started to work on this. My target is to add a gallery field to the suppliers form in the admin panel.

 

I'm trying to use the same gallery field that use Categories.

 

I've already overrided the suppliers controller.

class AdminSuppliersController extends AdminSuppliersControllerCore {

   public function renderForm() {
        // loads current warehouse
        if (!($obj = $this->loadObject(true)))
            return;

        $image = _PS_SUPP_IMG_DIR_ . $obj->id . '.jpg';
        $image_url = ImageManager::thumbnail($image, $this->table . '_' . (int) $obj->id . '.' . $this->imageType, 350, $this->imageType, true, true);
        $image_size = file_exists($image) ? filesize($image) / 1000 : false;

        $tmp_addr = new Address();
        $res = $tmp_addr->getFieldsRequiredDatabase();
        $required_fields = array();
        foreach ($res as $row)
            $required_fields[(int) $row['id_required_field']] = $row['field_name'];

        $this->fields_form = array(
            'legend' => array(
                'title' => $this->l('Suppliers'),
                'icon' => 'icon-truck'
            ),
            'input' => array(
                array(
                    'type' => 'hidden',
                    'name' => 'id_address',
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Name'),
                    'name' => 'name',
                    'required' => true,
                    'col' => 4,
                    'hint' => $this->l('Invalid characters:') . ' <>;=#{}',
                ),
                (in_array('company', $required_fields) ?
                        array(
                    'type' => 'text',
                    'label' => $this->l('Company'),
                    'name' => 'company',
                    'display' => in_array('company', $required_fields),
                    'required' => in_array('company', $required_fields),
                    'maxlength' => 16,
                    'col' => 4,
                    'hint' => $this->l('Company name for this supplier')
                        ) : null
                ),
                array(
                    'type' => 'textarea',
                    'label' => $this->l('Description'),
                    'name' => 'description',
                    'lang' => true,
                    'hint' => array(
                        $this->l('Invalid characters:') . ' <>;=#{}',
                        $this->l('Will appear in the list of suppliers.')
                    ),
                    'autoload_rte' => 'rte' //Enable TinyMCE editor for short description
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Phone'),
                    'name' => 'phone',
                    'required' => in_array('phone', $required_fields),
                    'maxlength' => 16,
                    'col' => 4,
                    'hint' => $this->l('Phone number for this supplier')
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Mobile phone'),
                    'name' => 'phone_mobile',
                    'required' => in_array('phone_mobile', $required_fields),
                    'maxlength' => 16,
                    'col' => 4,
                    'hint' => $this->l('Mobile phone number for this supplier.')
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Address'),
                    'name' => 'address',
                    'maxlength' => 128,
                    'col' => 6,
                    'required' => true
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Zip/postal code'),
                    'name' => 'postcode',
                    'required' => in_array('postcode', $required_fields),
                    'maxlength' => 12,
                    'col' => 2,
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('City'),
                    'name' => 'city',
                    'maxlength' => 32,
                    'col' => 4,
                    'required' => true,
                ),
                array(
                    'type' => 'select',
                    'label' => $this->l('Country'),
                    'name' => 'id_country',
                    'required' => true,
                    'col' => 4,
                    'default_value' => (int) $this->context->country->id,
                    'options' => array(
                        'query' => Country::getCountries($this->context->language->id, false),
                        'id' => 'id_country',
                        'name' => 'name',
                    ),
                ),
                array(
                    'type' => 'select',
                    'label' => $this->l('State'),
                    'name' => 'id_state',
                    'col' => 4,
                    'options' => array(
                        'id' => 'id_state',
                        'query' => array(),
                        'name' => 'name'
                    )
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Region'),
                    'name' => 'address2',
                    'required' => in_array('address2', $required_fields),
                    'col' => 4,
                    'maxlength' => 32,
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Tipus producte'),
                    'name' => 'tipus_producte',
                    'col' => 4,
                ),
                array(
                    'type' => 'file',
                    'label' => $this->l('Logo'),
                    'name' => 'logo',
                    'display_image' => true,
                    'image' => $image_url ? $image_url : false,
                    'size' => $image_size,
                    'hint' => $this->l('Upload a supplier logo from your computer.')
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Video URL'),
                    'name' => 'url_video',
                    'col' => 4,
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Meta title'),
                    'name' => 'meta_title',
                    'lang' => true,
                    'col' => 4,
                    'hint' => $this->l('Forbidden characters:') . ' <>;=#{}'
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Meta description'),
                    'name' => 'meta_description',
                    'lang' => true,
                    'col' => 6,
                    'hint' => $this->l('Forbidden characters:') . ' <>;=#{}'
                ),
                array(
                    'type' => 'tags',
                    'label' => $this->l('Meta keywords'),
                    'name' => 'meta_keywords',
                    'lang' => true,
                    'col' => 6,
                    'hint' => array(
                        $this->l('To add "tags" click in the field, write something and then press "Enter".'),
                        $this->l('Forbidden characters:') . ' <>;=#{}'
                    )
                ),
                array(
                    'type' => 'switch',
                    'label' => $this->l('Enable'),
                    'name' => 'active',
                    'required' => false,
                    'class' => 't',
                    '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'),
            )
        );
        
        $this->tpl_form_vars['displayBackOfficeCategory'] = Hook::exec('displayBackOfficeCategory');
        
        // loads current address for this supplier - if possible
        $address = null;
        if (isset($obj->id)) {
            $id_address = Address::getAddressIdBySupplierId($obj->id);

            if ($id_address > 0)
                $address = new Address((int) $id_address);
        }

        // force specific fields values (address)
        if ($address != null) {
            $this->fields_value = array(
                'id_address' => $address->id,
                'phone' => $address->phone,
                'phone_mobile' => $address->phone_mobile,
                'address' => $address->address1,
                'address2' => $address->address2,
                'postcode' => $address->postcode,
                'city' => $address->city,
                'id_country' => $address->id_country,
                'id_state' => $address->id_state,
            );
        } else
            $this->fields_value = array(
                'id_address' => 0,
                'id_country' => Configuration::get('PS_COUNTRY_DEFAULT')
            );


        if (Shop::isFeatureActive()) {
            $this->fields_form['input'][] = array(
                'type' => 'shop',
                'label' => $this->l('Shop association'),
                'name' => 'checkBoxShopAsso',
            );
        }

        return AdminController::renderForm();
    }

Note that also i've added some other fields, but they don't affect this matter.

 

The important line that i've copied from the CategoriesController is 

$this->tpl_form_vars['displayBackOfficeCategory'] = Hook::exec('displayBackOfficeCategory');

Also, i've overrided the ModuleCore to add to the Suppliers Module the hook displayBackOfficeCategory.

class Module extends ModuleCore {

    protected $allowed_mod = array(
        'displayBackOfficeCategory' => array('blocksupplier'),
    );

    public function hookDisplayBackOfficeCategory($params) {
        if (in_array($this->name, $this->allowed_mod['displayBackOfficeCategory'])) {
            if (Context::getContext()->controller->className == 'Supplier') {
                $supplier = new Supplier((int) Tools::getValue('id_supplier'));
                $files = array();
                
                for ($i = 0; $i < 3; $i++) {
                    if (file_exists(_PS_SUPP_IMG_DIR_ . (int) $supplier->id . '-' . $i . '_thumb.jpg')) {
                        $files[$i]['type'] = HelperImageUploader::TYPE_IMAGE;
                        $files[$i]['image'] = ImageManager::thumbnail(_PS_SUPP_IMG_DIR_ . (int) $supplier->id . '-' . $i . '_thumb.jpg', $this->context->controller->table . '_' . (int) $supplier->id . '-' . $i . '_thumb.jpg', 100, 'jpg', true, true);
                        $files[$i]['delete_url'] = Context::getContext()->link->getAdminLink('AdminBlockCategories') . '&deleteThumb=' . $i . '&id_supplier=' . (int) $supplier->id;
                    }
                }

                $helper = new HelperImageUploader();
                $helper->setMultiple(true)->setUseAjax(true)->setName('thumbnail')->setFiles($files)->setMaxFiles(3)->setUrl(
                        Context::getContext()->link->getAdminLink('AdminBlockCategories') . '&ajax=1&id_supplier=' . $supplier->id
                        . '&action=uploadThumbnailImages');
                $this->smarty->assign('helper', $helper->render());
                return $this->display(_PS_MODULE_DIR_ . '/blocksupplier/blocksupplier.php', 'views/blocksupplier_admin.tpl');
            }
        } else {
            return false;
        }
    }

}

But doesn't work. I know that the hook is been firing, but the admin panel doesn't change. What i'm missing? Any help will be very welcome!

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...