Source for file SubmitFieldsPlugin.php

Documentation is available at SubmitFieldsPlugin.php

  1. <?php
  2.     /**
  3.      * The submit fields plugin
  4.      * @copyright Copyright (c) 2006,2007 Mirchev Ideas Ltd. All rights reserved.
  5.      * @package MIPHPF
  6.      */
  7.     
  8.     /**
  9.      * Handles submit fields
  10.      * @copyright Copyright (c) 2006,2007 Mirchev Ideas Ltd. All rights reserved.
  11.      * @package MIPHPF
  12.      */
  13.     class miSubmitFieldsPlugin extends miViewPlugin {
  14.         /**
  15.          * Array with the submit fields this plugin works on
  16.          * The array has the following format
  17.          * $submitFields = array(
  18.          *     array('fieldName' => '', 'namePrefixSave' => '', 'namePrefixWeb' => '', 'nameSuffix' => ''),
  19.          *     array('fieldName' => '', 'namePrefixSave' => '', 'namePrefixWeb' => '', 'nameSuffix' => ''),
  20.          *     ...
  21.          * );
  22.          * 
  23.          * @access protected
  24.          */
  25.         protected $_submitFields;
  26.         
  27.         /**
  28.          * Constructs the submit fields hanlder
  29.          * 
  30.          * @access public
  31.          * @param miView $view 
  32.          * @param array $submitFields the submit fields
  33.          */
  34.         public function __construct($view$submitFields)
  35.         {
  36.             $this->_submitFields = $submitFields;
  37.             parent::__construct($view);
  38.         }
  39.         
  40.         /**
  41.          * Assign the submit fields template variables as page elements
  42.          *
  43.          * @access protected
  44.          */
  45.         protected function assignSubmitFields($id)
  46.         {
  47.             $templateVars array();
  48.             foreach ($this->_submitFields as $field{
  49.                 if (file_exists($field['namePrefixSave'$id $field['nameSuffix'])) {
  50.                     $templateVars['%%' strtoupper($field['fieldName']'%%'=  $field['namePrefixWeb'$id $field['nameSuffix'];
  51.                     $templateVars['%%' strtoupper($field['fieldName']'_MISSING%%''';
  52.                 else {
  53.                     $templateVars['%%' strtoupper($field['fieldName']'%%'=  '';
  54.                     $templateVars['%%' strtoupper($field['fieldName']'_MISSING%%''style="display: none"';
  55.                 }
  56.             }
  57.             $this->_view->addMainPageElements($templateVars);
  58.         }
  59.         
  60.         /**
  61.          * Processes the submit fields
  62.          *
  63.          * @access protected
  64.          * @param int $id the id of the currently created/edited row
  65.          * @throws miException
  66.          */
  67.         protected function processSubmitFields($id)
  68.         {
  69.             foreach ($this->_submitFields as $field{
  70.                 if (empty($_FILES[$field['fieldName']]['name'])) 
  71.                     continue;
  72.                 
  73.                 $dest $field['namePrefixSave'$id $field['nameSuffix'];
  74.                 if (move_uploaded_file($_FILES[$field['fieldName']]['tmp_name']$dest=== false)
  75.                     throw new miException('Cannot upload file. Saving into "' $dest '" failed.');
  76.             }
  77.         }
  78.         
  79.         
  80.         /**
  81.          * Delete all attached files
  82.          *
  83.          * @param int $id the id of the deleted row
  84.          * @access protected
  85.          */
  86.         protected function deleteSubmitFields($id)
  87.         {
  88.             foreach ($this->_submitFields as $field{
  89.                 $name $field['namePrefixSave'$id $field['nameSuffix'];
  90.                 @unlink($name)// Ignore errors
  91.             }
  92.         }
  93.         
  94.         /**
  95.          * If the action is one of View, Create or Edit assign the submit fields display vars
  96.          * 
  97.          * @access public
  98.          * @param miAction $actionObj action object, subclassed from miAction
  99.          * @param string $actionStep the action step
  100.          */
  101.         public function processActionStep(miAction $actionObj$actionStep)
  102.         {
  103.             if (($actionObj instanceof miViewActionor
  104.                 ($actionObj instanceof miCreateActionor
  105.                 ($actionObj instanceof miEditAction))
  106.             {
  107.                 if ($actionStep == 'preShowForm' && !($actionObj instanceof miCreateAction)) {
  108.                     $this->assignSubmitFields($this->_view->getRecord()->getPK());
  109.                 }
  110.                 if (($actionStep == 'postCreate'or ($actionStep == 'postUpdate')) {
  111.                     $this->processSubmitFields($this->_view->getRecord()->getPK());
  112.                 }
  113.             }
  114.             
  115.             if ($actionObj instanceof miExecDeleteAction{
  116.                 if ($actionStep == 'postDelete'{
  117.                     $this->deleteSubmitFields($this->_view->getRecord()->getPK());
  118.                 }
  119.             }
  120.         }
  121.     }
  122. ?>

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