Amiro.CMS API Reference
Amiro.Ru / Manual
Loading search...

AJAX Responder Plugin.

This is an example of specblock that uses JS API and CMS API to display item lists for several modules.

Attention!

  1. All Plugin_AJAXResponder package files are the part of CMS and possible can be overwritten in the future. Copy «_local/plugins_distr/ami_ajax_responder» folder to your own to create new plugin.
  2. This plugin allows access to CMS modules data escaping common CMS access validation. Be careful allowing modules access in plugin admin page.

Client side generated by common CMS plugin having the entry point at plg_ami_ajax_responder.php .
Server side has the entry point at ami_resp.php (front AJAX request common entry point), that passes control to the plugin script ami_server.php.

Then a model of selected module in plugin script creates array of data in format:
    array(
        'list' => array(
            // Numberic array of data for every row
        )
    )
Plugin creates new array with ("data" => module array) and return the result array to CMS Responce. Responce converts this to JSON string and sends to client.

There are two ways to use AJAX Responder Plugin:

  1. Clone plugin to another and overwrite reuiqred methods.
  2. Use existing plugin and add your own modules if necessary.


How to clone and develop AJAX Responder based plugin

Let the plugin has «competition_winners» id.
Let it has simple database table storing names and places of the winners if some competition.
  1. Copy «_local/plugins_distr/ami_ajax_responder» folder to «_local/plugins_distr/competition_winners»;
  2. Replace «ami_ajax_responder» substring by «competition_winners» in file «_local/plugins_distr/competition_winners/config.php»;
  3. Create «_local/plugins_distr/competition_winners/database» folder and place install/uninstall sql-files, i. e.:
    install.sql:
    DROP TABLE IF EXISTS `plg_competition_winners`;
    CREATE TABLE `plg_competition_winners` (
      `id` int(11) NOT NULL auto_increment,
      `name` varchar(255) NOT NULL default '',
      `place` int NOT NULL default 0,
      PRIMARY KEY  (`id`),
      UNIQUE `i_name` (`name`)
    ) ENGINE=MyISAM;
    INSERT INTO `plg_competition_winners`(`name`,`place`) VALUES ('Tori Amos','2');
    INSERT INTO `plg_competition_winners`(`name`,`place`) VALUES ('Suzanne Vega','3');
    INSERT INTO `plg_competition_winners`(`name`,`place`) VALUES ('Makhar Guschin','1');
    
    uninstall.sql:
    DROP TABLE IF EXISTS `plg_competition_winners`;
  4. Modify locale files in «_local/plugins_distr/competition_winners/i18n» folder;
  5. Create own module table model in «_local/plugins_distr/competition_winners/code». Model table models has an beta version so the best way is to create files having only following content:
    CompetitionWinners_Table.php:
    class CompetitionWinners_Table extends AMI_ModTable{
        protected $tableName = 'plg_competition_winners';
    }
    CompetitionWinners_TableItem.php:
    class CompetitionWinners_TableItem extends AMI_ModTableItem{
    }
    CompetitionWinners_TableList.php:
    class CompetitionWinners_TableList extends AMI_ModTableList{
    }
  6. Create own plugin module model and view in «_local/plugins_distr/competition_winners/code»:
    PlgAJAXResponder_CompetitionWinners.php:
    // Plugin module model
    class PlgAJAXResponder_CompetitionWinners_State implements PlgAJAXResponder_iState{
        public function getDateFieldName(){
            // We have no date field
            return '';
        }
    
        public function useCategories(){
            // We have no categories table
            return false;
        }
    }
    
    class PlgAJAXResponder_CompetitionWinners_ListView extends PlgAJAXResponder_ListView{
        protected function getColumns(){
            $aCols = array(
                'place', 'name'
            );
            return $aCols + parent::getColumns();
        }
    }
  7. Replace option 'order' value in file «_local/plugins_distr/competition_winners/options/options.php» by 'i.place';
  8. Replace rule 'order' value in file «_local/plugins_distr/competition_winners/options/rules.php» by 'i.place';
  9. Set $aAllowedModules array to ('competition_winners') in file «_local/plugins_distr/competition_winners/code/ami_server.php»:
    $aAllowedModules = array('competition_winners');
  10. Set class/resource mapping in file «_local/plugins_distr/competition_winners/code/ami_server.php»:
    AMI::addClassMapping(array(
        'PlgAJAXResponder_CompetitionWinners_State'    => 'PlgAJAXResponder_CompetitionWinners',
        'PlgAJAXResponder_CompetitionWinners_ListView' => 'PlgAJAXResponder_CompetitionWinners'
    ));
    
    AMI::addResourceMapping(array(
        'plg_ajax_resp/competition_winners/state/model' => 'PlgAJAXResponder_CompetitionWinners_State',
        'plg_ajax_resp/competition_winners/list/view'   => 'PlgAJAXResponder_CompetitionWinners_ListView',
    ));
  11. Create in «_local/plugins_distr/competition_winners/templates/front.tpl» new set «render_row(module=competition_winners)» filled width:
    <div class="ami_resp_row_##module##">
        <div>
            @@if(ext_img_small != "")@@<img src="@@ext_img_small@@" alt="" />@@endif@@
            @@if(fdate != "")@@<div class="ami_resp_row_fdate_##module##">@@fdate@@</div>@@endif@@
            @@if(cat_header != "")@@<div class="ami_resp_row_cat_header_##module##">@@cat_header@@</div>@@endif@@
            <div class="ami_resp_row_header_##module##"><a href="@@url@@">@@header@@</a></div>
        </div>
        <div class="ami_resp_row_announce_##module##">@@announce@@</div>
        <hr>
    </div>
  12. Install plugin and add specblock in Site Manager.



How to add your own module to plugin

  1. In the folders «_local/plugins_distr/ami_ajax_responder/code» copy and rename any of PlgAJAXResp_*.php file.
  2. Create own module table model and plugin module model (points 5, 6 and 10 in clone description).
  3. Add to $aAllowedModules array in file «_local/plugins_distr/ami_ajax_responder/code/ami_server.php» your module name.
  4. If required create your controller that overrides standard functionality of plugin controller, e.g. for SearchHistory:
    class PlgAJAXResp_SearchHistory extends PlgAJAXResp{
        // Overriden methods here
    }
    Then add controller resource "plg_ajax_resp/controller" to your module resources in «_local/plugins_distr/ami_ajax_responder/code/ami_server.php».

You can see PlgAJAXResp_SearchHistory as example of custom module for AJAX responder plugin. This module works with two models, builds custom queries and creates the own responce for JavaScript AMI.UI.Suggestion object.

• Interfaces:

Model
PlgAJAXResp_iState - Plugin module model (server-side plugin context).

• Classes:

Controller
PlgAJAXResp - AJAX Responder plugin JSON generation class.
PlgAJAXResp_SearchHistory - AJAX Responder SearchHistory plugin JSON generation class.
Model
PlgAJAXResp_Articles_State - Plugin Articles module model (server-side plugin context).
PlgAJAXResp_Blog_State - Plugin Blog module model (server-side plugin context).
PlgAJAXResp_EshopItem_State - Plugin E-shop Product module model (server-side plugin context).
PlgAJAXResp_Files_State - Plugin Files module model (server-side plugin context).
PlgAJAXResp_KbItem_State - Plugin Knowledge Base module model (server-side plugin context).
PlgAJAXResp_News_State - Plugin News module model (server-side plugin context).
PlgAJAXResp_Photoalbum_State - Plugin Photo Gallery module model (server-side plugin context).
PlgAJAXResp_PortfolioItem_State - Plugin PortfolioItem module model (server-side plugin context).
PlgAJAXResp_SearchHistory_State - Plugin SearchHistory module model (server-side plugin context).
PlgAJAXResp_Search_State - Plugin Search module model (server-side plugin context).
PlgAJAXResp_Stickers_State - Plugin Stickers module model (server-side plugin context).
Mixed
PlgAJAXRespAdmin - Admin side script displaing select box with available modules (CMS context).
View
PlgAJAXRespFront - Plugin front side (CMS context).
PlgAJAXResp_Articles_ListView - Plugin Articles module list view (server-side plugin context).
PlgAJAXResp_Blog_ListView - Plugin Blog module list view (server-side plugin context).
PlgAJAXResp_EshopItem_ListView - Plugin E-shop Product module list view (server-side plugin context).
PlgAJAXResp_Files_ListView - Plugin Files module list view (server-side plugin context).
PlgAJAXResp_KbItem_ListView - Plugin Knowledge Base module list view (server-side plugin context).
PlgAJAXResp_ListView - Plugin list view.
PlgAJAXResp_ListViewLinks - Plugin list view with front links generation.
PlgAJAXResp_News_ListView - Plugin News module list view (server-side plugin context).
PlgAJAXResp_Photoalbum_ListView - Plugin Photo Gallery module list view (server-side plugin context).
PlgAJAXResp_PortfolioItem_ListView - Plugin PortfolioItem module list view (server-side plugin context).
PlgAJAXResp_SearchHistory_ListView - Plugin SearchHistory module list view (server-side plugin context).
PlgAJAXResp_Search_ListView - Plugin Search module list view (server-side plugin context).
PlgAJAXResp_Stickers_ListView - Plugin Stickers module list view (server-side plugin context).

• Files: