Amiro.CMS API Reference [beta] (available from v5.10) Amiro.Ru / Manual

Source for file PlgAJAXResp.php

Documentation is available at PlgAJAXResp.php

  1. <?php
  2. /**
  3.  * @copyright  Amiro.CMS. All rights reserved.
  4.  * @category   Plugin
  5.  * @package    Plugin_AJAXResponder
  6.  * @since      5.10.0
  7.  * @filesource
  8.  */
  9.  
  10. /**
  11.  * Plugin module model (server-side plugin context).
  12.  *
  13.  * @package    Plugin_AJAXResponder
  14.  * @subpackage Model
  15.  */
  16. interface PlgAJAXResp_iState{
  17.     /**
  18.      * Returns item date field name or empty string
  19.      *
  20.      * @return string 
  21.      * @see    PlgAJAXResp::initModel()
  22.      */
  23.     public function getDateFieldName();
  24. }
  25.  
  26. /**
  27.  * AJAX Responder plugin JSON generation class.
  28.  *
  29.  * @package    Plugin_AJAXResponder
  30.  * @subpackage Controller
  31.  */
  32. class PlgAJAXResp{
  33.     /**
  34.      * Maximum list items limit to avoid memory/cpu overload during getting large lists
  35.      */
  36.     const MAX_LIST_ITEMS_LIMIT 20;
  37.  
  38.     /**
  39.      * Current plugin module id
  40.      *
  41.      * @var string 
  42.      */
  43.     protected $modId;
  44.  
  45.     /**
  46.      * @var AMI_Request 
  47.      */
  48.     protected $oRequest;
  49.  
  50.     /**
  51.      * Plugin module view
  52.      *
  53.      * @var PlgAJAXResp_ListView 
  54.      */
  55.     protected $oView;
  56.  
  57.     /**
  58.      * Module table model
  59.      *
  60.      * @var AMI_ModTable 
  61.      */
  62.     private $oTableModel;
  63.  
  64.     /**
  65.      * Plugin module state
  66.      *
  67.      * @var PlgAJAXResp_iState 
  68.      */
  69.     private $oState;
  70.  
  71.     /**
  72.      * Filter by page id flag
  73.      *
  74.      * @var bool 
  75.      */
  76.     private $doFilterByPageId;
  77.  
  78.     /**
  79.      * Constructor
  80.      *
  81.      * @param AMI_Request $oRequest 
  82.      * @param array $aAllowedModules 
  83.      */
  84.     public function __construct(AMI_Request $oRequestarray $aAllowedModules){
  85.         AMI::getSingleton('response')->displayBench();
  86.         AMI::getSingleton('db')->displayQueries(true);
  87.  
  88.         $this->oRequest $oRequest;
  89.         $this->modId $this->oRequest->get('module');
  90.         if(!$this->modId){
  91.             trigger_error("Missing request parameter 'module'"E_USER_ERROR);
  92.         }
  93.         if(!in_array($this->modId$aAllowedModules)){
  94.              trigger_error("Disallowed module id '" $this->modId "'"E_USER_ERROR);
  95.         }
  96.         $limit = (int)abs($this->oRequest->get('limit'3));
  97.         if(!$limit){
  98.             $limit 3;
  99.         }
  100.         if($limit self::MAX_LIST_ITEMS_LIMIT){
  101.             $limit self::MAX_LIST_ITEMS_LIMIT;
  102.         }
  103.         $this->oRequest->set('order'$this->oRequest->get('order''id'));
  104.         $this->oRequest->set('limit'$limit);
  105.         $this->oRequest->set('offset'(int)abs($this->oRequest->get('offset'0)));
  106.         $this->oRequest->set('locale'$this->oRequest->get('locale''en'));
  107.         $this->oRequest->set('id_site'(int)abs($this->oRequest->get('id_site'0)));
  108.  
  109.         $pageId $this->oRequest->get('id_page''');
  110.         if($pageId == 'common'){
  111.             $this->doFilterByPageId true;
  112.             $this->oRequest->set('id_page'0);
  113.         }else{
  114.             $pageId = (int)abs($pageId);
  115.             $this->doFilterByPageId = (bool)$pageId;
  116.             $this->oRequest->set('id_page'$pageId);
  117.         }
  118.  
  119.         if(
  120.             !$this->oRequest->get('limit'|| !is_numeric($this->oRequest->get('limit')) || (
  121.                 $this->oRequest->get('dir'!= 'A' &&
  122.                 $this->oRequest->get('dir'!= 'D' &&
  123.                 $this->oRequest->get('dir'!= ''
  124.             || !preg_match('~^[a-z]{2,3}$~'$this->oRequest->get('locale'))
  125.         ){
  126.              trigger_error("Invalid request parameter"E_USER_ERROR);
  127.         }
  128.  
  129.         $this->initStateAndView();
  130.  
  131.         // Set initialized model
  132.         $oModelList $this->initModel();
  133.         $this->oView->setModel($oModelList);
  134.     }
  135.  
  136.     /**
  137.      * Initializes table model and returns list model
  138.      *
  139.      * @return AMI_ModTableList 
  140.      */
  141.     protected function initStateAndView(){
  142.         // Module state (plugin module model)
  143.         $this->oState AMI::getResourceModel('plg_ajax_resp/' $this->modId '/state');
  144.  
  145.         // Setting for Image extension to get generated images {
  146.  
  147.         AMI::setOption($this->modId'generate_pictures'array('picture''popup_picture''small_picture'));
  148.         AMI::setOption($this->modId'prior_source_picture''popup_picture');
  149.         AMI::setOption($this->modId'picture_maxwidth'300);
  150.         AMI::setOption($this->modId'picture_maxheight'300);
  151.         AMI::setOption($this->modId'small_picture_maxwidth'80);
  152.         AMI::setOption($this->modId'small_picture_maxheight'80);
  153.         AMI::setOption($this->modId'popup_picture_maxwidth'800);
  154.         AMI::setOption($this->modId'popup_picture_maxheight'600);
  155.         AMI::setOption($this->modId'generate_bigger_image'true);
  156.  
  157.         // } Setting for Image extension to get generated images
  158.  
  159.         // Will be described later
  160.         AMI::initModExtensions($this->modId);
  161.         // Module view (server-side plugin context)
  162.         $this->oView AMI::getResource('plg_ajax_resp/' $this->modId '/list/view');
  163.     }
  164.  
  165.     /**
  166.      * Initializes table model and returns list model
  167.      *
  168.      * @return AMI_ModTableList 
  169.      */
  170.     protected function initModel(){
  171.         // Module table model
  172.         $this->oTableModel AMI::getResourceModel($this->modId '/table');
  173.  
  174.         /**
  175.          * Item list model
  176.          *
  177.          * @var AMI_ModTableList
  178.          */
  179.         $oModelList $this->oTableModel->getList();
  180.  
  181.         $prefix $oModelList->getMainTableAlias(true);
  182.  
  183.         // Common front filtering {
  184.  
  185.         if($this->oTableModel->hasField('public')){
  186.             $oModelList->addWhereDef('AND ' $prefix $this->oTableModel->getFieldName('public'' = 1');
  187.             if(AMI::getOption($this->modId'use_categories')){
  188.                 $oModelList->addWhereDef('AND cat.public = 1');
  189.             }
  190.         }
  191.         if($this->oTableModel->hasField('hide_in_list')){
  192.             $oModelList->addWhereDef('AND ' $prefix $this->oTableModel->getFieldName('hide_in_list'' = 0');
  193.         }
  194.         if($this->doFilterByPageId){
  195.             $oModelList->addWhereDef(
  196.                 DB_Query::getSnippet("AND %s = %s")
  197.                     ->plain($prefix $this->oTableModel->getFieldName('id_page'))
  198.                     ->q($this->oRequest->get('id_page'))
  199.             );
  200.         }
  201.  
  202.         // } Common front filtering
  203.         // Date field {
  204.  
  205.         $dateFieldName $this->oState->getDateFieldName();
  206.         if($dateFieldName !== '' && $this->oTableModel->hasField($dateFieldName)){
  207.             $oModelList->addExpressionColumn(
  208.                 'fdate',
  209.                 DB_Query::getSnippet("DATE_FORMAT(%s, %s)")
  210.                     ->plain($this->oTableModel->getFieldName($dateFieldName$prefix))
  211.                     ->q(AMI::getDateFormat($this->oRequest->get('locale')'DB_DATE'))
  212.             );
  213.             $oModelList->addExpressionColumn(
  214.                 'ftime',
  215.                 DB_Query::getSnippet("DATE_FORMAT(%s, %s)")
  216.                     ->plain($this->oTableModel->getFieldName($dateFieldName$prefix))
  217.                     ->q(AMI::getDateFormat($this->oRequest->get('locale')'DB_TIME'))
  218.  
  219.             );
  220.         }
  221.  
  222.         // } Date field
  223.         // { Locale
  224.  
  225.         if($this->oTableModel->hasField('lang')){
  226.             $oModelList->addWhereDef(
  227.                 DB_Query::getSnippet("AND %s = %s")
  228.                     ->plain($this->oTableModel->getFieldName('lang'$prefix))
  229.                     ->q($this->oRequest->get('locale'))
  230.             );
  231.         }
  232.  
  233.         // } Locale
  234.  
  235.         $aDirMapping array('A' => 'ASC''D' => 'DESC');
  236.         $dir $this->oRequest->get('dir''');
  237.         if(isset($aDirMapping[$dir])){
  238.             $dir $aDirMapping[$dir];
  239.         }
  240.         $oModelList->addOrder(mb_strtolower($this->oRequest->get('order'))$dir);
  241.  
  242.         $oModelList->setLimitParameters(
  243.             $this->oRequest->get('offset'),
  244.             $this->oRequest->get('limit')
  245.         );
  246.  
  247.         return $oModelList;
  248.     }
  249.  
  250.     /**
  251.      * Returns list data
  252.      *
  253.      * @return array 
  254.      * @see    PlgAJAXResp_ListView::get()
  255.      */
  256.     public function getResponse(){
  257.         $aData array(
  258.             'list' => $this->oView->get()
  259.         );
  260.         return $aData;
  261.     }
  262. }

Documentation generated on Wed, 21 Dec 2011 10:42:20 +0600 by