Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.33 KB | None | 0 0
  1. function getLinkedList($listData){    
  2.     $sql = Yii::app()->db->createCommand($listData['sqlString']);
  3.     $sqlData = array('childs'   =>  array());    
  4.     foreach ($sql->queryAll(true,$listData['sqlParams']) as $row) {
  5.         $i = -1;
  6.         $parents = array();
  7.         foreach ($listData['lists'] as $list){
  8.             $i++;
  9.             $value = $row[$list['columnValue']];
  10.             $name = $row[$list['columnName']];
  11.             $sort = $row[$list['columnSort']];
  12.             if ($i==0){
  13.                 $parent = &$sqlData['childs'];
  14.                 $parents[] = &$parent;
  15.             }
  16.             $newparents = array();
  17.             foreach ($parents as &$parent){                
  18.                 if ($list['includeEmptyValue']){
  19.                     if (!isset($parent[''])){
  20.                         $parent[''] = array(
  21.                             'value'     =>  '',
  22.                             'name'      =>  $list['nameForEmptyValue'],
  23.                             'sort'      =>  '',
  24.                             'childs'    =>  array(),
  25.                         );
  26.                     }
  27.                     $newparents[] = &$parent['']['childs'];                    
  28.                 }
  29.                 if (!isset($parent[$value])){
  30.                     $parent[$value] = array(
  31.                         'value'     =>  $value,
  32.                         'name'      =>  $name,
  33.                         'sort'      =>  $sort,
  34.                         'childs'    =>  array(),
  35.                     );
  36.                 }
  37.                 $newparents[] = &$parent[$value]['childs'];
  38.             }
  39.             $parents = $newparents;
  40.         }
  41.     }
  42.     $i=-1;
  43.     $listsResult = array();    
  44.     foreach ($listData['lists'] as $list){
  45.         $i++;
  46.         if ($i==0){
  47.             $parent = &$sqlData['childs'];            
  48.         }        
  49.         $listSort = array();        
  50.         foreach ($parent as $child){
  51.             if ($child['value']!='')
  52.                 $listSort[$child['value']] = $child['sort'];
  53.         }
  54.         if ($list['sortDirection'] == 'asc')
  55.             asort($listSort);
  56.         else if ($list['sortDirection'] == 'desc')
  57.             arsort($listSort);
  58.         $listsResult[$list['listName']] = array();
  59.         $listResult = &$listsResult[$list['listName']];
  60.         $listResult['data'] = array();
  61.         if ($list['includeEmptyValue']){
  62.             $listResult['data'][''] = $list['nameForEmptyValue'];
  63.         }
  64.         $defaultValue = $list['defaultValue'];
  65.         if ($defaultValue == null)
  66.             $defaultValue = '';        
  67.         foreach ($listSort as $value => $name){            
  68.             $listResult['data'][$value] = $parent[$value]['name'];
  69.             if ($value == $defaultValue)
  70.                 $listResult['selectedValue'] = $value;
  71.         }
  72.         if (!isset($listResult['selectedValue'])){
  73.             if (sizeof($listResult['data']) > 0){
  74.                 reset($listResult['data']);
  75.                 $listResult['selectedValue'] = key($listResult['data']);
  76.             }
  77.             else
  78.                 $listResult['selectedValue'] = '';
  79.         }
  80.         if (sizeOf($sqlData['childs']) > 0)
  81.             $parent = &$parent[$listResult['selectedValue']]['childs'];
  82.         else
  83.             $parent = &$sqlData['childs'];
  84.     }    
  85.     return $listsResult;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement