Guest

biakaveron

By: a guest on May 8th, 2009  |  syntax: PHP  |  size: 7.85 KB  |  hits: 2,860  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2.  
  3. /**
  4.  * helper for working with system-generated messages
  5.  *
  6.  * Each message has a type (error/warning/success etc). Also there are two
  7.  * additional types: custom (for data store) and validate (for validation errors)
  8.  *
  9.  * @package    CMS
  10.  * @author     Brotkin Ivan (BIakaVeron) <BIakaVeron@gmail.com>
  11.  * @copyright  Copyright (c) 2009 Brotkin Ivan
  12.  */
  13.  
  14. class message_Core {
  15.  
  16. /*
  17.  * @property Session $session
  18.  *
  19.  */
  20.  
  21.         // all helper data
  22.         static protected $data = array();
  23.         // array for previous requests data
  24.         static protected $loaded = array();
  25.         // array for currently added data only
  26.         static protected $added = array();
  27.         // default template name (use in render() method)
  28.         static protected $template = 'messages/default';
  29.         // this property allow to render all-in-one validation errors
  30.         static protected $show_validation = FALSE;
  31.         // what is the name of session variable
  32.         static protected $session_var;
  33.         // session object
  34.         static protected $session = FALSE;
  35.  
  36.  
  37.         /*
  38.          * Initial data loading
  39.          */
  40.         public function init() {
  41.                 // don't call init() twice!
  42.                 if (self::$session !== FALSE) return FALSE;
  43.  
  44.                 // set the session var name
  45.                 self::$session_var = Kohana::config('message.message_key');
  46.  
  47.                 self::$session = Session::instance();
  48.                
  49.                 // load session data
  50.                 self::$loaded = self::$session->get_once(self::$session_var);
  51.                
  52.                 if (empty(self::$loaded)) {
  53.                         // create empty array - there is no data
  54.                         self::$loaded = array();
  55.                 }
  56.  
  57.                 self::$data = self::$loaded;
  58.         }
  59.  
  60. /**
  61.  *
  62.  * Save data in session. Saves new data only ($added array)
  63.  *
  64.  */
  65.         public function save() {
  66.                 self::$session->set(self::$session_var, self::$added);
  67.         }
  68.  
  69. /**
  70.  *
  71.  * Save all data in session ($data array).
  72.  * Needed if we want to use current data on the next page
  73.  *
  74.  */
  75.  
  76.         public function save_all() {
  77.                 self::$session->set(self::$session_var, self::$data);
  78.         }
  79.  
  80. /**
  81.  *
  82.  * Sets new template.
  83.  *
  84.  * @param string $template   new template name
  85.  */
  86.  
  87.         public function set_template($template = 'default') {
  88.                 self::$template = 'messages/'.$template;
  89.         }
  90.  
  91. /**
  92.  *
  93.  * Syncronizes $data, $added and $loaded arrays. Uses when some new data added
  94.  *
  95.  * @param string $type   which type of messages will we syncronize
  96.  */
  97.  
  98.         private function sync($type = NULL) {
  99.                 if (is_null($type)) {
  100.                         // sync all data
  101.                         self::$data = self::$loaded + self::$added;
  102.                 }
  103.                 else {
  104.                         // sync $type messages only
  105.                         self::$data[$type] = (isset(self::$loaded[$type]) ? self::$loaded[$type] : array()) + (isset(self::$added[$type]) ? self::$added[$type] : array());
  106.                 }
  107.         }
  108.  
  109. /**
  110.  *
  111.  * Removes data from helper.
  112.  *
  113.  * Can delete all data ($type is NULL), or $type data, or one value ($tag is set)
  114.  *
  115.  * @param string $type    messages type to remove
  116.  * @param string $tag     tagname to remove one value
  117.  */
  118.  
  119.         private function remove($type, $tag = NULL) {
  120.                 if (is_null($tag)) {
  121.                         // remove all data
  122.                         if (isset(self::$data[$type])) unset(self::$data[$type]);
  123.                         if (isset(self::$added[$type])) unset(self::$added[$type]);
  124.                         if (isset(self::$loaded[$type])) unset(self::$loaded[$type]);
  125.                 }
  126.                 else {
  127.                         // remove only $type data (with $tag checking)
  128.                         if (isset(self::$data[$type][$tag])) unset(self::$data[$type][$tag]);
  129.                         if (isset(self::$added[$type][$tag])) unset(self::$added[$type][$tag]);
  130.                         if (isset(self::$loaded[$type][$tag])) unset(self::$loaded[$type][$tag]);
  131.                 }
  132.         }
  133.  
  134. /**
  135.  *
  136.  * Returns all values of supplied type and tagname and removes them from helper
  137.  *
  138.  * @param string $type   messages type
  139.  * @param string $tag    tagname if one value needed
  140.  * @return array
  141.  */
  142.  
  143.         public function get_type($type, $tag = NULL) {
  144.                 if (is_null($type)) {
  145.                         // full data request
  146.                         $result = self::$data;
  147.                         self::clear();
  148.                         return $result;
  149.                 }
  150.  
  151.                 // check for data of supplied type
  152.                 if (!isset(self::$data[$type])) return array();
  153.  
  154.                 if (isset($tag)) {
  155.                         // returns one value if tagname supplied
  156.                         if (!isset(self::$data[$type][$tag])) return array();
  157.                         else $result = array(self::$data[$type][$tag]);
  158.                 }
  159.                 else {
  160.                         // get all data of this type
  161.                         $result = self::$data[$type];
  162.                 }
  163.  
  164.                 // delete returned data from helper
  165.                 self::remove($type, $tag);
  166.  
  167.                 return $result;
  168.         }
  169.  
  170. /**
  171.  *
  172.  * @param string|array $message  data to store
  173.  * @param string       $type     data type
  174.  */
  175.  
  176.         public function add($message, $type = 'info') {
  177.                 if (is_array($message)) {
  178.                         // save data as $key=>$value
  179.                         if (isset(self::$added[$type])) {
  180.                                 // this data type array already exists
  181.                                 self::$added[$type] += $message;
  182.                         }
  183.                         else {
  184.                                 // its a first data of this type
  185.                                 self::$added[$type] = $message;
  186.                         }
  187.                 }
  188.                 else {
  189.                         // its a string data
  190.                         if (!isset(self::$added[$type])) {
  191.                                 // there is no such type data
  192.                                 self::$added[$type][] = $message;
  193.                         }
  194.                         elseif (!in_array($message, self::$added[$type])) {
  195.                                 // add message string
  196.                                 self::$added[$type][] = $message;
  197.                         }
  198.                 }
  199.  
  200.                 // sync data after every change
  201.                 self::sync($type);
  202.         }
  203.  
  204. /**
  205.  *
  206.  * Adds validation errors (usually from Validation->errors() method)
  207.  *
  208.  * Data key will be a fieldname, value - i18n-string with error description
  209.  *
  210.  * @param array  $errors        validation errors
  211.  * @param string $i18n_file   i18n filename
  212.  */
  213.  
  214.         public function add_validation(array $errors, $i18n_file) {
  215.                 $result = array();
  216.  
  217.                 // collecting error messages in result array
  218.                 foreach($errors as $key => $value)
  219.                         $result[$key] = Kohana::lang($i18n_file.".".$key."_".$value);
  220.  
  221.                 // add errors to $added array with validation type
  222.                 self::add($result, 'validation');
  223.         }
  224.  
  225. /**
  226.  *
  227.  * Delete all storing data (possible by type and tagname)
  228.  *
  229.  * @param string $type  data type to delete
  230.  */
  231.  
  232.         public function clear($type = NULL, $tag = NULL) {
  233.                 if (is_null($type)) {
  234.                         // remove all data
  235.                         self::$data = self::$added = self::$loaded = array();
  236.                 }
  237.                 else {
  238.                         // remove supplied type only
  239.                         self::remove($type, $tag);
  240.                 }
  241.         }
  242.  
  243. /**
  244.  *
  245.  * Returns custom type data by tagname.
  246.  *
  247.  * @param  string       $tag      tagname
  248.  * @param  mixed        $default  default value if not exists
  249.  * @return string|array
  250.  */
  251.  
  252.         public function custom($tag = NULL, $default = NULL) {
  253.                 if (is_null($tag)) {
  254.                         // returns all custom data
  255.                         if (!isset(self::$data['custom'])) return array();
  256.                         $result = self::$data['custom'];
  257.                         // dont forget to clear data!
  258.                         self::remove('custom');
  259.                         return $result;
  260.                 }
  261.  
  262.                 // check for existing
  263.                 if (isset(self::$data['custom'][$tag])) {
  264.                         // get tagged value and delete it from data
  265.                         $result = self::$data['custom'][$tag];
  266.                         self::remove('custom', $tag);
  267.                         return $result;
  268.                 }
  269.                 else return $default;
  270.         }
  271.  
  272. /**
  273.  * Shows message box with supplied type and tagname
  274.  *
  275.  * @param   string   $type   data type to render
  276.  * @param   string   $tag    tagname if only one value needed
  277.  * @return  boolean          FALSE if there was no rendering
  278.  */
  279.  
  280.         public function render($type = NULL, $tag = NULL) {
  281.                 // check for data
  282.                 if (count(self::$data)==0) return FALSE;
  283.  
  284.                 // custom data may use in form submitting for example
  285.                 if ($type === 'custom') return FALSE;
  286.                
  287.                 if (is_null($type)) {
  288.                         // render all data by existing types
  289.                         foreach(self::$data as $type=>$value) {
  290.                                 self::render($type, $tag);
  291.                         }
  292.                         return TRUE;
  293.                 }
  294.  
  295.                 // don't show validation data without tagname
  296.                 if (self::$show_validation===FALSE AND $type == 'validation' AND is_null($tag))
  297.                         return FALSE;
  298.  
  299.                 $data = self::get_type($type, $tag);
  300.                 // there is no data to render
  301.                 if (count($data)==0) return FALSE;
  302.  
  303.                 $view = new View(self::$template);
  304.                 $view->data = $data;
  305.                 $view->type = $type;
  306.                 // if tagname supplied it will be a single box without many message string
  307.                 $view->inline = (isset($tag) ? " inline" : "");
  308.                
  309.                 echo $view->render();
  310.  
  311.                 return TRUE;
  312.         }
  313.  
  314. }