public function applyDefaultEntriesSelectParams(array $params)
 {
     if ($this->primaryKeyFieldName != 'id') {
         unset(self::$defaultEntriesSelectParams['id']);
         self::$defaultEntriesSelectParams[$this->primaryKeyFieldName] = -1;
         self::$defaultEntriesSelectParams['orderBy'] = $this->primaryKeyFieldName;
     }
     if (isset($params['perPage']) && isset($params['current_page'])) {
         $params['currentPage'] = $params['current_page'] * $params['perPage'];
     }
     if (isset($params['fields']) && is_array($params['fields'])) {
         $params['fields'] = array_merge(array('id'), $params['fields']);
     }
     $params = array_merge(self::$defaultEntriesSelectParams, $params);
     // Is there a sort column?
     $tableColumns = array_flip(TableSchema::getTableColumns($this->table, null, true));
     if (array_key_exists('sort', $tableColumns)) {
         $params['orderBy'] = 'sort';
     }
     array_walk($params, array($this, 'castFloatIfNumeric'));
     return $params;
 }
 public function constructPreferences($user_id, $table, $preferences = null, $title = null)
 {
     if ($preferences) {
         $newPreferencesData = false;
         // @todo enforce non-empty set
         if (empty($preferences['columns_visible'])) {
             $newPreferencesData = true;
             $columns_visible = TableSchema::getTableColumns($table, 6);
             $preferences['columns_visible'] = implode(',', $columns_visible);
         }
         $preferencesDefaultsApplied = $this->applyDefaultPreferences($table, $preferences);
         if (count(array_diff($preferences, $preferencesDefaultsApplied))) {
             $newPreferencesData = true;
         }
         $preferences = $preferencesDefaultsApplied;
         if ($newPreferencesData) {
             $id = $this->addOrUpdateRecordByArray($preferences);
         }
         return $preferences;
     }
     $insert = new Insert($this->table);
     // User doesn't have any preferences for this table yet. Please create!
     $columns_visible = TableSchema::getTableColumns($table, 6);
     $data = array('user' => $user_id, 'columns_visible' => implode(',', $columns_visible), 'table_name' => $table, 'title' => $title);
     if (TableSchema::hasTableColumn($table, 'sort')) {
         $data['sort'] = 'sort';
     }
     $data = $this->applyDefaultPreferences($table, $data);
     $insert->values($data);
     $this->insertWith($insert);
     return $data;
 }