Advertisement
Guest User

Untitled

a guest
May 28th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. <?php
  2.  
  3. class selectOption
  4. {
  5.     public $id;
  6.     public $value;
  7.     private $lvl;
  8.     private $children;
  9.  
  10.     public function __construct($aLvl, $aId, $aValue)
  11.     {
  12.         $this->children = [];
  13.         $this->lvl = $aLvl;
  14.         $this->id = htmlspecialchars($aId);
  15.         $this->value = htmlspecialchars($aValue);
  16.     }
  17.  
  18.     /**
  19.      * Zwraca ostatnie dziecko (jeśli nie istnieje, zwraca siebie)
  20.      */
  21.     public function lastChild()
  22.     {
  23.         if (isset($this->children))
  24.             return $this->children[count($this->children) - 1];
  25.         else
  26.             return $this;
  27.     }
  28.  
  29.     /**
  30.      * Dodaje dziecko do tablicy
  31.      * @param int $aId - Id dla tagu <option>
  32.      * @param string $aValue - Zawartość tekstowa tagu <option>
  33.      */
  34.     public function pushChildren($aId, $aValue)
  35.     {
  36.         $this->children[] = new selectOption($this->lvl + 1, $aId, $aValue);
  37.     }
  38.  
  39.     /**
  40.      * Wyświetla kod swój i dzieci, jeśli jest to masteroption wyświetla same dzieci
  41.      * @param &string $htmlCode - do tego stringa zostanie dopisany kod html
  42.      */
  43.     public function show(&$htmlCode)
  44.     {
  45.         if ($this->value != null) {
  46.             $htmlCode .= '<option id="' . $this->id . '">' . str_repeat('&nbsp;', $this->lvl * 5) . $this->value . '</option>';
  47.             //$htmlCode.='<option id="'.$this->id.'" style="padding-left: '.$this->lvl*(10).'px;">'.$this->value.'</option>';
  48.         }
  49.         foreach ($this->children as $child)
  50.         {
  51.             $child->show($htmlCode);
  52.         }
  53.     }
  54. }
  55.  
  56. class tagSelect
  57. {
  58.     private $masteroption;
  59.  
  60.     /**
  61.      * Konstruktor; masteroption to główny obiekt typu selectOption, który ma przechowywać wszystkie dzieci.
  62.      * Nie jest on widoczny, pełni tylko rolę kontenera
  63.      * @param string $url - ścieżka do pliku z JSON
  64.      */
  65.     public function __construct($url)
  66.     {
  67.         $this->masteroption = new selectOption(-1, null, null);
  68.         $this->generate($url);
  69.     }
  70.  
  71.     public function getHtmlCode()
  72.     {
  73.         $htmlCode = '<select>';
  74.         $this->masteroption->show($htmlCode);
  75.         $htmlCode .= '</select>';
  76.         return $htmlCode;
  77.     }
  78.  
  79.     public function show()
  80.     {
  81.         echo $this->getHtmlCode();
  82.     }
  83.  
  84.     /**
  85.      * Rekurencyjna funkcja do interpretacji JSON
  86.      * @param array $jsonChildren - tablica "childs" rodzica z kodu JSON
  87.      * @param &selectOption $parentHandle - uchwyt do rodzica
  88.      */
  89.     private function parseOne($jsonChildren, &$parentHandle)
  90.     {
  91.         foreach ($jsonChildren as $v)
  92.         {
  93.             $parentHandle->pushChildren($v['id'], $v['name']);
  94.             if (isset($v['childs']))
  95.                 $this->parseOne($v['childs'], $parentHandle->lastChild());
  96.         }
  97.     }
  98.  
  99.     /**
  100.      * Pobiera zawartość JSON i zaczyna interpretację
  101.      * @param string $url - ścieżka do pliku z JSON
  102.      */
  103.     private function generate($url)
  104.     {
  105.         if (!$json = file_get_contents($url))
  106.             die('Can not open JSON file');
  107.         $info = json_decode($json, true);
  108.         if ($info == null)
  109.             die('Bad JSON code');
  110.         $this->parseOne($info, $this->masteroption);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement