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

Source for file JSONBuilder.class.php

Documentation is available at JSONBuilder.class.php

  1. <?php
  2.  
  3. /**
  4.  * Класс JSONBuilder
  5.  *
  6.  * @package energine
  7.  * @subpackage core
  8.  * @author dr.Pavka
  9.  * @copyright Energine 2006
  10.  * @version $Id$
  11.  */
  12.  
  13. //require_once('core/framework/Builder.class.php');
  14.  
  15. /**
  16.  * Построитель данных в формат JSON (JavaScript Object Notation).
  17.  *
  18.  * @package energine
  19.  * @subpackage core
  20.  * @author dr.Pavka
  21.  */
  22. class JSONBuilder extends Builder {
  23.     /**
  24.      * Листалка
  25.      *
  26.      * @var Pager 
  27.      * @access private
  28.      */
  29.     private $pager = null;
  30.  
  31.     /**
  32.      * @access private
  33.      * @var array список ошибок
  34.      * @todo зачем это!?
  35.      */
  36.     private $errors = array();
  37.  
  38.     /**
  39.      * Конструктор класса.
  40.      *
  41.      * @access public
  42.      * @return void 
  43.      */
  44.     public function __construct({
  45.         parent::__construct();
  46.     }
  47.  
  48.     /**
  49.      * Создает результирующий JSON-объект.
  50.      *
  51.      * @access public
  52.      * @return bool 
  53.      */
  54.     public function build({
  55.         $result false;
  56.  
  57.         if ($this->dataDescription == false{
  58.             throw new SystemException('ERR_DEV_NO_DATA_DESCRIPTION'SystemException::ERR_DEVELOPER);
  59.         }
  60.  
  61.         $fields $this->dataDescription->getFieldDescriptions();
  62.         foreach ($fields as $fieldName => $fieldInfo{
  63.             $result['meta'][$fieldNamearray(
  64.                 'title' => $fieldInfo->getPropertyValue('title'),
  65.                 'type' => $fieldInfo->getType(),
  66.                 'key' => $fieldInfo->getPropertyValue('key')&&$fieldInfo->getPropertyValue('index')=='PRI'truefalse,
  67.                 'visible' => $fieldInfo->getPropertyValue('key')&&$fieldInfo->getPropertyValue('index')=='PRI' false true,
  68.                 'name' => $fieldInfo->getPropertyValue('tableName')."[$fieldName]",
  69.                 'rights' => $fieldInfo->getRights(),
  70.                 'field' => $fieldName
  71.             );
  72.         }
  73.  
  74.         if (!$this->data->isEmpty()) {
  75.             for ($i 0$i $this->data->getRowCount()$i++{
  76.                 foreach ($fields as $fieldName => $fieldInfo{
  77.                     $fieldType $fieldInfo->getType();
  78.                     $fieldValue $this->data->getFieldByName($fieldName)->getRowData($i);
  79.                     switch ($fieldType{
  80.                         case FieldDescription::FIELD_TYPE_DATETIME:
  81.                         case FieldDescription::FIELD_TYPE_DATE:
  82.                             if (!empty($fieldValue)) {
  83.                                 $fieldValue strftime($fieldInfo->getPropertyValue('outputFormat')$fieldValue);
  84.                             }
  85.                             break;
  86.                         case FieldDescription::FIELD_TYPE_IMAGE:
  87.                             if ($fieldValue{
  88.                                 if(file_exists(dirname($fieldValue).'/.'.basename($fieldValue)))
  89.                                     $fieldValue dirname($fieldValue).'/.'.basename($fieldValue);    
  90.                             }
  91.                             break;    
  92.                         case FieldDescription::FIELD_TYPE_SELECT:
  93.                             $value $fieldInfo->getAvailableValues();
  94.                             if (isset($value[$fieldValue])) {
  95.                                 $fieldValue $value[$fieldValue]['value'];
  96.                             }
  97.                             break;
  98.                         default// not used
  99.                     }
  100.                     if (is_null($fieldValue)) {
  101.                         $fieldValue '';
  102.                     }
  103.                     $result['data'][$i][$fieldName$fieldValue;
  104.                 }
  105.             }
  106.         }
  107.  
  108.         $result['result'true;
  109.         $result['mode''select';
  110.  
  111.         $this->result = $result;
  112.  
  113.         return true;
  114.     }
  115.  
  116.     /**
  117.      * Возвращает результат работы построителя.
  118.      *
  119.      * @access public
  120.      * @return string 
  121.      */
  122.     public function getResult({
  123.         $result $this->result;
  124.         if (!is_null($this->pager)) {
  125.             $result['pager'array(
  126.                'current' => $this->pager->getCurrentPage(),
  127.                'count' => $this->pager->getNumPages()
  128.             );
  129.         }
  130.         $result json_encode($result);
  131.         return $result;
  132.     }
  133.  
  134.     /**
  135.      * Возвращает список ошибок.
  136.      *
  137.      * @return string 
  138.      * @access public
  139.      * @todo зачем это!?
  140.      */
  141.     public function getErrors({
  142.         return json_encode($this->errors);
  143.     }
  144.  
  145.     /**
  146.      * Устанавливает кооличество страниц для листлки
  147.      *
  148.      * @param int 
  149.      * @return void 
  150.      * @access public
  151.      */
  152.  
  153.     public function setPager($pager{
  154.         $this->pager = $pager;
  155.     }
  156. }
В создании документации нам помог: phpDocumentor