Shemes Posted May 12, 2015 Share Posted May 12, 2015 (edited) Hello! First of all thanks for reading this. I'm trying to add extra inputs into features. What I'm trying to accomplish is moslty what it shows on the screenshot. Features should accept: Name Image Detail (or description, to be shown on a tooltip over the badge) URL (to make the badge clicable, if the user wants to redirect to an extended info page) I was trying to extend FeatureValueCore, but I don't really understand how to extend the database tables to include and save the new inputs. Any pointers? Ideas? Thanks! Edited May 12, 2015 by Shemes (see edit history) Link to comment Share on other sites More sharing options...
Shemes Posted May 14, 2015 Author Share Posted May 14, 2015 Hi again. I've got my idea mostly working. I don't know if it's the best way to achieve this, but is a way. I've got a couple of questions. This is what i've done. SQL modifications: ALTER TABLE `ps_feature_lang` add column `image` varchar (255); ALTER TABLE `ps_feature_lang` add column `detail` text; ALTER TABLE `ps_feature_lang` add column `url` varchar (255); Product class Product extends ProductCore { public static function getFrontFeaturesStatic($id_lang, $id_product) { if (!Feature::isFeatureActive()) return array(); if (!array_key_exists($id_product.'-'.$id_lang, self::$_frontFeaturesCache)) { self::$_frontFeaturesCache[$id_product.'-'.$id_lang] = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT name, value, pf.id_feature, fl.Image, fl.Detail, fl.Url FROM '._DB_PREFIX_.'feature_product pf LEFT JOIN '._DB_PREFIX_.'feature_lang fl ON (fl.id_feature = pf.id_feature AND fl.id_lang = '.(int)$id_lang.') LEFT JOIN '._DB_PREFIX_.'feature_value_lang fvl ON (fvl.id_feature_value = pf.id_feature_value AND fvl.id_lang = '.(int)$id_lang.') LEFT JOIN '._DB_PREFIX_.'feature f ON (f.id_feature = pf.id_feature AND fl.id_lang = '.(int)$id_lang.') '.Shop::addSqlAssociation('feature', 'f').' WHERE pf.id_product = '.(int)$id_product.' ORDER BY f.position ASC' ); } return self::$_frontFeaturesCache[$id_product.'-'.$id_lang]; } } AdminFeaturesController class AdminFeaturesController extends AdminFeaturesControllerCore { public function __construct() { $this->table = 'feature'; $this->className = 'Feature'; $this->list_id = 'feature'; $this->identifier = 'id_feature'; $this->lang = true; $this->fields_list = array( 'id_feature' => array( 'title' => $this->l('ID'), 'align' => 'center', 'class' => 'fixed-width-xs' ), 'name' => array( 'title' => $this->l('Name'), 'width' => 'auto', 'filter_key' => 'b!name' ), 'value' => array( 'title' => $this->l('Values'), 'orderby' => false, 'search' => false, 'align' => 'center', 'class' => 'fixed-width-xs' ), 'image' => array( 'title' => $this->l('Image'), 'orderby' => false, 'search' => false, 'align' => 'center', 'class' => 'fixed-width-xs' ), 'detail' => array( 'title' => $this->l('Detail'), 'orderby' => false, 'search' => false, 'align' => 'center', 'class' => 'fixed-width-xs' ), 'url' => array( 'title' => $this->l('Url'), 'orderby' => false, 'search' => false, 'align' => 'center', 'class' => 'fixed-width-xs' ), 'position' => array( 'title' => $this->l('Position'), 'filter_key' => 'a!position', 'align' => 'center', 'class' => 'fixed-width-xs', 'position' => 'position' ) ); $this->bulk_actions = array( 'delete' => array( 'text' => $this->l('Delete selected'), 'icon' => 'icon-trash', 'confirm' => $this->l('Delete selected items?') ) ); AdminController::__construct(); } /** * AdminController::renderForm() override * @see AdminController::renderForm() */ public function renderForm() { $this->toolbar_title = $this->l('Add a new feature'); $this->fields_form = array( 'legend' => array( 'title' => $this->l('Feature'), 'icon' => 'icon-info-sign' ), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Name'), 'name' => 'name', 'lang' => true, 'size' => 33, 'hint' => $this->l('Invalid characters:').' <>;=#{}', 'required' => true ) ) ); if (Shop::isFeatureActive()) { $this->fields_form['input'][] = array( 'type' => 'shop', 'label' => $this->l('Shop association'), 'name' => 'checkBoxShopAsso', ); } // new inputs $this->fields_form['input'][] = array( 'type' => 'text', 'label' => $this->l('Image'), 'name' => 'image', 'required' => false, 'lang' => true, ); $this->fields_form['input'][] = array( 'type' => 'text', 'label' => $this->l('Detail'), 'name' => 'detail', 'required' => false, 'lang' => true, ); $this->fields_form['input'][] = array( 'type' => 'text', 'label' => $this->l('Url'), 'name' => 'url', 'required' => false, 'lang' => true, ); $this->fields_form['submit'] = array( 'title' => $this->l('Save'), ); return AdminController::renderForm(); } } FeatureValue class Feature extends FeatureCore { public static $definition = array( 'table' => 'feature', 'primary' => 'id_feature', 'multilang' => true, 'fields' => array( 'position' => array('type' => self::TYPE_INT, 'validate' => 'isInt'), /* Lang fields */ 'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128), 'detail' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => false, 'size' => 255), 'url' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => false, 'size' => 255), 'image' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => false, 'size' => 255), ), ); } on the view i've got only a test {foreach from=$features item=feature} {p($feature)} {/foreach} This mostly works, but when I edit a feature, it doesn't get the current data on the new inputs (even if it's not blank, there is some data saved). And now i've got to re-think the image, now it's a name, and I uplaod the image by ftp, I'll want to use something more user friendly. Any ideas? Thanks! Link to comment Share on other sites More sharing options...
PascalVG Posted May 14, 2015 Share Posted May 14, 2015 To upload an image you can use this piece of code (copied from controllers/admin/AdminCategoriesController.php: array( 'type' => 'file', 'label' => $this->l('Image'), 'name' => 'image', 'display_image' => true, 'image' => $image_url ? $image_url : false, 'size' => $image_size, 'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->_category->id.'&token='.$this->token.'&deleteImage=1', 'hint' => $this->l('Upload a category logo from your computer.'), ), Do I understand correctly that the table DID store the values in the new fields when pressing save, but it doesn't re-load its values from the table to display on the edit page? pascal Link to comment Share on other sites More sharing options...
Shemes Posted May 14, 2015 Author Share Posted May 14, 2015 Yes, you did understand me right, the database has the values, the values are shown on the front end, BUT when I edit a feature, the values are not showing on the form. I'll try tomorrow the thing with the images. Do you know if that has any requirements? or can I just replace my Image piece of code with that? I'll experiment with it anyways tomorrow morning. Thanks a lot! Tomás. Link to comment Share on other sites More sharing options...
Shemes Posted May 15, 2015 Author Share Posted May 15, 2015 Ok! Now I've got working the image uploader and it shows the "badges" on the client side. The only thing that still bothers me is that whenever I try to edit a Feature, it doesn't auto-fill with the data that already exists on the new inputs. I was going to copy / paste the source like before, but I made a gist to have it a little more controlled. https://gist.github.com/tomasgarvalin/4d20fa7ed01d0e9e99d2 Thanks for the help! PS: Any ideas on how would I go about making the badges clickable to show different things? one can be a modal view with a youtube video, or a modal with a script that shows the product on 360º. Link to comment Share on other sites More sharing options...
jawaid Posted May 27, 2015 Share Posted May 27, 2015 I'm doing similar stuff where I added a custom field to Features. When I add a new Feature this new custom field also gets saves. But when I edit a Feature the value doesn't get updated. What could be the reason for that? Link to comment Share on other sites More sharing options...
Shemes Posted May 28, 2015 Author Share Posted May 28, 2015 I haven't found yet the way to fix that, I'm on the same boat. If I ever find the fix, I'll post it here. I've been working on other parts of the theme I'm making, but I still have that as an issue on my vcs to be worked on before release. Un saludo. Tomás. Link to comment Share on other sites More sharing options...
Shemes Posted June 9, 2015 Author Share Posted June 9, 2015 Could anyone explain to me if there is a need to have a getContent() function on an extension as it is on a module? I tried several things, I've made a module clean, with only this modifications and it still doesn't load the data once you load the form. It saves the data, it does everything as it is supposed to, but when I come back to an already created feature, it doesn't load the data that IS on the database. Could anybody lend a helping hand here? Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now