Example #1
0
 public static function tableOrder($field, CModel $model = null, $manualSort = false)
 {
     if (is_null($model)) {
         return "";
     }
     $labels = CCoreObjectsManager::getAttributeLabels($model);
     $columnLabels = CCoreObjectsManager::getAttributeTableLabels($model);
     if (array_key_exists($field, $columnLabels)) {
         $label = $columnLabels[$field];
     } elseif (array_key_exists($field, $labels)) {
         $label = $labels[$field];
     } else {
         $label = $field;
     }
     $exclude = array('order', 'direction');
     foreach (CRequest::getGlobalRequestVariables()->getItems() as $key => $value) {
         if (!in_array($key, $exclude)) {
             if (is_scalar($value)) {
                 $actions[] = $key . "=" . $value;
             }
         }
     }
     /**
      * Позволяем сортировать только если поле есть в модели
      */
     $showLink = $manualSort;
     if (is_a($model, "CActiveModel")) {
         if ($model->getDbTableFields()->hasElement($field)) {
             $showLink = true;
         }
     }
     $actions[] = "order=" . $field;
     if (CRequest::getString("order") == $field) {
         if (CRequest::getString("direction") == "") {
             $actions[] = "direction=asc";
         } elseif (CRequest::getString("direction") == "asc") {
             $actions[] = "direction=desc";
         } elseif (CRequest::getString("direction") == "desc") {
             $actions[] = "direction=asc";
         }
     } else {
         $actions[] = "direction=desc";
     }
     if ($showLink) {
         $label = '<a href="?' . implode($actions, "&") . '">' . $label . '</a>';
     }
     echo $label;
 }