Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getLinkedList($listData){
- $sql = Yii::app()->db->createCommand($listData['sqlString']);
- $sqlData = array('childs' => array());
- foreach ($sql->queryAll(true,$listData['sqlParams']) as $row) {
- $i = -1;
- $parents = array();
- foreach ($listData['lists'] as $list){
- $i++;
- $value = $row[$list['columnValue']];
- $name = $row[$list['columnName']];
- $sort = $row[$list['columnSort']];
- if ($i==0){
- $parent = &$sqlData['childs'];
- $parents[] = &$parent;
- }
- $newparents = array();
- foreach ($parents as &$parent){
- if ($list['includeEmptyValue']){
- if (!isset($parent[''])){
- $parent[''] = array(
- 'value' => '',
- 'name' => $list['nameForEmptyValue'],
- 'sort' => '',
- 'childs' => array(),
- );
- }
- $newparents[] = &$parent['']['childs'];
- }
- if (!isset($parent[$value])){
- $parent[$value] = array(
- 'value' => $value,
- 'name' => $name,
- 'sort' => $sort,
- 'childs' => array(),
- );
- }
- $newparents[] = &$parent[$value]['childs'];
- }
- $parents = $newparents;
- }
- }
- $i=-1;
- $listsResult = array();
- foreach ($listData['lists'] as $list){
- $i++;
- if ($i==0){
- $parent = &$sqlData['childs'];
- }
- $listSort = array();
- foreach ($parent as $child){
- if ($child['value']!='')
- $listSort[$child['value']] = $child['sort'];
- }
- if ($list['sortDirection'] == 'asc')
- asort($listSort);
- else if ($list['sortDirection'] == 'desc')
- arsort($listSort);
- $listsResult[$list['listName']] = array();
- $listResult = &$listsResult[$list['listName']];
- $listResult['data'] = array();
- if ($list['includeEmptyValue']){
- $listResult['data'][''] = $list['nameForEmptyValue'];
- }
- $defaultValue = $list['defaultValue'];
- if ($defaultValue == null)
- $defaultValue = '';
- foreach ($listSort as $value => $name){
- $listResult['data'][$value] = $parent[$value]['name'];
- if ($value == $defaultValue)
- $listResult['selectedValue'] = $value;
- }
- if (!isset($listResult['selectedValue'])){
- if (sizeof($listResult['data']) > 0){
- reset($listResult['data']);
- $listResult['selectedValue'] = key($listResult['data']);
- }
- else
- $listResult['selectedValue'] = '';
- }
- if (sizeOf($sqlData['childs']) > 0)
- $parent = &$parent[$listResult['selectedValue']]['childs'];
- else
- $parent = &$sqlData['childs'];
- }
- return $listsResult;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement