diff --git a/advancedfeaturesvalues.php b/advancedfeaturesvalues.php index 5470723..ef19a88 100644 --- a/advancedfeaturesvalues.php +++ b/advancedfeaturesvalues.php @@ -33,8 +33,8 @@ public function __construct() { $this->name = 'advancedfeaturesvalues'; $this->tab = 'administration'; - $this->version = '1.0.7'; - $this->author = 'Jérôme Danthinne'; + $this->version = '2.0.0'; + $this->author = 'Muhammad Jawaid Shamshad - based on Jérôme Danthinne module'; $this->need_instance = 0; $this->ps_versions_compliancy = array('min' => '1.5.3', 'max' => _PS_VERSION_); $this->bootstrap = true; @@ -62,6 +62,15 @@ public function install() if (!Db::getInstance()->execute(' ALTER TABLE '._DB_PREFIX_.'feature_value ADD position INT UNSIGNED NOT NULL DEFAULT 0;')) return false; + // Add parent_id_feature field to feature + if (!Db::getInstance()->execute(' + ALTER TABLE '._DB_PREFIX_.'feature ADD parent_id_feature INT UNSIGNED DEFAULT NULL;')) + return false; + // Add parent_id_feature_value field to feature_value + if (!Db::getInstance()->execute(' + ALTER TABLE '._DB_PREFIX_.'feature_value ADD parent_id_feature_value INT UNSIGNED DEFAULT NULL;')) + return false; + $features = Db::getInstance()->executeS(' SELECT GROUP_CONCAT(id_feature_value ORDER BY id_feature_value) AS id_feature_value,id_feature FROM '._DB_PREFIX_.'feature_value GROUP BY id_feature;'); @@ -101,6 +110,14 @@ public function uninstall() if (!Db::getInstance()->execute(' ALTER TABLE '._DB_PREFIX_.'feature_value DROP position;')) return false; + // Remove parent_id_feature_value field from feature_value + if (!Db::getInstance()->execute(' + ALTER TABLE '._DB_PREFIX_.'feature_value DROP parent_id_feature_value;')) + return false; + // Remove parent_id_feature field from feature + if (!Db::getInstance()->execute(' + ALTER TABLE '._DB_PREFIX_.'feature DROP parent_id_feature;')) + return false; return true; } diff --git a/config.xml b/config.xml index 3b15709..107c031 100644 --- a/config.xml +++ b/config.xml @@ -2,9 +2,9 @@ advancedfeaturesvalues - + - + 0 diff --git a/override/classes/Feature.php b/override/classes/Feature.php new file mode 100644 index 0000000..bd7946f --- /dev/null +++ b/override/classes/Feature.php @@ -0,0 +1,82 @@ + +* @copyright 2007-2015 PrestaShop SA +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ +class Feature extends FeatureCore +{ + /** @var string Name */ + public $parent_id_feature; + + /** + * @see ObjectModel::$definition + */ + public static $definition = array( + 'table' => 'feature', + 'primary' => 'id_feature', + 'multilang' => true, + 'fields' => array( + 'position' => array('type' => self::TYPE_INT, 'validate' => 'isInt'), + // Parent Feature ID + 'parent_id_feature' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => false), + + // Lang fields + 'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128), + ) + ); + + /** + * Get a parent feature id for a given id_feature + * + * @param integer $id_feature Feature id + * @return integer ID of parent feature + * @static + */ + public static function getParentFeatureID($id_feature) + { + return Db::getInstance()->getValue(' + SELECT parent_id_feature + FROM `'._DB_PREFIX_.'feature` f + WHERE f.`id_feature` = '.(int)$id_feature + ); + } + + /** + * Get all features for a given language except for given id + * + * @param integer $id_lang Language id + * @param integer $id_feature Feature id to exclude + * @return array Multiple arrays with feature's data + * @static + */ + public static function getFeaturesExcept($id_lang, $id_feature, $with_shop = true) + { + return Db::getInstance()->executeS(' + SELECT DISTINCT f.id_feature, f.*, fl.* + FROM `'._DB_PREFIX_.'feature` f + '.($with_shop ? Shop::addSqlAssociation('feature', 'f') : '').' + LEFT JOIN `'._DB_PREFIX_.'feature_lang` fl ON (f.`id_feature` = fl.`id_feature` AND fl.`id_lang` = '.(int)$id_lang.') + WHERE f.id_feature != '.(int)$id_feature.' + ORDER BY f.`position` ASC'); + } +} \ No newline at end of file diff --git a/override/classes/FeatureValue.php b/override/classes/FeatureValue.php index fff6fea..d954f16 100644 --- a/override/classes/FeatureValue.php +++ b/override/classes/FeatureValue.php @@ -27,6 +27,7 @@ class FeatureValue extends FeatureValueCore { public $position; + public $parent_id_feature_value; public static $definition = array( 'table' => 'feature_value', @@ -34,6 +35,7 @@ class FeatureValue extends FeatureValueCore 'multilang' => true, 'fields' => array( 'id_feature' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), + 'parent_id_feature_value' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => false), 'position' => array('type' => self::TYPE_INT, 'validate' => 'isInt'), 'custom' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'), 'value' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 255), diff --git a/override/controllers/admin/AdminFeaturesController.php b/override/controllers/admin/AdminFeaturesController.php index 1c74775..afe41c7 100644 --- a/override/controllers/admin/AdminFeaturesController.php +++ b/override/controllers/admin/AdminFeaturesController.php @@ -26,6 +26,108 @@ 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' + ), + 'parent_id_feature' => array( + 'title' => $this->l('ParentID'), + '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 with Parent'), + '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 + ), + array( + 'type' => 'select', + 'label' => $this->l('Parent Feature'), + 'name' => 'parent_id_feature', + 'options' => array( + 'query' => Feature::getFeaturesExcept($this->context->language->id, Tools::getValue('id_feature')), + 'id' => 'id_feature', + 'name' => 'name' + ), + 'required' => true + ) + ) + ); + + if (Shop::isFeatureActive()) + { + $this->fields_form['input'][] = array( + 'type' => 'shop', + 'label' => $this->l('Shop association'), + 'name' => 'checkBoxShopAsso', + ); + } + + $this->fields_form['submit'] = array( + 'title' => $this->l('Save'), + ); + + return AdminController::renderForm(); + } + public function renderView() { if (($id = Tools::getValue('id_feature'))) @@ -58,6 +160,11 @@ public function renderView() 'value' => array( 'title' => $this->l('Value') ), + 'parent_id_feature_value' => array( + 'title' => $this->l('ParentID'), + 'align' => 'center', + 'class' => 'fixed-width-xs' + ), 'position' => array( 'title' => $this->l('Position'), 'filter_key' => 'a!position', @@ -75,6 +182,105 @@ public function renderView() } } + /** + * AdminController::renderForm() override + * @see AdminController::renderForm() + */ + public function initFormFeatureValue() + { + $this->setTypeValue(); + + $parent_id = Feature::getParentFeatureID((int)Tools::getValue('id_feature')); + + $this->fields_form[0]['form'] = array( + 'legend' => array( + 'title' => $this->l('Feature value'), + 'icon' => 'icon-info-sign' + ), + 'input' => array( + array( + 'type' => 'select', + 'label' => $this->l('Feature'), + 'name' => 'id_feature', + 'options' => array( + 'query' => Feature::getFeatures($this->context->language->id), + 'id' => 'id_feature', + 'name' => 'name' + ), + 'required' => true + ), + array( + 'type' => 'text', + 'label' => $this->l('Value'), + 'name' => 'value', + 'lang' => true, + 'size' => 33, + 'hint' => $this->l('Invalid characters:').' <>;=#{}', + 'required' => true + ), + array( + 'type' => 'select', + 'label' => $this->l('Parent Feature Value'), + 'name' => 'parent_id_feature_value', + 'options' => array( + 'query' => FeatureValue::getFeatureValuesWithLang($this->context->language->id, $parent_id), + 'id' => 'id_feature_value', + 'name' => 'value' + ), + 'required' => true + ), + ), + 'submit' => array( + 'title' => $this->l('Save'), + ), + 'buttons' => array( + 'save-and-stay' => array( + 'title' => $this->l('Save then add another value'), + 'name' => 'submitAdd'.$this->table.'AndStay', + 'type' => 'submit', + 'class' => 'btn btn-default pull-right', + 'icon' => 'process-icon-save' + ) + ) + ); + + $this->fields_value['id_feature'] = (int)Tools::getValue('id_feature'); + + // Create Object FeatureValue + $feature_value = new FeatureValue(Tools::getValue('id_feature_value')); + + $this->tpl_vars = array( + 'feature_value' => $feature_value, + ); + + $this->getlanguages(); + $helper = new HelperForm(); + $helper->show_cancel_button = true; + + $back = Tools::safeOutput(Tools::getValue('back', '')); + if (empty($back)) + $back = self::$currentIndex.'&token='.$this->token; + if (!Validate::isCleanHtml($back)) + die(Tools::displayError()); + + $helper->back_url = $back; + $helper->currentIndex = self::$currentIndex; + $helper->token = $this->token; + $helper->table = $this->table; + $helper->identifier = $this->identifier; + $helper->override_folder = 'feature_value/'; + $helper->id = $feature_value->id; + $helper->toolbar_scroll = false; + $helper->tpl_vars = $this->tpl_vars; + $helper->languages = $this->_languages; + $helper->default_form_language = $this->default_form_language; + $helper->allow_employee_form_lang = $this->allow_employee_form_lang; + $helper->fields_value = $this->getFieldsValue($feature_value); + $helper->toolbar_btn = $this->toolbar_btn; + $helper->title = $this->l('Add a new feature value'); + $this->content .= $helper->generateForm($this->fields_form); + } + public function ajaxProcessUpdatePositions() { if ($this->tabAccess['edit'] === '1')