Прайслист — различия между версиями
Материал из Umicms
Mad grant (обсуждение | вклад) |
Mad grant (обсуждение | вклад) |
||
| Строка 3: | Строка 3: | ||
== Общее описание == | == Общее описание == | ||
== Листинг макроса== | == Листинг макроса== | ||
| + | <source lang="php"> | ||
| + | //возвращаем прайслист | ||
| + | public function getPriceList($asFile = '', $parent = '', $childs = 10, $expire = 0, $fileName = '', $template = 'default'){ | ||
| + | if(!$asFile) $asFile = 'yes'; | ||
| + | if(!is_numeric($expire)){ | ||
| + | throw new publicException('<b>В макросе emarket getPriceList() необходимо указать корректное значение второго параметра $expire<b/>'); | ||
| + | } | ||
| + | if(!$parent){ | ||
| + | $parentId = cmsController::getInstance()->getCurrentElementId(); | ||
| + | }else{ | ||
| + | $parentId = def_module::analyzeRequiredPath($parent); | ||
| + | if($parentId === false && $parent != KEYWORD_GRAB_ALL) { | ||
| + | throw new publicException('<b>В макросе emarket getPriceList() необходимо указать корректное значение пятого параметра $parent<b/>'); | ||
| + | } | ||
| + | } | ||
| + | if(!$childs) $childs = 10; | ||
| + | if(!is_numeric($childs)){ | ||
| + | throw new publicException('<b>В макросе emarket getPriceList() необходимо указать корректное значение шестого параметра $childs<b/>'); | ||
| + | } | ||
| + | $hierarchy = umiHierarchy::getInstance(); | ||
| + | $page = $hierarchy->getElement($parentId); | ||
| + | $altName = $page->getAltName(); | ||
| + | switch ($asFile) { | ||
| + | case 'no': | ||
| + | $isXslt = def_module::isXSLTResultMode(); | ||
| + | if ($isXslt){ | ||
| + | $path = self::getPath('/sys-temp/price_as_page/', $altName .'-cashed-as-array.txt'); | ||
| + | if(is_file($path)) $mtime = filemtime($path); | ||
| + | if(!is_file($path) || time() > ($mtime + $expire)){ | ||
| + | $pages = self::getQuery($parentId, $childs); | ||
| + | $result = self::getResult($pages, $template); | ||
| + | file_put_contents($path, serialize($result)); | ||
| + | unset($path, $mtime, $pages); | ||
| + | return $result; | ||
| + | }else{ | ||
| + | return unserialize(file_get_contents($path)); | ||
| + | } | ||
| + | }else{ | ||
| + | $path = self::getPath('/sys-temp/price_as_page/', $altName .'-cashed-as-query.txt'); | ||
| + | if(is_file($path)) $mtime = filemtime($path); | ||
| + | if(!is_file($path) || time() > ($mtime + $expire)){ | ||
| + | $pages = self::getQuery($parentId, $childs); | ||
| + | file_put_contents($path, serialize($pages)); | ||
| + | $result = self::getResult($pages, $template); | ||
| + | unset($path, $mtime, $pages); | ||
| + | return $result; | ||
| + | }else{ | ||
| + | return self::getResult(unserialize(file_get_contents($path)), $template); | ||
| + | } | ||
| + | } | ||
| + | break; | ||
| + | case 'yes': | ||
| + | if(!$fileName){ | ||
| + | $correctFileName = $altName; | ||
| + | }else{ | ||
| + | $correctFileName = translit::convert($fileName); | ||
| + | } | ||
| + | $path = self::getPath('/files/price_as_file/', $correctFileName . '.csv'); | ||
| + | if(is_file($path)) $mtime = filemtime($path); | ||
| + | if(!is_file($path) || time() > ($mtime + $expire)){ | ||
| + | $open = fopen($path, 'w+'); | ||
| + | $hat = array( | ||
| + | 'artikul' => 'Артикул', | ||
| + | 'name' => 'Наименование', | ||
| + | 'price' => 'Цена' | ||
| + | ); | ||
| + | fputcsv($open, $hat, ';'); | ||
| + | $pages = self::getQuery($parentId, $childs); | ||
| + | try { | ||
| + | foreach($pages as $page){ | ||
| + | $lines = array(); | ||
| + | $lines['artikul'] = $page->getValue('artikul'); | ||
| + | $lines['name'] = $page->getName(); | ||
| + | $lines['price'] = $page->getValue('price'); | ||
| + | fputcsv($open, $lines, ';'); | ||
| + | } | ||
| + | }catch (Exception $e) { | ||
| + | return '<b>Файл с прайсом не смог сгенерироваться из-за ошибки:</b>'.$e->getMessage(); | ||
| + | } | ||
| + | unset($pages, $page, $lines, $price, $name); | ||
| + | fclose($open); | ||
| + | return '/files/price_as_file/'.$correctFileName.'.csv'; | ||
| + | }else{ | ||
| + | return '/files/price_as_file/'.$correctFileName.'.csv'; | ||
| + | } | ||
| + | break; | ||
| + | default: | ||
| + | throw new publicException('<b>В макросе emarket getPriceList() необходимо указать корректное значение первого параметра $asFile</b>'); | ||
| + | } | ||
| + | } | ||
| + | //возвращаем путь до файла | ||
| + | private function getPath($dirName, $file){ | ||
| + | $folder = CURRENT_WORKING_DIR . $dirName; | ||
| + | $path = $folder . $file; | ||
| + | if(!is_dir($folder)) mkdir($folder, 0777, true); | ||
| + | return $path; | ||
| + | } | ||
| + | //возвращаем результат выборки | ||
| + | private function getQuery($parentId, $childs){ | ||
| + | $pages = new selector('pages'); | ||
| + | $pages->types('object-type')->name('catalog', 'object'); | ||
| + | $pages->where('hierarchy')->page($parentId)->childs($childs); | ||
| + | $pages->limit(0,2000); | ||
| + | return $pages; | ||
| + | } | ||
| + | //возвращаем результат парсинга шаблонов | ||
| + | private function getResult($pages, $template){ | ||
| + | if(!@$template) $template = 'default'; | ||
| + | list($itemsTemplate, $itemTemplate) = def_module::loadTemplates("emarket/{$template}", "items", "item"); | ||
| + | $item = array(); | ||
| + | $items = array(); | ||
| + | foreach($pages as $page){ | ||
| + | $item['attribute:id'] = $page->id; | ||
| + | $item['attribute:link'] = $page->link; | ||
| + | $item['attribute:price'] = $page->getValue('price'); | ||
| + | $item['attribute:artikul'] = $page->getValue('artikul'); | ||
| + | $item['attribute:name'] = $page->getName(); | ||
| + | $items[] = def_module::parseTemplate($itemTemplate, $item); | ||
| + | } | ||
| + | $items = array('subnodes:items' => $items); | ||
| + | $result = def_module::parseTemplate($itemsTemplate, $items); | ||
| + | return $result; | ||
| + | } | ||
| + | </source> | ||
== Применение в tpl == | == Применение в tpl == | ||
== Применение в xslt == | == Применение в xslt == | ||
== Полезные ссылки == | == Полезные ссылки == | ||
Версия 10:47, 17 октября 2013
Актуально для версии 2.9.1
Содержание
Общее описание
Листинг макроса
//возвращаем прайслист
public function getPriceList($asFile = '', $parent = '', $childs = 10, $expire = 0, $fileName = '', $template = 'default'){
if(!$asFile) $asFile = 'yes';
if(!is_numeric($expire)){
throw new publicException('<b>В макросе emarket getPriceList() необходимо указать корректное значение второго параметра $expire<b/>');
}
if(!$parent){
$parentId = cmsController::getInstance()->getCurrentElementId();
}else{
$parentId = def_module::analyzeRequiredPath($parent);
if($parentId === false && $parent != KEYWORD_GRAB_ALL) {
throw new publicException('<b>В макросе emarket getPriceList() необходимо указать корректное значение пятого параметра $parent<b/>');
}
}
if(!$childs) $childs = 10;
if(!is_numeric($childs)){
throw new publicException('<b>В макросе emarket getPriceList() необходимо указать корректное значение шестого параметра $childs<b/>');
}
$hierarchy = umiHierarchy::getInstance();
$page = $hierarchy->getElement($parentId);
$altName = $page->getAltName();
switch ($asFile) {
case 'no':
$isXslt = def_module::isXSLTResultMode();
if ($isXslt){
$path = self::getPath('/sys-temp/price_as_page/', $altName .'-cashed-as-array.txt');
if(is_file($path)) $mtime = filemtime($path);
if(!is_file($path) || time() > ($mtime + $expire)){
$pages = self::getQuery($parentId, $childs);
$result = self::getResult($pages, $template);
file_put_contents($path, serialize($result));
unset($path, $mtime, $pages);
return $result;
}else{
return unserialize(file_get_contents($path));
}
}else{
$path = self::getPath('/sys-temp/price_as_page/', $altName .'-cashed-as-query.txt');
if(is_file($path)) $mtime = filemtime($path);
if(!is_file($path) || time() > ($mtime + $expire)){
$pages = self::getQuery($parentId, $childs);
file_put_contents($path, serialize($pages));
$result = self::getResult($pages, $template);
unset($path, $mtime, $pages);
return $result;
}else{
return self::getResult(unserialize(file_get_contents($path)), $template);
}
}
break;
case 'yes':
if(!$fileName){
$correctFileName = $altName;
}else{
$correctFileName = translit::convert($fileName);
}
$path = self::getPath('/files/price_as_file/', $correctFileName . '.csv');
if(is_file($path)) $mtime = filemtime($path);
if(!is_file($path) || time() > ($mtime + $expire)){
$open = fopen($path, 'w+');
$hat = array(
'artikul' => 'Артикул',
'name' => 'Наименование',
'price' => 'Цена'
);
fputcsv($open, $hat, ';');
$pages = self::getQuery($parentId, $childs);
try {
foreach($pages as $page){
$lines = array();
$lines['artikul'] = $page->getValue('artikul');
$lines['name'] = $page->getName();
$lines['price'] = $page->getValue('price');
fputcsv($open, $lines, ';');
}
}catch (Exception $e) {
return '<b>Файл с прайсом не смог сгенерироваться из-за ошибки:</b>'.$e->getMessage();
}
unset($pages, $page, $lines, $price, $name);
fclose($open);
return '/files/price_as_file/'.$correctFileName.'.csv';
}else{
return '/files/price_as_file/'.$correctFileName.'.csv';
}
break;
default:
throw new publicException('<b>В макросе emarket getPriceList() необходимо указать корректное значение первого параметра $asFile</b>');
}
}
//возвращаем путь до файла
private function getPath($dirName, $file){
$folder = CURRENT_WORKING_DIR . $dirName;
$path = $folder . $file;
if(!is_dir($folder)) mkdir($folder, 0777, true);
return $path;
}
//возвращаем результат выборки
private function getQuery($parentId, $childs){
$pages = new selector('pages');
$pages->types('object-type')->name('catalog', 'object');
$pages->where('hierarchy')->page($parentId)->childs($childs);
$pages->limit(0,2000);
return $pages;
}
//возвращаем результат парсинга шаблонов
private function getResult($pages, $template){
if(!@$template) $template = 'default';
list($itemsTemplate, $itemTemplate) = def_module::loadTemplates("emarket/{$template}", "items", "item");
$item = array();
$items = array();
foreach($pages as $page){
$item['attribute:id'] = $page->id;
$item['attribute:link'] = $page->link;
$item['attribute:price'] = $page->getValue('price');
$item['attribute:artikul'] = $page->getValue('artikul');
$item['attribute:name'] = $page->getName();
$items[] = def_module::parseTemplate($itemTemplate, $item);
}
$items = array('subnodes:items' => $items);
$result = def_module::parseTemplate($itemsTemplate, $items);
return $result;
}