<?php
/**
* СодеÑÐ¶Ð¸Ñ ÐºÐ»Ð°ÑÑ DivisionEditor
*
* @package energine
* @author dr.Pavka
* @copyright ColoCall 2006
* @version $Id: DivisionEditor.class.php,v 1.30 2008/01/15 11:04:05 pavka Exp $
*/
//require_once('core/modules/share/components/Grid.class.php');
//require_once('core/framework/TagManager.class.php');
/**
* РедакÑÐ¾Ñ Ñазделов
*
* @package energine
* @subpackage share
* @final
*/
final class DivisionEditor extends Grid {
/**
* РедакÑÐ¾Ñ Ñаблонов
*
* @var TemplateEditor
* @access private
*/
private $templateEditor;
/**
* РедакÑÐ¾Ñ Ð¿ÐµÑеводов
*
* @var TranslationEditor
* @access private
*/
private $transEditor;
/**
* ÐонÑÑÑÑкÑÐ¾Ñ ÐºÐ»Ð°ÑÑа
*
* @return void
*/
public function __construct($name, $module, Document $document, array $params = null) {
parent::__construct($name, $module, $document, $params);
$this->setTableName('share_sitemap');
$this->setTitle($this->translate('TXT_DIVISION_EDITOR'));
$this->setOrder(array('smap_order_num'=>QAL::ASC));
$this->setParam('recordsPerPage', false);
$this->setOrderColumn('smap_order_num');
//$this->setFilter(array('smap_pid'=>QAL::EMPTY_STRING));
}
/**
* ÐеÑод вÑÐ²Ð¾Ð´Ð¸Ñ ÑоÑÐ¼Ñ Ð½Ð°Ð·Ð½Ð°ÑÐµÐ½Ð¸Ñ Ð¿Ñав
*
* @return void
* @access protected
*/
protected function setPageRights() {
$this->setType(self::COMPONENT_TYPE_FORM);
//$this->addCrumb('TXT_SET_RIGHTS');
$this->setDataSetAction('save-rights');
$this->prepare();
}
/**
* СÑÑÐ¾Ð¸Ñ Ð²ÐºÐ»Ð°Ð´ÐºÑ Ð¿Ñав
*
* @return DOMNode
* @access private
*/
private function buildRightsTab() {
$builder = new SimpleBuilder($this->getTitle());
$id = $this->getFilter();
$id = (!empty($id))?current($id):false;
//полÑÑаем инÑоÑмаÑÐ¸Ñ Ð¾ вÑеÑ
гÑÑппаÑ
имеÑÑиÑ
ÑÑ Ð² ÑиÑÑеме
$groups = $this->dbh->select('user_groups', array('group_id', 'group_name', 'group_default_rights'));
$groups = convertDBResult($groups, 'group_id');
//Ñоздаем маÑÑиÑÑ
//название гÑÑппÑ/пеÑеÑÐµÐ½Ñ Ð¿Ñав
foreach ($groups as $groupID=>$groupInfo) {
$res[] = array('right_id'=>($this->getAction() == 'add')?$groupInfo['group_default_rights']:0, 'group_id'=>$groupID);
}
$resultData = new Data();
$resultData->load($res);
$builder->setData($resultData);
$rightsField = $resultData->getFieldByName('right_id');
$groupsField = $resultData->getFieldByName('group_id');
if ($id) {
//Ñоздаем пеÑеменнÑÑ ÑодеÑжаÑÑÑ Ð¸Ð´ÐµÐ½ÑиÑикÑоÑÑ Ð³ÑÑпп в коÑоÑÑе вÑ
Ð¾Ð´Ð¸Ñ Ð¿Ð¾Ð»ÑзоваÑелÑ
$data = $this->dbh->select('share_access_level', true, array('smap_id'=>$id));
if(is_array($data)) {
$data = convertDBResult($data, 'group_id', true);
for ($i=0; $i<$resultData->getRowCount(); $i++) {
//еÑли ÑÑÑÐ°Ð½Ð¾Ð²Ð»ÐµÐ½Ñ Ð¿Ñава Ð´Ð»Ñ Ð³ÑÑÐ¿Ð¿Ñ - изменÑем в обÑекÑе даннÑÑ
if (isset($data[$groupsField->getRowData($i)])) {
$rightsField->changeRowData($i, $data[$groupsField->getRowData($i)]['right_id']);
}
$groupsField->setRowProperty($i, 'group_id', $groupsField->getRowData($i));
}
}
}
for ($i=0; $i<$resultData->getRowCount(); $i++) {
$groupsField->setRowProperty($i, 'group_id', $groupsField->getRowData($i));
$groupsField->changeRowData($i, $groups[$groupsField->getRowData($i)]['group_name']);
}
$resultDD = new DataDescription();
$fd = new FieldDescription('group_id');
$fd->setSystemType(FieldDescription::FIELD_TYPE_STRING);
$fd->setMode(FieldDescription::FIELD_MODE_READ);
$fd->setLength(30);
$resultDD->addFieldDescription($fd);
$fd = new FieldDescription('right_id');
$fd->setSystemType(FieldDescription::FIELD_TYPE_SELECT);
$data = $this->dbh->select('user_group_rights', array('right_id', 'right_const as right_name'));
$data = array_map(create_function('$a', '$a["right_name"] = DBWorker::_translate("TXT_".$a["right_name"]); return $a;'), $data);
$data[] = array('right_id'=>0, 'right_name'=>$this->translate('TXT_NO_RIGHTS'));
$fd->loadAvailableValues($data, 'right_id', 'right_name');
$resultDD->addFieldDescription($fd);
$builder->setDataDescription($resultDD);
$builder->build();
$result = $this->doc->createElement('rights');
$result->setAttribute('title', $this->translate('TXT_RIGHTS'));
$result->appendChild($this->doc->importNode($builder->getResult(), true));
return $result;
}
/**
* ÐÐ»Ñ setRole Ñоздаем Ñвое опиÑание даннÑÑ
* ÐÐ»Ñ Ð¿Ð¾Ð»Ñ smap_pid ÑоÑмиÑÑеÑÑÑ ÐеÑево Ñазделов
*
* @return DataDescription
* @access protected
*/
protected function createDataDescription() {
$result = parent::createDataDescription();
//Ð´Ð»Ñ ÑедакÑиÑÐ¾Ð²Ð°Ð½Ð¸Ñ Ð¸ Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ð¸Ñ Ð½Ñжно ÑÑоÑмиÑоваÑÑ "кÑаÑивое деÑево Ñазделов"
if (in_array($this->getAction(), array('add', 'edit'))) {
$fd = $result->getFieldDescriptionByName('smap_pid');
$fd->setType(FieldDescription::FIELD_TYPE_STRING);
$fd->setMode(FieldDescription::FIELD_MODE_READ);
$result->getFieldDescriptionByName('smap_name')->removeProperty('nullable');
}
else {
//ÐÐ»Ñ Ñежима ÑпиÑка нам нÑжно вÑводиÑÑ Ð½Ðµ знаÑение а клÑÑ
if ($this->getType() == self::COMPONENT_TYPE_LIST) {
$smapPIDFieldDescription = $result->getFieldDescriptionByName('smap_pid');
if ($smapPIDFieldDescription) {
$smapPIDFieldDescription->setType(FieldDescription::FIELD_TYPE_INT);
}
}
if ($this->getAction() == 'getRawData') {
$field = new FieldDescription('smap_segment');
$field->setType(FieldDescription::FIELD_TYPE_STRING);
$field->addProperty('tableName', $this->getTableName());
$result->addFieldDescription($field);
}
}
return $result;
}
/**
* ÐобавлÑÐµÑ Ð´Ð°Ð½Ð½Ñе об УРÐ
*
* @return array
* @access protected
*/
protected function loadData() {
$result = parent::loadData();
if($result && $this->getAction() == 'getRawData') {
$result = array_map(create_function('$val', '$val["smap_segment"] = SiteMap::getInstance()->getURLByID($val["smap_id"]); return $val;'), $result);
}
return $result;
}
/**
* ÐодменÑем поÑÑÑоиÑÐµÐ»Ñ Ð´Ð»Ñ Ð¼ÐµÑода setPageRights
*
* @return Builder
* @access protected
*/
protected function prepare() {
parent::prepare();
$actionParams = $this->getActionParams();
if ($this->getAction() == 'edit') {
$field = $this->getData()->getFieldByName('smap_pid');
$smapSegment = '';
if($field->getRowData(0) !== null) {
$smapSegment = Sitemap::getInstance()->getURLByID($field->getRowData(0));
}
$smapName = simplifyDBResult($this->dbh->select($this->getTranslationTableName(), array('smap_name'), array('smap_id' => $field->getRowData(0), 'lang_id' => $this->document->getLang())), 'smap_name', true);
for ($i = 0; $i < count(Language::getInstance()->getLanguages()); $i++) {
$field->setRowProperty($i, 'data_name', $smapName);
$field->setRowProperty($i, 'segment', $smapSegment);
}
if ($this->getDataDescription()->getFieldDescriptionByName('tags')) {
$field = new Field('tags');
$tags = TagManager::getInstance()->getPageTags($this->getData()->getFieldByName('smap_id')->getRowData(0));
$tags = implode(', ', $tags);
$field->setData(array_fill(0, count(Language::getInstance()->getLanguages()), $tags));
$this->getData()->addField($field);
}
}
elseif ($this->getAction() == 'add' && !empty($actionParams)) {
$field = $this->getData()->getFieldByName('smap_pid');
$smapSegment = Sitemap::getInstance()->getURLByID($actionParams[0]);
$res = $this->dbh->select($this->getTranslationTableName(), array('smap_name'), array('smap_id' => $actionParams[0], 'lang_id' => $this->document->getLang()));
if (!empty($res)) {
$name = simplifyDBResult($res, 'smap_name', true);
for ($i = 0; $i < count(Language::getInstance()->getLanguages()); $i++) {
$field->changeRowData($i, $actionParams[0]);
$field->setRowProperty($i, 'data_name', $name);
$field->setRowProperty($i, 'segment', $smapSegment);
}
}
}
}
/**
* ÐеÑеопÑеделеннÑй внеÑний меÑод ÑоÑ
ÑанениÑ
* добавлено знаÑение ÑÑла ÑÑÑаниÑÑ
* ÐÑзÑÐ²Ð°ÐµÑ Ð²Ð½ÑÑÑенний меÑод ÑоÑ
ÑÐ°Ð½ÐµÐ½Ð¸Ñ saveData(), коÑоÑÑй и пÑÐ¾Ð¸Ð·Ð²Ð¾Ð´Ð¸Ñ ÑобÑÑвенно вÑе дейÑÑвиÑ
*
* @return void
* @access protected
*/
protected function save() {
$transactionStarted = $this->dbh->beginTransaction();
try {
$result = $this->saveData();
if (is_int($result)) {
$mode = 'insert';
$id = $result;
/*ТÑÑ Ð¿ÑиÑлоÑÑ Ð¿Ð¾Ð¹Ñи на извÑаÑÐ°ÐµÐ½Ð¸Ñ Ð´Ð»Ñ Ð¿Ð¾Ð»ÑÑÐµÐ½Ð¸Ñ Ð£Ð Ðа ÑÑÑаниÑÑ, поÑколÑÐºÑ Ð½Ð¾Ð²Ð¾ÑÐ¾Ð·Ð´Ð°Ð½Ð½Ð°Ñ ÑÑÑаниÑа еÑе не пÑиÑоединена к деÑевÑ*/
$smapPID = simplifyDBResult($this->dbh->select('share_sitemap', 'smap_pid', array('smap_id'=>$id)), 'smap_pid', true);
$url = $_POST[$this->getTableName()]['smap_segment'].'/';
if ($smapPID) {
$url = Sitemap::getInstance()->getURLByID($smapPID).$url;
}
}
else {
$mode = 'update';
$id = $this->getFilter();
$id = $id['smap_id'];
$url = Sitemap::getInstance()->getURLByID($id);
}
$transactionStarted = !($this->dbh->commit());
$JSONResponse = array(
'result' => true,
'url' => $url,
'mode' => $mode
);
}
catch (FormException $formError) {
$this->dbh->rollback();
//ФоÑмиÑÑем JS маÑÑив оÑибок коÑоÑÑй бÑÐ´ÐµÑ ÑазбиÑаÑÑÑÑ Ð½Ð° клиенÑе
$errors = $this->saver->getErrors();
foreach ($errors as $errorFieldName) {
$message['errors'][] = array(
'field'=>$this->translate('FIELD_'.strtoupper($errorFieldName)),
'message'=>$this->translate($this->saver->getDataDescription()->getFieldDescriptionByName($errorFieldName)->getPropertyValue('message'))
);
}
$JSONResponse = array_merge(array('result'=>false, 'header'=>$this->translate('TXT_SHIT_HAPPENS')), $message);
}
catch (SystemException $e){
if ($transactionStarted) {
$this->dbh->rollback();
}
$message['errors'][] = array('message'=>$e->getMessage().current($e->getCustomMessage()));
$JSONResponse = array_merge(array('result'=>false, 'header'=>$this->translate('TXT_SHIT_HAPPENS')), $message);
}
$this->response->write(json_encode($JSONResponse));
$this->response->commit();
}
/**
* ÐеÑеопÑеделеннÑй меÑод ÑоÑ
ÑанениÑ
* ÐÐ»Ñ Ñого ÑÑÐ¾Ð±Ñ ÑеализоваÑÑ ÑникалÑноÑÑÑ smap_default
*
* @param array
* @return void
* @access protected
*/
protected function saveData() {
if (!isset($_POST['right_id']) || !is_array($_POST['right_id'])) {
throw new SystemException('ERR_BAD_DATA', SystemException::ERR_CRITICAL);
}
if (isset($_POST[$this->getTableName()]['smap_default']) && $_POST[$this->getTableName()]['smap_default'] !== '0') {
$this->dbh->modify(QAL::UPDATE, $this->getTableName(), array('smap_default'=>null));
}
//ÐÑÑÑавлÑем ÑилÑÑÑ Ð´Ð»Ñ ÑодиÑелÑÑкого иденÑиÑикаÑоÑа
$PID = $_POST[$this->getTableName()]['smap_pid'];
if (empty($PID)) {
$PID = null;
}
$this->setFilter(array('smap_pid'=>$PID));
$result = parent::saveData();
$smapID = (is_int($result))?$result:current($this->getFilter());
$rights = $_POST['right_id'];
//УдалÑем вÑе пÑедÑдÑÑие запиÑи в ÑаблиÑе пÑав
$this->dbh->modify(QAL::DELETE , 'share_access_level', null, array('smap_id'=>$smapID));
foreach ($rights as $groupID => $rightID) {
if ($rightID != ACCESS_NONE) {
$this->dbh->modify(QAL::INSERT, 'share_access_level', array('smap_id'=>$smapID, 'right_id'=>$rightID, 'group_id'=>$groupID));
}
}
//ÐзменÑем smap_modified
$this->dbh->modify(QAL::UPDATE, $this->getTableName(), array('smap_modified' => date('Y-m-d H:i:s')), array('smap_id'=>$smapID));
if (($field = $this->getDataDescription()->getFieldDescriptionByName('tags')) && isset($_POST['tags']) && !empty($_POST['tags'])) {
$tags = $_POST['tags'];
$tags = explode(',', $tags);
$tags = array_map(create_function('$tag', 'return trim($tag);'), $tags);
$tagManager = TagManager::getInstance();
$tagManager->clearPageTags($smapID);
foreach ($tags as $tagName) {
if (!$tagManager->tagExists($tagName)) {
$tagManager->createTag($tagName);
}
$tagManager->addTagToPage($tagName, $smapID);
}
}
return $result;
}
/**
* ÐобавлÑем пеÑевод
*
* @return void
* @access protected
*/
protected function add() {
parent::add();
$this->addTranslation('MSG_START_EDITING');
}
/**
* Ðобавлен пеÑевод Ð´Ð»Ñ ÐºÐ¾ÑÐ½Ñ Ð´ÐµÑева Ñазделов
*
* @return void
* @access protected
*/
protected function main() {
parent::main();
$this->addTranslation('TXT_DIVISIONS');
}
/**
* Ðе позволÑÐµÑ ÑдалиÑÑ Ñаздел по ÑмолÑÐ°Ð½Ð¸Ñ Ð° Ñакже ÑиÑÑемнÑе ÑазделÑ
*
* @param int
* @return void
* @access protected
*/
protected function deleteData($id) {
$res = $this->dbh->select('share_sitemap', array('smap_is_system', 'smap_default', 'smap_pid'), array($this->getPK()=>$id));
if (!is_array($res))
throw new SystemException('ERR_DEV_BAD_DATA', SystemException::ERR_CRITICAL);
list($res) = $res;
if ($res['smap_is_system'] || $res['smap_default']) {
throw new SystemException('ERR_DEFAULT_OR_SYSTEM_DIVISION', SystemException::ERR_NOTICE );
}
$PID = $res['smap_pid'];
if (empty($PID)) {
$PID = null;
}
$this->setFilter(array('smap_pid'=>$PID));
parent::deleteData($id);
}
/**
* ÐÐ»Ñ Ð¼ÐµÑода setPageRights еÑли Ñаздел коÑоÑÑй ÑедакÑиÑÑеÑÑÑ - ÑиÑÑемнÑй Ñо дизейблÑÑÑÑ Ð²ÐºÐ»Ð°Ð´ÐºÐ¸ Ñ Ð¿Ñавами
* ÐÐ»Ñ Ð¼ÐµÑода show ÑлеÑаÑÑÑ Ð¸Ð¼ÐµÐ½Ð° Ñазделов
* ÐÐ»Ñ ÑоÑÐ¼Ñ ÑедакÑиÑÐ¾Ð²Ð°Ð½Ð¸Ñ Ð´ÐµÐ»Ð°ÐµÑÑÑ Ð½ÐµÐ°ÐºÑивнÑм пеÑеклÑÑаÑÐµÐ»Ñ smap_default
*
* @return DOMNode
* @access public
*/
public function build() {
switch ($this->getAction()) {
case 'showPageToolbar':
$result = false;
// вÑзÑваем ÑодиÑелÑÑкий меÑод поÑÑÑоениÑ
$result = Component::build();
$result->documentElement->appendChild($result->importNode($this->buildJS(), true));
$result->documentElement->appendChild($result->importNode($this->getToolBar()->build(), true));
break;
case 'showTemplate':
$result = $this->templateEditor->build();
break;
case 'showTransEditor':
$result = $this->transEditor->build();
break;
default:
if ($this->getType() == self::COMPONENT_TYPE_FORM_ALTER ) {
if (($field = $this->getData()->getFieldByName('smap_default')) && ($field->getRowData(0)=== true)) {
if ($fieldDescription = $this->getDataDescription()->getFieldDescriptionByName('smap_default')) {
$fieldDescription->setMode(FieldDescription::FIELD_MODE_READ);
}
}
}
$result = parent::build();
if ($this->getType() != self::COMPONENT_TYPE_LIST ) {
$result->documentElement->appendChild($this->buildRightsTab());
}
break;
}
return $result;
}
/**
* ÐеÑод возвÑаÑÐ°ÐµÑ ÑвойÑÑва Ñзла
*
* @return void
* @access protected
*/
protected function getDivisionName() {
try {
$id = $_POST['id'];
$langID = $_POST['languageID'];
if (!$this->recordExists($id)) {
throw new SystemException('ERR_404', SystemException::ERR_404);
}
$this->setFilter(array('smap_id'=>$id, 'lang_id'=>$langID));
$result = $this->dbh->select($this->getTranslationTableName(), array('smap_name'), $this->getFilter());
$JSONResponse = array(
'result'=>true,
'data'=>simplifyDBResult($result, 'smap_name', true)
);
}
catch (SystemException $e){
$JSONResponse = $this->generateError($e->getCode(), $e->getMessage());
}
$this->response->write(json_encode($JSONResponse));
$this->response->commit();
}
/**
* ÐÑÐ²Ð¾Ð´Ð¸Ñ Ð¿Ð°Ð½ÐµÐ»Ñ ÑпÑÐ°Ð²Ð»ÐµÐ½Ð¸Ñ ÑÑÑаниÑей
*
* @return void
* @access protected
*/
protected function showPageToolbar() {
if (!$this->config->getCurrentMethodConfig()) {
throw new SystemException('ERR_DEV_TOOLBAR_MUST_HAVE_CONFIG', SystemException::ERR_DEVELOPER);
}
$this->setToolbar($this->createToolbar());
}
/**
* СелекÑоÑ
*
* @return void
* @access protected
*/
protected function selector() {
$this->addTranslation('TXT_DIVISIONS');
$this->prepare();
}
/**
* ÐÑвод ÑелакÑоÑа Ñаблонов
*
* @return void
* @access protected
*/
protected function showTemplate() {
$this->request->setPathOffset($this->request->getPathOffset() + 1);
$this->templateEditor = $this->document->componentManager->createComponent('templateEditor', 'share', 'TemplateEditor', null);
$this->templateEditor->run();
}
/**
* ÐÑвод ÑедакÑоÑа пеÑеводов
*
* @return void
* @access protected
*/
protected function showTransEditor() {
$this->request->setPathOffset($this->request->getPathOffset() + 1);
$this->transEditor = $this->document->componentManager->createComponent('transEditor', 'share', 'TranslationEditor', null);
$this->transEditor->run();
}
/**
* ÐзменÑÐµÑ Ð¿Ð¾ÑÑдок ÑледованиÑ
*
* @param string
* @return JSON String
* @access protected
*/
protected function changeOrder($direction) {
try {
$id = $this->getActionParams();
list($id) = $id;
if (!$this->recordExists($id)) {
throw new SystemException('ERR_404', SystemException::ERR_404);
}
$order = $this->getOrder();
if ($direction == Grid::DIR_UP) {
$order[key($order)] = ($order[key($order)] == QAL::ASC)?QAL::DESC:QAL::ASC;
}
//ÐпÑеделÑем PID
$res = $this->dbh->select($this->getTableName(), array('smap_pid'), array('smap_id' => $id));
$PID = simplifyDBResult($res, 'smap_pid', true);
if (!is_null($PID)) {
$PID = ' = '.$PID;
}
else {
$PID = 'IS NULL';
}
$orderFieldName = key($order);
$request = sprintf('SELECT %s, %s
FROM %s
WHERE %s %s= (
SELECT %s
FROM %s
WHERE %s = %s )
AND smap_pid %s
%s
LIMIT 2 ',
$this->getPK(), $orderFieldName,
$this->getTableName(),
$orderFieldName, $direction,
$orderFieldName,
$this->getTableName(),
$this->getPK(), $id,
$PID,
$this->dbh->buildOrderCondition($order));
$result = $this->dbh->selectRequest($request);
if ($result === true || sizeof($result)<2) {
throw new SystemException('ERR_CANT_MOVE', SystemException::ERR_NOTICE);
}
$result = convertDBResult($result, $this->getPK(), true);
/**
* @todo ТÑÑ Ð½Ñжно ÑÑо Ñо поопÑималÑней пÑидÑмаÑÑ Ð´Ð»Ñ Ñого ÑÑÐ¾Ð±Ñ Ð¾ÑÑÑеÑÑвиÑÑ Ð¾Ð¿ÐµÑаÑÐ¸Ñ Ð¿ÐµÑеноÑа знаÑений Ð¼ÐµÐ¶Ð´Ñ Ð´Ð²ÑÐ¼Ñ ÑлеменÑами маÑÑива
* $a = $b;
* $b =$a;
*/
$keys = array_keys($result);
$data = array();
$c = $result[current($keys)];
$data[current($keys)] = $result[next($keys)];
$data[current($keys)] = $c;
foreach ($data as $id2 => $value) {
$order = $value['smap_order_num'];
$this->dbh->modify(QAL::UPDATE, $this->getTableName(), array($orderFieldName=>$order), array($this->getPK()=>$id2));
if ($id2 != $id) {
$result = $id2;
}
}
$JSONResponse = array(
'result' => true,
'nodeID' => $result,
'dir' => $direction
);
}
catch (SystemException $e){
$JSONResponse = $this->generateError($e->getCode(), $e->getMessage());
}
return json_encode($JSONResponse);
}
}