...
 
Commits (4)
<?php
/**
* This file contains package_quiqqer_products_ajax_products_update
*/
use QUI\ERP\Products\Utils\Products;
/**
* Update a product
*
* @param string $params - JSON query params
*
* @return array
*/
QUI::$Ajax->registerFunction(
'package_quiqqer_products_ajax_products_checkUrl',
function ($urls, $category) {
try {
QUI\ERP\Products\Utils\Products::checkUrlByUrlFieldValue(
\json_decode($urls, true),
$category
);
} catch (QUI\Exception $Exception) {
return [
'exists' => true,
'message' => $Exception->getMessage()
];
}
return [
'exists' => false,
'message' => ''
];
},
['urls', 'category'],
'Permission::checkAdminUser'
);
......@@ -13,6 +13,7 @@ define('package/quiqqer/products/bin/controls/products/Product', [
'qui/controls/buttons/ButtonSwitch',
'qui/controls/windows/Confirm',
'qui/utils/Form',
'Ajax',
'Locale',
'Users',
'controls/grid/Grid',
......@@ -35,11 +36,11 @@ define('package/quiqqer/products/bin/controls/products/Product', [
'text!package/quiqqer/products/bin/controls/products/CreateField.html',
'css!package/quiqqer/products/bin/controls/products/Product.css'
], function (QUI, QUIPanel, QUIButton, QUISwitch, QUIButtonSwitch, QUIConfirm, QUIFormUtils, QUILocale,
Users, Grid, FolderViewer, Mustache, Packages, Locker,
], function (QUI, QUIPanel, QUIButton, QUISwitch, QUIButtonSwitch, QUIConfirm, QUIFormUtils,
QUIAjax, QUILocale, Users, Grid, FolderViewer, Mustache, Packages, Locker,
Products, Product, Categories, Fields, FieldUtils, FieldWindow,
CategorySelect, FieldTypeSelect,
informationTemplate, templateProductData, templateProductPrices, templateField) {
CategorySelect, FieldTypeSelect, informationTemplate, templateProductData,
templateProductPrices, templateField) {
"use strict";
var lg = 'quiqqer/products',
......@@ -68,7 +69,8 @@ define('package/quiqqer/products/bin/controls/products/Product', [
'openAttributeList',
'openFieldAdministration',
'$onCreateMediaFolderClick',
'$render'
'$render',
'$checkUrl'
],
options: {
......@@ -1068,6 +1070,17 @@ define('package/quiqqer/products/bin/controls/products/Product', [
Field.setValue(data[fieldId].value);
}
});
// set url events
var UrlRow = Container.getElement('[data-fieldid="19"]');
if (UrlRow) {
var inputs = UrlRow.getElements('input');
inputs.removeEvents('blur');
inputs.addEvent('blur', self.$checkUrl);
}
});
},
......@@ -2331,32 +2344,6 @@ define('package/quiqqer/products/bin/controls/products/Product', [
}
return false;
// self.$data[wantedId] = folder[0];
// self.$FileViewer.setAttribute('folderUrl', folder[0].value);
// self.$ImageViewer.setAttribute('folderUrl', folder[0].value);
//
// self.$ImageViewer.refresh();
// self.$ImageViewer.show();
//
// self.$FileViewer.refresh();
// self.$FileViewer.show();
// image fields
/*
var images = self.getElm().getElements(
'[data-qui="package/quiqqer/products/bin/controls/fields/types/Image"]'
);
images.each(function (Input) {
var quiId = Input.get('data-quiid'),
Control = QUI.Controls.getById(quiId);
if (Control) {
Control.setAttribute('productFolder', folder[0].value);
}
});
*/
});
},
......@@ -2382,6 +2369,31 @@ define('package/quiqqer/products/bin/controls/products/Product', [
self.getElm().getElements('.folder-missing-container').destroy();
});
},
/**
* Checks the url field
*
* @param event
*/
$checkUrl: function (event) {
var Target = event.target;
var FieldInput = Target.getParent('tr').getElement('[name="field-19"]');
var value = FieldInput.value;
this.$Product.getCategory().then(function (categoryId) {
QUIAjax.get('package_quiqqer_products_ajax_products_checkUrl', function (result) {
if (result.exists) {
QUI.getMessageHandler().then(function (MH) {
MH.addError(result.message, Target);
});
}
}, {
'package': 'quiqqer/products',
urls : value,
category : categoryId
});
});
}
});
});
......@@ -5,7 +5,7 @@
{{fieldTitleFreeTextSearch}}
</span>
<div class="quiqqer-products-search-field-container-field">
<input type="text" name="search"/>
<input type="text" name="search" autofocus/>
</div>
</label>
{{/freeTextSearch}}
......
......@@ -142,7 +142,9 @@ define('package/quiqqer/products/bin/controls/products/search/Form', [
QUI.parse(this.$Elm).then(function () {
var Field;
var controls = QUI.Controls.getControlsInElement(this.$Container);
var Search = this.$Elm.getElement('[name="search"]');
var getControlByFieldById = function (fieldId) {
for (var c = 0, len = controls.length; c < len; c++) {
......@@ -156,7 +158,7 @@ define('package/quiqqer/products/bin/controls/products/search/Form', [
for (i = 0, len = result.length; i < len; i++) {
id = result[i].id;
if (i === 0) {
if (!Search && i === 0) {
getControlByFieldById(result[i].id).focus();
}
......@@ -170,6 +172,10 @@ define('package/quiqqer/products/bin/controls/products/search/Form', [
Field.setSearchData(result[i].searchData);
}
}
if (Search) {
Search.focus();
}
}.bind(this));
if (this.getAttribute('searchbutton')) {
......@@ -193,6 +199,7 @@ define('package/quiqqer/products/bin/controls/products/search/Form', [
);
}
this.$loaded = true;
}.bind(this));
......
......@@ -1089,105 +1089,11 @@ class Model extends QUI\QDOM
$urls = $urlField[0]['value'];
}
$categoryId = $this->getCategory()->getId();
$urlCacheField = 'F'.Fields::FIELD_URL;
$table = QUI\ERP\Products\Utils\Tables::getProductCacheTableName();
$where = [];
$binds = [];
$i = 0;
foreach ($urls as $lang => $url) {
if (empty($url)) {
continue;
}
$this->checkUrlLength($url, $lang);
$binds[':lang'.$i] = $lang;
$binds[':url'.$i] = $url;
$binds[':category'.$i] = '%,'.$categoryId.',%';
$where[] = "(F19 LIKE :url{$i} AND lang LIKE :lang{$i} AND category LIKE :category{$i})";
$i++;
}
if (empty($where)) {
return;
}
$where = \implode(' OR ', $where);
$query = "
SELECT id, {$urlCacheField}
FROM {$table}
WHERE {$where}
";
$PDO = QUI::getDataBase()->getPDO();
$Statement = $PDO->prepare($query);
foreach ($binds as $bind => $value) {
$Statement->bindValue($bind, $value, \PDO::PARAM_STR);
}
$Statement->execute();
$result = $Statement->fetchAll();
// no results, all is fine
if (empty($result)) {
return;
}
foreach ($result as $entry) {
if ((int)$entry['id'] === $this->getId()) {
continue;
}
throw new Exception([
'quiqqer/products',
'exception.url.already.exists'
]);
}
}
/**
* Checks the urls length for the product
*
* @param string $url
* @param string $lang
* @throws Exception
*/
protected function checkUrlLength($url, $lang)
{
try {
$Category = $this->getCategory();
$projects = QUI::getProjectManager()->getProjects(true);
} catch (QUI\Exception $Exception) {
return;
}
/* @var $Project QUI\Projects\Project */
foreach ($projects as $Project) {
if ($Project->getLang() !== $lang) {
continue;
}
try {
$categoryUrl = $Category->getUrl($Project);
} catch (QUI\Exception $Exception) {
continue;
}
if (!empty($categoryUrl) && strlen($categoryUrl.'/'.$url) > 2000) {
throw new Exception([
'quiqqer/products',
'exception.url.is.too.long'
]);
}
}
QUI\ERP\Products\Utils\Products::checkUrlByUrlFieldValue(
$urls,
$this->getCategory()->getId(),
$this->getId()
);
}
/**
......
......@@ -34,7 +34,6 @@ use QUI\ERP\Products\Handler\Search as SearchHandler;
* frontend
* @todo produkt liste -> varianten produkt -> kein warenkorb button -> Zur Auswahl
* @todo canonical auf variante wenn variant=id
* @todo flag Variant Children an / auszeigen
*/
class VariantParent extends AbstractType
{
......
......@@ -7,7 +7,10 @@
namespace QUI\ERP\Products\Utils;
use QUI;
use QUI\ERP\Products\Handler\Categories;
use QUI\ERP\Products\Handler\Fields as FieldHandler;
use QUI\ERP\Products\Handler\Fields;
use QUI\ERP\Products\Product\Exception;
/**
* Class Products Helper
......@@ -277,4 +280,111 @@ class Products
return false;
}
/**
* @param array $urlFieldValue
* @param integer $categoryId
* @param integer|false $ignoreProductId - optional
*
* @throws Exception
*/
public static function checkUrlByUrlFieldValue($urlFieldValue, $categoryId, $ignoreProductId = false)
{
$urlCacheField = 'F'.Fields::FIELD_URL;
$table = QUI\ERP\Products\Utils\Tables::getProductCacheTableName();
$where = [];
$binds = [];
$i = 0;
foreach ($urlFieldValue as $lang => $url) {
if (empty($url)) {
continue;
}
self::checkUrlLength($url, $lang, $categoryId);
$binds[':lang'.$i] = $lang;
$binds[':url'.$i] = $url;
$binds[':category'.$i] = '%,'.$categoryId.',%';
$where[] = "(F19 LIKE :url{$i} AND lang LIKE :lang{$i} AND category LIKE :category{$i})";
$i++;
}
if (empty($where)) {
return;
}
$where = \implode(' OR ', $where);
$query = "
SELECT id, {$urlCacheField}
FROM {$table}
WHERE {$where}
";
$PDO = QUI::getDataBase()->getPDO();
$Statement = $PDO->prepare($query);
foreach ($binds as $bind => $value) {
$Statement->bindValue($bind, $value, \PDO::PARAM_STR);
}
$Statement->execute();
$result = $Statement->fetchAll();
// no results, all is fine
if (empty($result)) {
return;
}
foreach ($result as $entry) {
if ($ignoreProductId && (int)$entry['id'] === $ignoreProductId) {
continue;
}
throw new Exception([
'quiqqer/products',
'exception.url.already.exists'
]);
}
}
/**
* Checks the urls length for the product
*
* @param string $url
* @param string $lang
* @param integer $categoryId
*
* @throws Exception
*/
public static function checkUrlLength($url, $lang, $categoryId)
{
try {
$Category = Categories::getCategory($categoryId);
$projects = QUI::getProjectManager()->getProjects(true);
} catch (QUI\Exception $Exception) {
return;
}
/* @var $Project QUI\Projects\Project */
foreach ($projects as $Project) {
if ($Project->getLang() !== $lang) {
continue;
}
$categoryUrl = $Category->getUrl($Project);
if (!empty($categoryUrl) && \strlen($categoryUrl.'/'.$url) > 2000) {
throw new Exception([
'quiqqer/products',
'exception.url.is.too.long'
]);
}
}
}
}