Advertisement
rfv123

CheckboxHistory class

Apr 27th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.89 KB | None | 0 0
  1. <?php
  2. namespace app\system;
  3. /**
  4. * Maintain a list of checkboxes and the immediate history
  5. *
  6. * Uses: array of  $key => $label
  7. *
  8. *   the key can be numbers or string the class works with keys    
  9. *
  10. *   e.g.  $cbxHistory = new CheckboxHistory(array('key1' => 'label 1',
  11. *                                                 'key2' => 'label 2',
  12. *                                                 'key3' => 'label 3',
  13. *                                                 ))
  14. *  
  15. * It uses a string to hold whether each checkbox in the list currently is checked or not
  16. *
  17. *  e.g.   $cbxHistory->cbxStateString()
  18. *  
  19. *         This will be stored in a hidden field $_POST and will become the $prvCheckedState
  20. *          
  21. *
  22. *
  23. *  It generates all the details to
  24. *     1) use in the checkbox HTML
  25. *          'label' is for the html checkbox label
  26. *          'value' is for the html checkbox value
  27. *            note: the value is a position in the list not a key!      
  28. *
  29. *     2) The state of the checkbox and whether is has just changed
  30. *
  31. *  e.g.  $cbxHistory->cbxDetails($key);
  32. *
  33. *        returns: array('label'       => $this->cbxLabel($key),
  34. *                       'value'       => $this->cbxValue($key),
  35. *                       'key'         => $key,
  36. *                        
  37. *                       'hasChanged'  => $this->hasChanged($key),
  38. *                       'curChecked'  => $this->curChecked($key),
  39. *                       'prvChecked'  => $this->prvChecked($key));
  40. *      
  41. *  It uses a cbxStateString to know what the previous states of the checkbox
  42. *
  43. *    e.g.  $cbxHistory->setCbxCheckedState($oldCbxCheckedState);
  44. *
  45. *          This will normally be from the html 'hidden' cbxState field
  46. *  
  47. */
  48.  
  49. class CheckboxHistory implements ICheckboxHistory {
  50.  
  51.    protected $labelList = null;
  52.        
  53.    protected $curCheckedState = '';
  54.    
  55.    protected $prvCheckedState = '';
  56.    
  57.    protected $keyList = null; // needed to find the position of the key
  58.    
  59.    public function __construct(array $labelList, $prvStateStr = '')
  60.    {
  61.        $pos = 0;
  62.        foreach($labelList as $key => $label) {
  63.            $this->labelList[$key] = array('label' => $label, 'pos' => $pos);
  64.            $pos++;
  65.        }
  66.        $this->curCheckedState = str_repeat('0', count($this->labelList));                                      
  67.        $this->prvCheckedState = str_repeat('0', count($this->labelList));
  68.        $this->keyList = array_keys($labelList);
  69.        if (!empty($prvStateStr)) {
  70.            $this->fromCbxStateString($prvStateStr);
  71.        }                      
  72.    }
  73.  
  74.    /**
  75.    * The label value to be used in the html
  76.    *
  77.    * @param mixed $key
  78.    *
  79.    * @return label text
  80.    */
  81.    public function cbxLabel($key)
  82.    {
  83.        return $this->labelList[$key]['label'];
  84.    }
  85.  
  86.    /**
  87.    * The value to be used for the checkbox in the html.
  88.    *
  89.    * It is actually the postion of the lookup key in the list
  90.    *  
  91.    * @param mixed $key
  92.    *
  93.    * @return integer
  94.    */
  95.    public function cbxValue($key)
  96.    {
  97.        return $this->keyToPosition($key);
  98.    }
  99.  
  100.    /**
  101.    * The 'key' of the checkbox given the 'value' (position])
  102.    *
  103.    * It is actually the key at that position in the list
  104.    *  
  105.    * @param integer position (value)
  106.    *
  107.    * @return integer
  108.    */
  109.    public function cbxKey($value)
  110.    {
  111.        return $this->keyList[$position];
  112.    }
  113.  
  114.    /**
  115.    * This is the current state vector of all the checkboxes
  116.    *
  117.    * It is stored in a 'hidden' field on the form
  118.    *
  119.    * It is used in the  fromCbxStateString() method  
  120.    *
  121.    * @return string
  122.    */  
  123.    public function cbxStateString()
  124.    {
  125.        return $this->curCheckedState;
  126.    }
  127.  
  128.  
  129.    /**
  130.    * An array of checkbox keys
  131.    *
  132.    * used to convert the 'position' (value) to a key for lookup.
  133.    *
  134.    * @return array
  135.    */
  136.    public function cbxKeyList()
  137.    {
  138.        return $this->keyList;
  139.    }
  140.    
  141.    /**
  142.    * All the details (checkbox state) in one convenient list
  143.    *
  144.    * @param mixed $key
  145.    *
  146.    * @return array
  147.    */
  148.    public function cbxDetails($key)
  149.    {
  150.        return array('label'       => $this->cbxLabel($key),
  151.                     'value'       => $this->cbxValue($key),
  152.                     'key'         => $key,
  153.                     'hasChanged'  => $this->hasChanged($key),
  154.                     'curChecked'  => $this->curChecked($key),
  155.                     'prvChecked'  => $this->prvChecked($key),
  156.                );
  157.    }
  158.    
  159.    public function cbxDetailsByKey($key) { return $this->cbxDetails($key); }
  160.  
  161.    /**
  162.    * All the details (checkbox state) in one convenient list given a checkbox value
  163.    *
  164.    *
  165.    * @param integer $position
  166.    *
  167.    * @return array
  168.    */
  169.    public function cbxDetailsByValue($position)
  170.    {
  171.        return $this->cbxDetails($this->keyList[$position]);
  172.     }
  173.  
  174.   /**
  175.   * All the cbxDetails as an iterator
  176.   *
  177.   *   The key of the array is position  (cbxValue)
  178.   *
  179.   * @return array
  180.   */
  181.   public function cbxDetailsArray()
  182.   {
  183.        $details = array();
  184.        foreach($this->keyList as $key) {
  185.            $details[] = $this->cbxDetails($key);
  186.        }
  187.        return $details;
  188.   }
  189.  
  190.    /**
  191.    * All the cbxDetails as an iterator
  192.    *
  193.    * The key of the array is position  (cbxValue)
  194.    *
  195.    * @return \ArrayIterator
  196.    */
  197.    public function getIterator()
  198.    {
  199.        return new \ArrayIterator($this->cbxDetailsArray());
  200.    }
  201.    
  202.      
  203.    /**
  204.    * Set or unset a checkbox by key value
  205.    *
  206.    * @param mixed   $key
  207.    * @param boolean $checked
  208.    *
  209.    * @return void
  210.    */
  211.    public function setCheckbox($key, $checked = true)
  212.    {
  213.            $keyPos = $this->keyToPosition($key);
  214.            $this->curCheckedState[$keyPos] = $checked ? '1' : '0';
  215.    }
  216.    
  217.    public function setCheckboxByKey($key, $checked = true) { $this->setCheckbox($key, $checked); }
  218.  
  219.  
  220.    public function setCheckboxByValue($cbxValue, $checked = true)
  221.    {
  222.            $this->setCheckbox($this->keyList[$cbxValue], $checked);
  223.    }
  224.  
  225.    /**
  226.    * current state of a checkbox
  227.    *
  228.    * @param mixed $key
  229.    *
  230.    * @return boolean
  231.    */
  232.    public function curChecked($key)
  233.    {
  234.        $keyPos = $this->keyToPosition($key);      
  235.        return (bool) $this->curCheckedState[$keyPos];
  236.    }
  237.  
  238.    /**
  239.    * previous state of a checkbox
  240.    *
  241.    * @param mixed $key
  242.    *
  243.    * @return booleam
  244.    */
  245.    public function prvChecked($key)
  246.    {
  247.        $keyPos = $this->keyToPosition($key);      
  248.        return (bool) $this->prvCheckedState[$keyPos];
  249.    }
  250.  
  251.    /**
  252.    * Has the checkbox changed state (user checked or unchecked it)
  253.    *
  254.    * @param mixed $key
  255.    *
  256.    * @return boolean
  257.    */
  258.    public function hasChanged($key)
  259.    {
  260.        $keyPos = $this->keyToPosition($key);      
  261.        return $this->curCheckedState[$keyPos] !== $this->prvCheckedState[$keyPos];
  262.    }  
  263.  
  264.    /**
  265.    * set the curCheckedState from an array of values ($positions)
  266.    *  
  267.    * @param array $positionList i.e. $_POST
  268.    *
  269.    * @return void
  270.    */
  271.    public function fromCheckedList(array $checkedPos)
  272.    {
  273.        $this->curCheckedState = str_repeat('0', count($this->labelList));                                      
  274.        foreach ($checkedPos as $position) {          
  275.            $this->setCheckbox($this->keyList[$position], true);
  276.        }
  277.    }
  278.    
  279.    /**
  280.    * This is the previous state of the all checkboxes
  281.    *
  282.    * It is obtained from the chechboxes 'hidden' field
  283.    *
  284.    * @param string $prvStateStr
  285.    *
  286.    * @return void
  287.    */
  288.    public function setCbxStateString($prvStateStr)
  289.    {
  290.        // must be the correct lentgth
  291.        $this->prvCheckedState = str_pad($prvStateStr, count($this->labelList), $this->prvCheckedState);
  292.    }
  293.    
  294.    
  295.    // given a key get the postion of the key in the list
  296.    protected function keyToPosition($key)
  297.    {
  298.        return $this->labelList[$key]['pos'];
  299.    }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement