menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file TAction.php

Documentation is available at TAction.php

  1. <?php
  2. namespace Adianti\Control;
  3.  
  4. use Adianti\Core\AdiantiCoreApplication;
  5. use Adianti\Core\AdiantiCoreTranslator;
  6. use Exception;
  7. use ReflectionMethod;
  8.  
  9. /**
  10.  * Structure to encapsulate an action
  11.  *
  12.  * @version    7.4
  13.  * @package    control
  14.  * @author     Pablo Dall'Oglio
  15.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  16.  * @license    http://www.adianti.com.br/framework-license
  17.  */
  18. class TAction
  19. {
  20.     protected $action;
  21.     protected $param;
  22.     protected $properties;
  23.     
  24.     /**
  25.      * Class Constructor
  26.      * @param $action Callback to be executed
  27.      * @param $parameters = array of parameters
  28.      */
  29.     public function __construct($action$parameters null)
  30.     {
  31.         $this->action = $action;
  32.         
  33.         if (!$this->validate($action))
  34.         {
  35.             $action_string $this->toString();
  36.             throw new Exception(AdiantiCoreTranslator::translate('Method ^1 must receive a parameter of type ^2'__METHOD__'Callback')' <br> '.
  37.                                 AdiantiCoreTranslator::translate('Check if the action (^1) exists'$action_string));
  38.         }
  39.         
  40.         if (!empty($parameters))
  41.         {
  42.             // does not override the action
  43.             unset($parameters['class']);
  44.             unset($parameters['method']);
  45.             
  46.             $this->param = $parameters;
  47.         }
  48.     }
  49.     
  50.     /**
  51.      * Return fields used in parameters
  52.      */
  53.     public function getFieldParameters()
  54.     {
  55.         $field_parameters [];
  56.         
  57.         if ($this->param)
  58.         {
  59.             foreach ($this->param as $parameter)
  60.             {
  61.                 if (substr($parameter,0,1== '{' && substr($parameter,-1== '}')
  62.                 {
  63.                     $field_parameters[substr($parameter,1,-1);
  64.                 }
  65.             }
  66.         }
  67.         
  68.         return $field_parameters;
  69.     }
  70.     
  71.     /**
  72.      * Returns the action as a string
  73.      */
  74.     public function toString()
  75.     {
  76.         $action_string '';
  77.         if (is_string($this->action))
  78.         {
  79.             $action_string $this->action;
  80.         }
  81.         else if (is_array($this->action))
  82.         {
  83.             if (is_object($this->action[0]))
  84.             {
  85.                 $action_string get_class($this->action[0]'::' $this->action[1];
  86.             }
  87.             else
  88.             {
  89.                 $action_string $this->action[0'::' $this->action[1];
  90.             }
  91.         }
  92.         return $action_string;
  93.     }
  94.     
  95.     /**
  96.      * Adds a parameter to the action
  97.      * @param  $param = parameter name
  98.      * @param  $value = parameter value
  99.      */
  100.     public function setParameter($param$value)
  101.     {
  102.         $this->param[$param$value;
  103.     }
  104.     
  105.     /**
  106.      * Set the parameters for the action
  107.      * @param  $parameters = array of parameters
  108.      */
  109.     public function setParameters($parameters)
  110.     {
  111.         // does not override the action
  112.         unset($parameters['class']);
  113.         unset($parameters['method']);
  114.         unset($parameters['static']);
  115.         
  116.         $this->param = $parameters;
  117.     }
  118.     
  119.     /**
  120.      * Returns a parameter
  121.      * @param  $param = parameter name
  122.      */
  123.     public function getParameter($param)
  124.     {
  125.         if (isset($this->param[$param]))
  126.         {
  127.             return $this->param[$param];
  128.         }
  129.         return NULL;
  130.     }
  131.     
  132.     /**
  133.      * Return the Action Parameters
  134.      */
  135.     public function getParameters()
  136.     {
  137.         return $this->param;
  138.     }
  139.     
  140.     /**
  141.      * Returns the current calback
  142.      */
  143.     public function getAction()
  144.     {
  145.         return $this->action;
  146.     }
  147.     
  148.     /**
  149.      * Set property
  150.      */
  151.     public function setProperty($property$value)
  152.     {
  153.         $this->properties[$property$value;
  154.     }
  155.     
  156.     /**
  157.      * Get property
  158.      */
  159.     public function getProperty($property)
  160.     {
  161.         return $this->properties[$property?? null;
  162.     }
  163.     
  164.     /**
  165.      * Prepare action for use over an object
  166.      * @param $object Data Object
  167.      */
  168.     public function prepare($object)
  169.     {
  170.         $parameters $this->param;
  171.         $action     clone $this;
  172.         
  173.         if ($parameters)
  174.         {
  175.             if (isset($parameters['*']))
  176.             {
  177.                 unset($parameters['*']);
  178.                 unset($action->param['*']);
  179.                 
  180.                 foreach ($object as $attribute => $value)
  181.                 {
  182.                     if (is_scalar($value))
  183.                     {
  184.                         $parameters[$attribute$value;
  185.                     }
  186.                 }
  187.             }
  188.             
  189.             foreach ($parameters as $parameter => $value)
  190.             {
  191.                 // replace {attribute}s
  192.                 $action->setParameter($parameter$this->replace($value$object) );
  193.             }
  194.         }
  195.         
  196.         return $action;
  197.     }
  198.     
  199.     /**
  200.      * Replace a string with object properties within {pattern}
  201.      * @param $content String with pattern
  202.      * @param $object  Any object
  203.      */
  204.     private function replace($content$object)
  205.     {
  206.         if (preg_match_all('/\{(.*?)\}/'$content$matches) )
  207.         {
  208.             foreach ($matches[0as $match)
  209.             {
  210.                 $property substr($match1-1);
  211.                 
  212.                 if (strpos($property'->'!== FALSE)
  213.                 {
  214.                     $parts explode('->'$property);
  215.                     $container $object;
  216.                     foreach ($parts as $part)
  217.                     {
  218.                         if (is_object($container))
  219.                         {
  220.                             $result $container->$part;
  221.                             $container $result;
  222.                         }
  223.                         else
  224.                         {
  225.                             throw new Exception(AdiantiCoreTranslator::translate('Trying to access a non-existent property (^1)'$property));
  226.                         }
  227.                     }
  228.                     $content $result;
  229.                 }
  230.                 else
  231.                 {
  232.                     $value    = isset($object->$property)$object->$property null;
  233.                     $content  str_replace($match$value$content);
  234.                 }
  235.             }
  236.         }
  237.         
  238.         return $content;
  239.     }
  240.     
  241.     /**
  242.      * Converts the action into an URL
  243.      * @param  $format_action = format action with document or javascript (ajax=no)
  244.      */
  245.     public function serialize($format_action TRUE)
  246.     {
  247.         // check if the callback is a method of an object
  248.         if (is_array($this->action))
  249.         {
  250.             // get the class name
  251.             $url['class'is_object($this->action[0]get_class($this->action[0]$this->action[0];
  252.             // get the method name
  253.             $url['method'$this->action[1];
  254.             
  255.             if (isset($_GET['register_state']AND $_GET['register_state'== 'false' AND empty($this->param['register_state']))
  256.             {
  257.                 $url['register_state''false';
  258.             }
  259.             
  260.             if (isset($_GET['target_container']AND !empty($_GET['target_container']AND empty($this->param['target_container']AND ($_GET['target_container'!== 'adianti_div_content'))
  261.             {
  262.                 $url['target_container'$_GET['target_container'];
  263.             }
  264.             
  265.             if ($this->isStatic())
  266.             {
  267.                 $url['static''1';
  268.             }
  269.         }
  270.         // otherwise the callback is a function
  271.         else if (is_string($this->action))
  272.         {
  273.             // get the function name
  274.             $url['method'$this->action;
  275.         }
  276.         
  277.         // check if there are parameters
  278.         if ($this->param)
  279.         {
  280.             $url array_merge($url$this->param);
  281.         }
  282.         
  283.         if ($format_action)
  284.         {
  285.             if ($router AdiantiCoreApplication::getRouter())
  286.             {
  287.                 return $router(http_build_query($url));
  288.             }
  289.             else
  290.             {
  291.                 return 'index.php?'.http_build_query($url);
  292.             }
  293.         }
  294.         else
  295.         {
  296.             //return http_build_query($url);
  297.             if ($router AdiantiCoreApplication::getRouter())
  298.             {
  299.                 return $router(http_build_query($url)FALSE);
  300.             }
  301.             else
  302.             {
  303.                 return http_build_query($url);
  304.             }
  305.         }
  306.     }
  307.     
  308.     /**
  309.      * Validate action
  310.      */
  311.     public function validate()
  312.     {
  313.         $class is_string($this->action[0])$this->action[0]get_class($this->action[0]);
  314.         
  315.         if (class_exists($class))
  316.         {
  317.             $method $this->action[1];
  318.             
  319.             if (method_exists($class$method))
  320.             {
  321.                 return TRUE;
  322.             }
  323.         }
  324.         
  325.         return FALSE;
  326.     }
  327.     
  328.     /**
  329.      * Returns if the action is static
  330.      */
  331.     public function isStatic()
  332.     {
  333.         if (is_array($this->action))
  334.         {
  335.             $class is_string($this->action[0])$this->action[0]get_class($this->action[0]);
  336.             
  337.             if (class_exists($class))
  338.             {
  339.                 $method $this->action[1];
  340.                 
  341.                 if (method_exists($class$method))
  342.                 {
  343.                     $rm new ReflectionMethod$class$method );
  344.                     return $rm-> isStatic (|| (isset($this->param['static']&& $this->param['static'== '1');
  345.                 }
  346.             }
  347.         }
  348.         return FALSE;
  349.     }
  350. }