Location: PHPKode > projects > phpAWE > phpAWE.0.4.beta.demo-page/awe/classes/TawePageTree.php
<?php



class TawePageTree extends TaweBaseListItem  {

/// IMPORTANT: text references are also used in code below, 
//             if you intend to change them, remember to change all occurences
//
//             first parameter of dataSource functions is ALWAYS parent folder             

protected $awePageMenuLevelsOpt = array (1, "distinct_active_level" => 1);
protected $awePageMenuStyles = array (0, 1, 2, 3, "single" => 1, "expand_selected" => 2, "expand_all" => 3);
protected $awePageMenuTempOpt = array (0, 1, 2, 3, 4, "distinct_selected" => 1, "distinct_first" => 2 , "distinct_last" => 3, "distinct_expandable" => 4);


      public function view ($dataSource, $dataSourceParams, $currentAddress, $template, $templateOpt, $style, $levelOpt="", $depth=0, $pictureExt ="", $level=0) {

        global $layoutHtmlDir;
        global $templateAreola;
        global $tempFormContent;
        global $tempSubLevel;
        
        global $aweSqlPrimaryKey;
        global $aweSqlPredecField;
        
        global $addressBase;
        global $tempItemSelectedSufix;
        global $tempItemFirstSufix;
        global $tempItemLastSufix;
        global $tempItemInactiveSufix;
        global $tempItemExpandableSufix;
        
        
        /// TEMPLATE ARRAY HANDLE
        $submenuTemplate = $template;
        $template = exArrayShift($submenuTemplate);
        
        /// STYLE OPTIONS ARRAY HANDLE
        $submenuTemplateOpt = $templateOpt;
        $templateOpt = exArrayShift($submenuTemplateOpt);
        
        /// MENU STYLE ARRAY HANDLE
        $submenuStyleOpt = $style;
        $menuStyle = exArrayShift($submenuStyleOpt);
       
        /// MENU STYLE SETUP
        $menuStyle = optionsToTable($menuStyle,$this->awePageMenuStyles);
       
        /// TEMPLATE SETUP
        $templateOpt = optionsToTable($templateOpt,$this->awePageMenuTempOpt);

        /// GET MENU ITEMS FROM DATABASE
        $menuData = $this->call($dataSource, $dataSourceParams); //($root,$order, true, true);
        
        /// GET ITEM DESCENDENTS FOR ADDRESS PURPOSES
        $descendents = $this->getPredecessors($menuData[0][$aweSqlPrimaryKey]);
        
        /// MENU LEVEL OPTIONS SETUP (ACTIVE/INACTIVE)
        if (($this->awePageMenuLevelsOpt[$levelOpt] == $this->awePageMenuLevelsOpt["distinct_active_level"] && strlen($levelOpt)) && $this->itemTopSelectedLevel($currentAddress)-1 != sizeof($descendents))  
          $inactive = $tempItemInactiveSufix;      
      
        /// TEMPLATE SETUP          
        $last = start_value;

        /// MENU ITEM ADD LOOP
        for ($t=0;$t<sizeof($menuData);$t++) {
              /// SPECIAL CASE TEMPLATE        
              $extra ="";
              if ($this->itemIsSelected($currentAddress, $menuData[$t][$aweSqlPrimaryKey]) && $templateOpt[$this->awePageMenuTempOpt["distinct_selected"]]) 
                {$extra .= $tempItemSelectedSufix;};
              if ($t==0 && $templateOpt[$this->awePageMenuTempOpt["distinct_first"]])
                {$extra .= $tempItemFirstSufix;}; 
              if (($t+1)==sizeof($menuData) && $templateOpt[$this->awePageMenuTempOpt["distinct_last"]])
                {$extra .= $tempItemLastSufix;};
              if ($this->itemHasChildren($menuData[$t][id]) && $templateOpt[$this->awePageMenuTempOpt["distinct_expandable"]])
                {$extra .= $tempItemExpandableSufix;};
      
              if (strcmp($last,$extra))
                 {
                 $itemTemplate = file_get_contents(getItemTemplate($this->moduleName,$template,$extra.$inactive)); 
                 $last =$extra; $extra="";
                 };
                 
              /// FILL UP TEMPLATES
              
              $content .= $this->makeListItem($menuData[$t], $itemTemplate, $template, $pictureExt);
              $dataSourceParams[0] = $menuData[$t][id];
              
              /// FIX DATA SOURCE FOR EXPANDED TREE                 
              $dataSource = "getSuccessors"; 
  
              /// SUBMENU EXPAND ALL
              if ($menuStyle[$this->awePageMenuStyles["expand_all"]] && $depth) {
                $content = str_replace(
                                      $templateAreola.$tempSubLevel.$templateAreola, 
                                      $this->view($dataSource, $dataSourceParams, $currentAddress,$submenuTemplate,$submenuTemplateOpt,$submenuStyleOpt,$levelOpt,$depth-1,$pictureExt,$level+1), 
                                      $content
                                      );

              }
      
              /// SUBMENU EXPAND SELECTED
              if ($menuStyle[$this->awePageMenuStyles["expand_selected"]] && $this->itemIsSelected($currentAddress, $menuData[$t][id]) && $depth) 
                $content = str_replace(
                                      $templateAreola.$tempSubLevel.$templateAreola, 
                                      $this->view($dataSource, $dataSourceParams, $currentAddress,$submenuTemplate,$submenuTemplateOpt,$submenuStyleOpt,$levelOpt,$depth-1,$pictureExt,$level+1), 
                                      $content
                                      ); 
                                      
              $content = str_replace($templateAreola.$tempSubLevel.$templateAreola, "", $content);                        

        }
        /// PUT ITEMS INTO FORM
        if (sizeof($menuData))
        $result = $this->makeListForm($content, file_get_contents(getFormTemplate($this->moduleName,$template,$inactive)));
        
        return $result;
      }
      
      
      public function makeAddress($itemId) {
        global $addressDelimiter;
        global $addressItemTemplate;
        global $menuStartLevel;
        global $aweSqlPrimaryKey;
        
        $descendents = $this->getPredecessors($itemId); 
        if ($descendents) $rootId = $descendents[0]; else $rootId = $itemId; 
        
        $parentaddr = $this->makeParentAddress($rootId);
        $treeaddr = $this->makePredecessorAddress($itemId); 
        
        $i = sizeof(explode($addressDelimiter,$treeaddr))-1;  
  
        $result = $parentaddr.$treeaddr.sprintf($addressItemTemplate, $this->navPrefix.($i+$menuStartLevel), $itemId).$addressDelimiter;
              
        return $result;
      }
      
      public function makePredecessorAddress($itemId) {
        global $addressDelimiter;
        global $addressItemTemplate;
        global $menuStartLevel;
        global $aweSqlPredecField;
        
 
        $descendents = $this->getPredecessors($itemId);
        
        if (is_array($descendents))
          for ($i=0; $i<sizeof($descendents); $i++) {
              $result .= sprintf($addressItemTemplate, $this->navPrefix.($i+$menuStartLevel), $descendents[$i]).$addressDelimiter;
            }
        else if (isset($descendents))
              $result .= sprintf($addressItemTemplate, $this->navPrefix.($startNumber), $descendents).$addressDelimiter;
        return $result;      
      }
      
      public function getSuccessors($itemId, $order) {
      global $aweSqlPredecField;
      global $aweDataTable;
        $result = $this->aweQuery->select("",$this->moduleName.$aweDataTable,sqlText(equals,$aweSqlPredecField,$itemId),$order);
        return $result;
      }
      
      public function getChildren($itemId, $order) {
      global $aweSqlParentField;
      global $aweDataTable;
        $result = $this->aweQuery->select("",$this->moduleName.$aweDataTable,sqlText(equals,$aweSqlParentField,$itemId),$order);
        return $result;
      }
      
      public function getPredecessor($itemId) {
        global $aweDataTable;
        global $aweSqlPrimaryKey;
        global $aweSqlPredecField;
        
        $itemData = $this->aweQuery->select("",$this->moduleName.$aweDataTable, sqlText(equals, $aweSqlPrimaryKey, $itemId)); 
        $result = $this->aweQuery->select("",$this->moduleName.$aweDataTable, sqlText(equals, $aweSqlPrimaryKey, $itemData[0][$aweSqlPredecField]));
    
        if (is_array($result[0])) return $result;
        else return -1;
      }
      
      
      public function getPredecessors($itemId) {
        global $addressDelimiter;
        global $aweSqlPredecField;
        global $aweSqlPrimaryKey;
        $t=0;
          if ($itemId != 0) {
                  do {
                  $predecessor = $this->getPredecessor($itemId);
                  if ($predecessor[0][$aweSqlPrimaryKey] != 0)  {$itemId = $predecessor[0][$aweSqlPrimaryKey]; $addressTable[$t++] = $predecessor[0]; };
                  } while ($predecessor[0][$aweSqlPrimaryKey] != 0);
          }
          for ($j=0,$i=sizeof($addressTable)-1; $i>=0; $j++,$i--)
            $result[$j] = $addressTable[$i][$aweSqlPrimaryKey];
          return $result;
      }

      public function itemIsSelected($addressTable, $itemId) {
        while( list($key, $value) = each($addressTable)) 
          if (!strncmp($key, $this->navPrefix, strlen($this->navPrefix)))
            if ($value == $itemId) return true;
        return false;
      }
      
      public function itemTopSelectedLevel($currentAddress) {
        $i=1;
        while (isset($currentAddress[$this->navPrefix.$i])) $i++;
        //echo $currentAddress[$this->navPrefix.$i]."cokolwiek".$i;
        return $i-1;
      }
      
      public function itemTopSelectedId($currentAddress) {
        return $currentAddress[$this->navPrefix.$this->itemTopSelectedLevel($currentAddress)];
      }
      
      public function itemSelected($currentAddress) {
        return $currentAddress[$this->navPrefix.$this->itemTopSelectedLevel($currentAddress)];
      }
      
      protected function itemHasChildren($parent) {
        global $aweSqlPredecField;
        global $aweDataTable;
         
        $children = $this->aweQuery->select("",$this->moduleName.$aweDataTable,sqlText(equals,$aweSqlPredecField, $parent), $limit=1);

        return isset($children);
      }
      
      protected function itemIsDescendant($parentId, $itemId) {
        global $aweSqlNameField;
        global $aweSqlPrimaryKey;

        if ($this->itemHasChildren($parentId)) {
          $children = $this->getChildren($parentId, "");
          for ($i=0; $i<sizeof($children); $i++) {

            if ($children[$i][$aweSqlPrimaryKey] == $itemId) return true;
            if ($this->itemIsDescendant($children[$i][$aweSqlPrimaryKey], $itemId)) return true;
            }
          }
        return false;         
      }
      
      public function updateNewItem() {
        global $aweSqlPredecField;
        global $aweDataTable;
        global $aweSqlPositionField;
        global $aweSqlPrimaryKey;
        global $aweSqlHiddenField;
        global $aweSqlSystemField;
        global $aweSqlNameField;
        global $aweSqlLangTmp;
        global $aweSqlHiddenDefault;
        global $aweSqlSystemDefault;

        if (func_num_args() <2) return NULL; // Logger->PageMenuCreateItem->not enough parameters 
        
        $name = func_get_arg(0);
        $parent = func_get_arg(1);
        $itemId = 1 + $this->aweQuery->getMax($this->moduleName.$aweDataTable, $aweSqlPrimaryKey);
        $maxPosition = 1 + $this->aweQuery->getMax($this->moduleName.$aweDataTable, $aweSqlPositionField, sqlText(equals, $aweSqlPredecField, $parent));
        
        
        if (func_num_args() >=3) {
          $position = func_get_arg(2);
   
          if ($position >= $maxPosition) $position = $maxPosition;
          else {
            $this->aweQuery->update($this->moduleName.$aweDataTable, $aweSqlPositionField, $aweSqlPositionField." +1 ", sqlText("and",sqlText(greaterEqual, $aweSqlPositionField, $position), sqlText(equals, $aweSqlPredecField, $parent)));
              
          }
        } else $position = $maxPosition;
        
        if (func_num_args() >=4) $hidden = func_get_arg(3); else $hidden = $aweSqlHiddenDefault;
        if (func_num_args() >=5) $system = func_get_arg(4); else $system = $aweSqlSystemDefault;
        

        $this->aweQuery->insert(
                                $this->moduleName.$aweDataTable,
                                array($aweSqlPrimaryKey, $aweSqlPredecField, $aweSqlPositionField, $aweSqlHiddenField, $aweSqlSystemField, $aweSqlNameField.$aweSqlLangTmp),
                                array($itemId, $parent, $position, $hidden, $system, $name)
                                );
        
      }
      
      public function updateChangeItem() {
        global $aweSqlNameField;
        global $aweSqlPrimaryKey;
        global $aweDataTable;
        global $sqlValueAreola;
        global $aweSqlLangTmp;
        
        if (func_num_args() <2) return NULL; //:Logger->PageMenuChangeItem->not enough parameters
        $itemId = func_get_arg(0);
        $itemName = func_get_arg(1);
        
        $this->aweQuery->update(
                                $this->moduleName.$aweDataTable,
                                $aweSqlNameField.$aweSqlLangTmp, 
                                valueInQuotes($itemName, $sqlValueAreola), 
                                sqlText(equals, $aweSqlPrimaryKey, $itemId)
                                );
        
      }
      
      /// Works only if directory is empty 
      public function updateRemoveItem($itemId) {
        if (!$this->itemHasChildren($itemId)) {
            parent::updateRemoveItem($itemId);
            return true;
          }
        else
          return false;
      }
      
      public function updateForceRemove($itemId) {
      /// TODO: recurrent delete all the subdirectories and all binded items
      }
      
      public function updateChangeParent($itemId, $newParent) {
        if (!$this->itemIsDescendant($itemId,$newParent) && $itemId != $newParent) {
          parent::updateChangeParent($itemId, $newParent);
        }
      }

}
Return current item: phpAWE