166 lines
5.8 KiB
PHP
166 lines
5.8 KiB
PHP
<?php
|
|
/**
|
|
* 2007-2022 PrestaShop
|
|
*
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* This source file is subject to the Academic Free License (AFL 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/afl-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 license@prestashop.com 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 <contact@prestashop.com>
|
|
* @copyright 2007-2022 PrestaShop SA
|
|
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
|
|
* International Registered Trademark & Property of PrestaShop SA
|
|
*/
|
|
|
|
class PchCustomerfieldModel extends ObjectModel
|
|
{
|
|
public $id_pchcustomerfield;
|
|
public $title;
|
|
public $position;
|
|
public $active;
|
|
|
|
public static $definition = array(
|
|
'table' => 'pchcustomerfield',
|
|
'primary' => 'id_pchcustomerfield',
|
|
'multilang' => true,
|
|
'fields' => array(
|
|
'id_pchcustomerfield' => array(
|
|
'type' => self::TYPE_INT
|
|
),
|
|
'title' => array(
|
|
'type' => self::TYPE_STRING,
|
|
'validate' => 'isString',
|
|
'lang' => true,
|
|
'required' => true
|
|
),
|
|
'position' => array(
|
|
'type' => self::TYPE_INT,
|
|
'validate' => 'isunsignedInt',
|
|
'required' => true
|
|
),
|
|
'active' => array(
|
|
'type' => self::TYPE_BOOL,
|
|
'validate' => 'isBool',
|
|
'required' => true
|
|
),
|
|
),
|
|
);
|
|
|
|
public function updatePosition($way, $position)
|
|
{
|
|
if (!$res = Db::getInstance()->executeS(
|
|
'SELECT `id_pchcustomerfield`, `position`
|
|
FROM `'._DB_PREFIX_.'pchcustomerfield`
|
|
WHERE id_pchcustomerfield = '.(int)Tools::getValue('id').'
|
|
ORDER BY `position` ASC'
|
|
)) {
|
|
return false;
|
|
}
|
|
|
|
foreach ($res as $field) {
|
|
if ((int) $field['id_pchcustomerfield'] == (int) $this->id) {
|
|
$moved_field = $field;
|
|
}
|
|
}
|
|
|
|
if (!isset($moved_field) || !isset($position)) {
|
|
return false;
|
|
}
|
|
|
|
// < and > statements rather than BETWEEN operator
|
|
// since BETWEEN is treated differently according to databases
|
|
return Db::getInstance()->execute('
|
|
UPDATE `'._DB_PREFIX_.'pchcustomerfield`
|
|
SET `position`= `position` '.($way? '- 1' : '+ 1').'
|
|
WHERE `position`
|
|
'.($way
|
|
? '> '.(int)$moved_field['position'].' AND `position` <= '.(int)$position
|
|
: '< '.(int)$moved_field['position'].' AND `position` >= '.(int)$position))
|
|
&& Db::getInstance()->execute('
|
|
UPDATE `'._DB_PREFIX_.'pchcustomerfield`
|
|
SET `position` = '.(int)$position.'
|
|
WHERE `id_pchcustomerfield` = '.(int)$moved_field['id_pchcustomerfield']);
|
|
}
|
|
|
|
public static function getHigherPosition()
|
|
{
|
|
$sql = 'SELECT MAX(`position`) FROM `'._DB_PREFIX_.'pchcustomerfield`';
|
|
$position = DB::getInstance()->getValue($sql);
|
|
return (is_numeric($position)) ? $position : -1;
|
|
}
|
|
|
|
public static function positionOccupied($position)
|
|
{
|
|
if (!$position) {
|
|
return false;
|
|
}
|
|
|
|
$sql = 'SELECT * FROM `'._DB_PREFIX_.'pchcustomerfield` WHERE position = '.(int)$position;
|
|
return (bool)DB::getInstance()->getRow($sql);
|
|
}
|
|
|
|
public static function getSpecializationValues($id_lang = null, $all_fields = false, $active = true)
|
|
{
|
|
if (!$id_lang) {
|
|
$id_lang = (int)Context::getContext()->language->id;
|
|
}
|
|
|
|
$specializationQuery = 'SELECT cf.`id_pchcustomerfield`, cfl.`title`
|
|
FROM `'._DB_PREFIX_.'pchcustomerfield` cf
|
|
INNER JOIN `'._DB_PREFIX_.'pchcustomerfield_lang` cfl
|
|
ON (cf.id_pchcustomerfield = cfl.id_pchcustomerfield AND cfl.id_lang = '.(int)$id_lang.')';
|
|
|
|
if (!$all_fields) {
|
|
$specializationQuery .= ' WHERE cf.active = '.(int)$active;
|
|
}
|
|
|
|
$specializationQuery .= ' ORDER BY cf.`position` ASC';
|
|
|
|
return Db::getInstance()->executeS($specializationQuery);
|
|
}
|
|
|
|
public static function getSpecializationByCustomerId($id_lang = null, $id_customer)
|
|
{
|
|
if (!$id_customer) {
|
|
return false;
|
|
}
|
|
|
|
if (!$id_lang) {
|
|
$id_lang = (int)Context::getContext()->language->id;
|
|
}
|
|
|
|
$specializationQuery = 'SELECT cfl.`title` FROM `'._DB_PREFIX_.'customer` c INNER JOIN `'._DB_PREFIX_.'pchcustomerfield_lang` cfl
|
|
ON (c.specialization = cfl.id_pchcustomerfield AND c.id_customer = '.$id_customer.' AND cfl.id_lang = '.(int)$id_lang.')';
|
|
|
|
return Db::getInstance()->getValue($specializationQuery);
|
|
}
|
|
|
|
public static function getSpecializationBySpecializationId($id_lang = null, $id_specialization)
|
|
{
|
|
if (!$id_specialization) {
|
|
return false;
|
|
}
|
|
|
|
if (!$id_lang) {
|
|
$id_lang = (int)Context::getContext()->language->id;
|
|
}
|
|
|
|
$specializationQuery = 'SELECT cfl.`title` FROM `'._DB_PREFIX_.'customer` c INNER JOIN `'._DB_PREFIX_.'pchcustomerfield_lang` cfl
|
|
ON (c.specialization = cfl.id_pchcustomerfield AND c.specialization = '.$id_specialization.' AND cfl.id_lang = '.(int)$id_lang.')';
|
|
|
|
return Db::getInstance()->getValue($specializationQuery);
|
|
}
|
|
}
|