Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Parent-Child relation between features #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions advancedfeaturesvalues.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;');
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<module>
<name>advancedfeaturesvalues</name>
<displayName><![CDATA[Advanced Features Values]]></displayName>
<version><![CDATA[1.0.7]]></version>
<version><![CDATA[2.0.0]]></version>
<description><![CDATA[Allows multiple values selection per feature, and features values ordering.]]></description>
<author><![CDATA[Jérôme Danthinne]]></author>
<author><![CDATA[Muhammad Jawaid Shamshad - based on J&eacute;r&ocirc;me Danthinne module]]></author>
<tab><![CDATA[administration]]></tab>
<confirmUninstall><![CDATA[Are you sure you want to uninstall?]]></confirmUninstall>
<is_configurable>0</is_configurable>
Expand Down
82 changes: 82 additions & 0 deletions override/classes/Feature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/*
* 2007-2015 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @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');
}
}
2 changes: 2 additions & 0 deletions override/classes/FeatureValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
class FeatureValue extends FeatureValueCore
{
public $position;
public $parent_id_feature_value;

public static $definition = array(
'table' => 'feature_value',
'primary' => 'id_feature_value',
'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),
Expand Down
206 changes: 206 additions & 0 deletions override/controllers/admin/AdminFeaturesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')))
Expand Down Expand Up @@ -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',
Expand All @@ -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')
Expand Down