Source for file Actions.php

Documentation is available at Actions.php

  1. <?php
  2.     /**
  3.      * Standard Action classes
  4.      * @copyright Copyright (c) 2006,2007 Mirchev Ideas Ltd. All rights reserved.
  5.      * @package MIPHPF
  6.      */
  7.     
  8.     /**
  9.      * Actions base class
  10.      * @copyright Copyright (c) 2006,2007 Mirchev Ideas Ltd. All rights reserved.
  11.      * @package MIPHPF
  12.      */
  13.     class miAction {
  14.         protected $_view;
  15.         
  16.         /**
  17.          * Construct the action object
  18.          * 
  19.          * @param miView $view 
  20.          */
  21.         public function __construct(miView $view)
  22.         {
  23.             $this->_view = $view;
  24.         }
  25.         
  26.         /**
  27.          * Performs the action
  28.          * 
  29.          * @access public
  30.          * @return boolean true on success, false if an error occured
  31.          */
  32.         public function doAction()
  33.         {
  34.         }
  35.     }
  36.     
  37.     /**
  38.      * Action class with support for miWebForm
  39.      * @copyright Copyright (c) 2006,2007 Mirchev Ideas Ltd. All rights reserved.
  40.      * @package MIPHPF
  41.      */
  42.     class miActionWithWebForm extends miAction {
  43.         protected $_webForm = null;
  44.         
  45.         /**
  46.          * Returns a web form object
  47.          * 
  48.          * @access public
  49.          * @return miWebForm 
  50.          */
  51.         public function getWebForm()
  52.         {
  53.             if ($this->_webForm == null) {
  54.                 $this->_webForm = new miWebForm(array());
  55.                 $this->_view->getViewMapperController()->setWebForm($this->_webForm);
  56.                 $this->_view->initWebForm($this->_webForm);
  57.             }
  58.             return $this->_webForm;
  59.         }
  60.         
  61.         /**
  62.          * Show the web form
  63.          * 
  64.          * @access protected
  65.          * @param miWebForm $form the webform object
  66.          * @param string $templateName the template name
  67.          * @param boolean $isEditable if the form will contain the editable fields/widgets
  68.          */
  69.         protected function showForm(&$form, $templateName, $isEditable)
  70.         {
  71.             $table = $this->_view->getTable();
  72.             if ($table) {
  73.                 $params = $table->getFeatureValues();
  74.                 $this->_view->addMainPageElements(miTable::escapeArray($params));
  75.             }
  76.             
  77.             $form->addMainPageElements($this->_view->getMainPageElements());
  78.             
  79.             $html = $form->parse($templateName, $isEditable);
  80.             $cmd = new miControllerCommand(miControllerCommand::CONTROLLER_COMMAND_HTML, $html);
  81.             $this->_view->addControllerCommand($cmd);
  82.         }
  83.     }
  84.     
  85.     /**
  86.      * The view action
  87.      * @copyright Copyright (c) 2006,2007 Mirchev Ideas Ltd. All rights reserved.
  88.      * @package MIPHPF
  89.      */
  90.     class miViewAction extends miActionWithWebForm {
  91.         public function doAction()
  92.         {
  93.             $vmController = $this->_view->getViewMapperController();
  94.             $vmController->read();
  95.             
  96.             $this->_view->callPlugin($this, 'preShowForm');
  97.             
  98.             $form = $this->getWebForm();
  99.             $vmController->setDataToWebForm();
  100.             $this->showForm($form, $this->_view->getTemplateFileName(miDefaultView::TEMPLATE_ID_VIEW), false);
  101.             return true;
  102.         }
  103.     }
  104.     
  105.     /**
  106.      * Show the page for creating a new record in the database
  107.      * @copyright Copyright (c) 2006,2007 Mirchev Ideas Ltd. All rights reserved.
  108.      * @package MIPHPF
  109.      */
  110.     class miCreateAction extends miActionWithWebForm {
  111.         public function doAction()
  112.         {
  113.             $this->_view->callPlugin($this, 'preShowForm');
  114.             
  115.             $form = $this->getWebForm();
  116.             $this->showForm($form, $this->_view->getTemplateFileName(miDefaultView::TEMPLATE_ID_CREATE), true);
  117.             return true;
  118.         }
  119.     }
  120.     
  121.     /**
  122.      * Takes the values from the user input and create a new record in the database
  123.      * @copyright Copyright (c) 2006 Mirchev Ideas Ltd. All rights reserved.
  124.      * @package MIPHPF
  125.      */
  126.     class miExecCreateAction extends miCreateAction {
  127.         public function doAction()
  128.         {
  129.             try {
  130.                 $form = $this->getWebForm();
  131.                 $vmController = $this->_view->getViewMapperController();
  132.                 
  133.                 $isOk = $form->processSubmit();
  134.                 $vmController->setDataFromWebForm();
  135.                 
  136.                 if ($isOk === false) {
  137.                     $vmController->updateSubmittedForm($form);
  138.                     parent::doAction();    // Call miCreateAction
  139.                     return false;
  140.                 }
  141.                 
  142.                 $this->_view->callPlugin($this, 'preCreate');
  143.                 $recordId = $vmController->insert();
  144.                 $this->_view->callPlugin($this, 'postCreate');
  145.                 
  146.             } catch (miException $exception) {
  147.                 $msg = $this->_view->getMessage('MI_RECORD_CREATE_FAILED_MSG');
  148.                 $this->_view->addRedirectToListControllerCommand($msg . $exception->getMessage(), miMessage::MSG_TYPE_ERROR);
  149.                 return false;
  150.             }
  151.             $msg = sprintf($this->_view->getMessage('MI_RECORD_CREATED_SUCCESSFULLY_MSG'), $recordId);
  152.             $this->_view->addRedirectToListControllerCommand($msg, miMessage::MSG_TYPE_INFO);
  153.             return true;
  154.         }
  155.     }
  156.     
  157.     /**
  158.      * Show the page for updating a record in the database
  159.      * @copyright Copyright (c) 2006,2007 Mirchev Ideas Ltd. All rights reserved.
  160.      * @package MIPHPF
  161.      */
  162.     class miEditAction extends miActionWithWebForm {
  163.         public function doAction()
  164.         {
  165.             try {
  166.                 $vmController = $this->_view->getViewMapperController();
  167.                 $vmController->read();
  168.                 
  169.                 $this->_view->callPlugin($this, 'preShowForm');
  170.                 
  171.                 $form = $this->getWebForm();
  172.                 $vmController->setDataToWebForm();
  173.                 $this->showForm($form, $this->_view->getTemplateFileName(miDefaultView::TEMPLATE_ID_EDIT), true);
  174.                 return true;
  175.             } catch (miException $exception) {
  176.                 $msg = $this->_view->getMessage('MI_RECORD_EDIT_FAILED_MSG');
  177.                 $this->_view->addRedirectToListControllerCommand($msg . $exception->getMessage(), miMessage::MSG_TYPE_ERROR);
  178.                 return false;
  179.             }
  180.         }
  181.     }
  182.     
  183.     /**
  184.      * Takes the values from the user input and update a record in the database
  185.      * @copyright Copyright (c) 2006,2007 Mirchev Ideas Ltd. All rights reserved.
  186.      * @package MIPHPF
  187.      */
  188.     class miExecEditAction extends miEditAction {
  189.         public function doAction()
  190.         {
  191.             try {
  192.                 $vmController = $this->_view->getViewMapperController();
  193.                 $vmController->read();
  194.                 
  195.                 $form = $this->getWebForm();
  196.                 $isOk = $form->processSubmit();
  197.                 $vmController->setDataFromWebForm();
  198.                 
  199.                 if ($isOk === false) {
  200.                     $vmController->updateSubmittedForm($form);
  201.                     $this->_view->callPlugin($this, 'preShowForm');
  202.                     $this->showForm($form, $this->_view->getTemplateFileName(miDefaultView::TEMPLATE_ID_EDIT), true);
  203.                     return false;
  204.                 }
  205.                 
  206.                 $this->_view->callPlugin($this, 'preUpdate');
  207.                 $recordId = $vmController->update();
  208.                 $this->_view->callPlugin($this, 'postUpdate');
  209.                 
  210.             } catch (miException $exception) {
  211.                 $msg = $this->_view->getMessage('MI_RECORD_UPDATE_FAILED_MSG');
  212.                 $this->_view->addRedirectToListControllerCommand($msg . $exception->getMessage(), miMessage::MSG_TYPE_ERROR);
  213.                 return false;
  214.             }
  215.             $msg = sprintf($this->_view->getMessage('MI_RECORD_UPDATED_SUCCESSFULLY_MSG'), $recordId);
  216.             $this->_view->addRedirectToListControllerCommand($msg, miMessage::MSG_TYPE_INFO);
  217.             return true;
  218.         }
  219.     }
  220.     
  221.     /**
  222.      * Delete a record in the table
  223.      * @copyright Copyright (c) 2006,2007 Mirchev Ideas Ltd. All rights reserved.
  224.      * @package MIPHPF
  225.      */
  226.     class miExecDeleteAction extends miAction {
  227.         public function doAction()
  228.         {
  229.             try {
  230.                 $vmController = $this->_view->getViewMapperController();
  231.                 
  232.                 $this->_view->callPlugin($this, 'preDelete');
  233.                 $recordId = $vmController->delete();
  234.                 $this->_view->callPlugin($this, 'postDelete');
  235.                 
  236.             } catch (miException $exception) {
  237.                 $msg = $this->_view->getMessage('MI_RECORD_DELETE_FAILED_MSG');
  238.                 $this->_view->addRedirectToListControllerCommand($msg . $exception->getMessage(), miMessage::MSG_TYPE_ERROR);
  239.                 return false;
  240.             }
  241.             $msg = sprintf($this->_view->getMessage('MI_RECORD_DELETED_SUCCESSFULLY_MSG'), $recordId);
  242.             $this->_view->addRedirectToListControllerCommand($msg, miMessage::MSG_TYPE_INFO);
  243.             return true;
  244.         }
  245.     }
  246.     
  247.     /**
  248.      * Shows data from the database in a table format
  249.      * @copyright Copyright (c) 2006 Mirchev Ideas Ltd. All rights reserved.
  250.      * @package MIPHPF
  251.      */
  252.     class miListAction extends miAction {
  253.         /**
  254.          * Sets the current page to show and the number of records per page
  255.          *
  256.          * @access public
  257.          * @param int $page current page to show
  258.          * @param int $recordsPerPage define how many rows to show in each page
  259.          */
  260.         protected function setPage($page, $recordsPerPage)
  261.         {
  262.             $recordset = $this->_view->getRecordset();
  263.             $totalRecords = $recordset->getRecordsCount();
  264.             if (($page-1) * $recordsPerPage >= $totalRecords)
  265.                 $page = intval(($totalRecords-1) / $recordsPerPage) + 1;
  266.             
  267.             $this->_view->getTablePagerObj()->setPagerLocation($page, $recordsPerPage, $totalRecords);
  268.             $from = intval($page - 1) * $recordsPerPage;
  269.             $recordset->setRecordsLimit($from, $recordsPerPage);
  270.         }
  271.         
  272.         protected function doList()
  273.         {
  274.             // Add main page elements
  275.             $table = $this->_view->getTable();
  276.             
  277.             // Set the page and records per page
  278.             $page = $this->_view->getTablePagerObj()->getPageParam();
  279.             $page = intval($page) > 0 ? intval($page) : 1;
  280.             $this->setPage($page, $this->_view->getTablePagerObj()->getRecordsPerPageParam());
  281.             
  282.             $table->updateState();
  283.             miAppFactory::singleton()->getStateObj()->setArray($table->getState());
  284.             
  285.             $table->addMainPageElements($this->_view->getMainPageElements());
  286.             $html = $table->parse($this->_view->getTemplateFileName(miDefaultView::TEMPLATE_ID_LIST));
  287.             $cmd = new miControllerCommand(miControllerCommand::CONTROLLER_COMMAND_HTML, $html);
  288.             $this->_view->addControllerCommand($cmd);
  289.         }
  290.         
  291.         public function doAction()
  292.         {
  293.             try {
  294.                 $table = $this->_view->getTable();
  295.                 
  296.                 $stateObj = miAppFactory::singleton()->getStateObj();
  297.                 $table->setState($stateObj->getArray());
  298.                 
  299.                 $this->_view->initTable($table);
  300.                 
  301.                 $recordSet = $this->_view->getTable()->getRecordset();
  302.                 $recordSet->setOrder(miGetParamDefault(miTableSorter::PARAM_SORTBY, ''), miGetParamDefault(miTableSorter::PARAM_SORTDIR, ''));
  303.                 
  304.                 $filterObjects = $this->_view->getTableFilterObj()->getFilterObjs();
  305.                 $recordSet->addFilters($filterObjects);
  306.                 
  307.                 $this->_view->callPlugin($this, 'preList');
  308.                 $this->doList();
  309.                 return true;
  310.             
  311.             } catch (miException $exception) {
  312.                 // TODO: we probably shouldn't die here
  313.                 $msg = $this->_view->getMessage('MI_RECORD_LIST_FAILED_MSG');
  314.                 die($msg . $exception->getMessage());
  315.             };
  316.         }
  317.     }
  318. ?>

Documentation generated on Thu, 08 May 2008 16:57:04 +0300 by phpDocumentor 1.4.1