energine
[ class tree: energine ] [ index: energine ] [ all elements ]

Source for file TextBlock.class.php

Documentation is available at TextBlock.class.php

  1. <?php
  2. /**
  3.  * Содержит класс TextBlock.
  4.  *
  5.  * @package energine
  6.  * @subpackage share
  7.  * @author 1m.dm
  8.  * @copyright Energine 2006
  9.  * @version $Id$
  10.  */
  11.  
  12. //require_once('core/modules/share/components/DataSet.class.php');
  13.  
  14.  
  15. /**
  16.  * Текстовый блок.
  17.  *
  18.  * @package energine
  19.  * @subpackage share
  20.  * @author 1m.dm
  21.  * @final
  22.  */
  23. final class TextBlock extends DataSet {
  24.     /**
  25.      * Компонент менеджера изображений
  26.      *
  27.      * @var ImageManager 
  28.      * @access private
  29.      */
  30.     private $imageManager;
  31.  
  32.     /**
  33.      * Компонент библиотеки изображений
  34.      *
  35.      * @var ImageLibrary 
  36.      * @access private
  37.      */
  38.     private $imageLibrary;
  39.  
  40.     /**
  41.      * Компонент библиотеки файлов
  42.      *
  43.      * @var FileLibrary 
  44.      * @access private
  45.      */
  46.     private $fileLibrary;
  47.  
  48.     /**
  49.      * Имя основной таблицы
  50.      *
  51.      * @var string 
  52.      * @access private
  53.      */
  54.     private $tableName;
  55.  
  56.     /**
  57.      * Идентификатор текстового блока
  58.      *
  59.      * @var int 
  60.      * @access private
  61.      */
  62.     private $id = false;
  63.  
  64.     /**
  65.      * Содержимое текстового блока
  66.      *
  67.      * @var string 
  68.      * @access private
  69.      */
  70.     private $content = '';
  71.  
  72.     /**
  73.      * Находится ли страница в режиме редактирования содержимого
  74.      *
  75.      * @var boolean 
  76.      * @access private
  77.      */
  78.     private $isEditable;
  79.  
  80.     /**
  81.      * Конструктор класса
  82.      *
  83.      * @param string 
  84.      * @param string 
  85.      * @return void 
  86.      * @access public
  87.      */
  88.     public function __construct($name$moduleDocument $document,  array $params null{
  89.         parent::__construct($name$module$document,  $params);
  90.         /**
  91.          * @todo Не забыть убрать $_REQUEST или переделать чтобы для режима отладки  -_REQUEST а так  - _POST
  92.          *
  93.          */
  94.         $this->isEditable = $this->document->isEditable();
  95.         $this->tableName = 'share_textblocks';
  96.         if ($this->isEditable{
  97.             $this->addWYSIWYGTranslations();
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * Добавлен параметр num
  103.      *
  104.      * @return array 
  105.      * @access protected
  106.      */
  107.  
  108.     protected function defineParams({
  109.         return array_merge(
  110.         parent::defineParams(),
  111.         array(
  112.             'num' => 1,
  113.             'active' => true,
  114.         )
  115.         );
  116.     }
  117.  
  118.     /**
  119.      * Возвращает идентификатор текстового блока по переданному идентификатору документа и порядковому номеру
  120.      *
  121.      * @param int идентификатор документа
  122.      * @param string идентификатор текстового блока
  123.      * @return int 
  124.      * @access protected
  125.      */
  126.  
  127.     protected function getTextBlockID($smapID $num{
  128.         $smapID (empty($smapID))?null:$smapID;
  129.         $result false;
  130.         $res $this->dbh->select($this->tableNamearray('tb_id')array('smap_id'=>$smapID'tb_num'=>$num));
  131.         if (is_array($res)) {
  132.             $result simplifyDBResult($res'tb_id'true);
  133.         }
  134.         return $result;
  135.     }
  136.  
  137.     /**
  138.      * Загрузка данных
  139.      *
  140.      * @return void 
  141.      * @access protected
  142.      */
  143.     protected function main({
  144.         /**
  145.          * @todo Тут вообще получается ограничение, что num лейаутного текстового блока не должен быть цифрой
  146.          */
  147.  
  148.         if (intval($this->getParam('num'))!==0{
  149.             $docID $this->document->getID();
  150.         }
  151.         else {
  152.             $docID '';
  153.         }
  154.         
  155.         $res $this->dbh->selectRequest(
  156.             'SELECT st.tb_id as id, stt.tb_content as content '.
  157.             'FROM `share_textblocks`  st '.
  158.             'LEFT JOIN share_textblocks_translation stt ON st.tb_id = stt.tb_id and lang_id = %s '.
  159.             'WHERE smap_id '.(($docID)?' = '.$docID:' IS NULL ').' AND tb_num = %s ',
  160.             $this->document->getLang(),
  161.             $this->getParam('num')
  162.         );
  163.         
  164.         if(is_array($res)){
  165.             list($res$res;
  166.             $this->id $res['id'];
  167.             $this->content $res['content'];    
  168.         }
  169.  
  170.         //Если мы находимся в режиме редактирования содержимого
  171.         if ($this->isEditable{
  172.             //Отключаем тулбар страницы если есть
  173.             if ($component $this->document->componentManager->getComponentByName('pageToolBar')) {
  174.                 //$component->disable();
  175.             }
  176.  
  177.             //выставляем свойство указывающее на то что блок находится в режиме редактирования
  178.             $this->setProperty('editable''editable');
  179.         }
  180.  
  181.         $this->setProperty('num'$this->getParam('num'));
  182.         $this->prepare();
  183.     }
  184.  
  185.     /**
  186.      * Переопределен метод создания объекта мета данных
  187.      *
  188.      * @return DataDescription 
  189.      * @access protected
  190.      */
  191.  
  192.     protected function createDataDescription({
  193.         $dataDescr new DataDescription();
  194.         $fieldDescr new FieldDescription($this->getName());
  195.         $fieldDescr->setType(FieldDescription::FIELD_TYPE_HTML_BLOCK);
  196.         $dataDescr->addFieldDescription($fieldDescr);
  197.  
  198.         return $dataDescr;
  199.     }
  200.  
  201.     /**
  202.      * Создаем свои данные
  203.      *
  204.      * @return Data 
  205.      * @access protected
  206.      */
  207.  
  208.     protected function createData({
  209.         $data new Data();
  210.         $field new Field($this->getName());
  211.         $field->setData($this->getContent());
  212.         $data->addField($field);
  213.  
  214.         return $data;
  215.     }
  216.  
  217.     /**
  218.      * Возвращает содержимое текстового блока
  219.      *
  220.      * @return string 
  221.      * @access protected
  222.      */
  223.  
  224.     protected function getContent({
  225.         return $this->content;
  226.     }
  227.  
  228.     /**
  229.      * Возвращает идентификатор текстового блока
  230.      *
  231.      * @return int 
  232.      * @access protected
  233.      */
  234.  
  235.     protected function getID({
  236.         return $this->id;
  237.     }
  238.  
  239.     /**
  240.      * Создание панели инструментов
  241.      *
  242.      * @return void 
  243.      * @access protected
  244.      */
  245.  
  246.     protected function createToolbar({
  247.         return false;
  248.     }
  249.  
  250.     /**
  251.      * Строит JS описание
  252.      *
  253.      * @return DOMNode 
  254.      * @access protected
  255.      */
  256.  
  257.     protected function buildJS({
  258.         $result false;
  259.         if ($this->isEditable{
  260.             $result parent::buildJS();
  261.         }
  262.  
  263.         return $result;
  264.     }
  265.  
  266.     /**
  267.      * Сохранение данных
  268.      *
  269.      * @return void 
  270.      * @access protected
  271.      */
  272.  
  273.     protected function save({
  274.         $this->dbh->beginTransaction();
  275.         try {
  276.  
  277.             if (!isset($_POST['data']&& !isset($_POST['num'])) {
  278.                 throw new SystemException('ERR_DEV_NO_DATA'SystemException::ERR_DEVELOPER );
  279.             }
  280.             $langID $this->document->getLang();
  281.             $docID (isset($_POST['docID']))?$_POST['docID']:'';
  282.             //пытаемся определить есть ли у нас запись о содержимом блока в основной таблице
  283.  
  284.             $tbID $this->getTextBlockID($docID$_POST['num']);
  285.             $result DataSet::cleanupHTML($_POST['data']);
  286.             //$result = $_POST['data'];
  287.  
  288.             if (!$tbID{
  289.                 $tbID $this->dbh->modify(QAL::INSERT'share_textblocks'array('smap_id'=>$docID'tb_num'=>$_POST['num']));
  290.             }
  291.             $tableName $this->tableName.'_translation';
  292.  
  293.             $res $this->dbh->select($tableNamearray('tb_id')array('tb_id'=>$tbID'lang_id'=>$langID));
  294.             //если есть запись в таблице переводов - апдейтим
  295.             if (is_array($res)) {
  296.  
  297.                 $res $this->dbh->modify(QAL::UPDATE$tableNamearray('tb_content'=>$result)array('tb_id'=>$tbID'lang_id'=>$langID));
  298.             }
  299.             elseif ($res === true{
  300.                 //если нет - вставляем
  301.                 $res $this->dbh->modify(QAL::INSERT$tableNamearray('tb_content'=>$result'tb_id'=>$tbID'lang_id'=>$langID));
  302.             }
  303.  
  304.             $this->dbh->commit();
  305.         }
  306.         catch (Exception $e{
  307.             $this->dbh->rollback();
  308.             $result $e->getMessage();
  309.         }
  310.  
  311.         $this->response->setHeader('Content-Type''application/xml; charset=utf-8');
  312.         $this->response->write($result);
  313.         $this->response->commit();
  314.     }
  315.  
  316.     protected function source({
  317.         $this->source = $this->document->componentManager->createComponent('textblocksource''share''TextBlockSource'null);
  318.         //$this->source->getAction();
  319.         $this->source->run();
  320.     }
  321.  
  322.     /**
  323.      * Выводит компонент менеджер изображений
  324.      *
  325.      * @return void 
  326.      * @access protected
  327.      */
  328.     protected function imageManager({
  329.         $this->imageManager  = $this->document->componentManager->createComponent('imagemanager''image''ImageManager'null);
  330.         //$this->imageManager->getAction();
  331.         $this->imageManager->run();
  332.     }
  333.  
  334.     /**
  335.      * Выводит компонент библиотека изображений
  336.      *
  337.      * @return void 
  338.      * @access protected
  339.      */
  340.     protected function imageLibrary({
  341.         $this->request->setPathOffset($this->request->getPathOffset(1);
  342.         $this->imageLibrary = $this->document->componentManager->createComponent('imagelibrary''image''ImageLibrary'null);
  343.         //$this->imageLibrary->getAction();
  344.         $this->imageLibrary->run();
  345.     }
  346.  
  347.     /**
  348.      * Выводит компонент библиотеки файлов
  349.      *
  350.      * @return void 
  351.      * @access protected
  352.      */
  353.      protected function fileLibrary({
  354.         $this->request->setPathOffset($this->request->getPathOffset(1);
  355.         $this->fileLibrary = $this->document->componentManager->createComponent('filelibrary''share''FileLibrary'nullfalse);
  356.         //$this->fileLibrary->getAction();
  357.         $this->fileLibrary->run();
  358.      }
  359.  
  360.     /**
  361.      * Для метода вывода редактора изображений вызывает построитель редактора изоборажений во всех других случаях - свой
  362.      *
  363.      * @return DOMNode 
  364.      * @access public
  365.      */
  366.  
  367.     public function build({
  368.         switch ($this->getAction()) {
  369.             case 'imageManager':
  370.                 $result $this->imageManager->build();
  371.                 break;
  372.             case 'imageLibrary':
  373.                 $result $this->imageLibrary->build();
  374.                 break;
  375.             case 'fileLibrary':
  376.                 $result $this->fileLibrary->build();
  377.                 break;
  378.             case 'source':
  379.                 $result $this->source->build();
  380.                 break;
  381.             default:
  382.                 $result parent::build();
  383.                 break;
  384.         }
  385.         return $result;
  386.     }
  387.  
  388. }
В создании документации нам помог: phpDocumentor