Example #1
0
 /**
  * Construct the column model of the grid. The model can be passed as array
  * or can be constructed from sql. See _setSQL() to determine which SQL is
  * used. The method try to determine the primary key and if it is found is
  * set as key:true to the appropriate field. If the primary key can not be
  * determined set the first field as key:true in the colModel.
  * Return true on success.
  * @see _setSQL
  * @param array $model if set construct the model ignoring the SQL command
  * @param array $params if a sql command is used parametters passed to the SQL
  * @param array $labels if this parameter is set it set the labels in colModel.
  * The array should be associative which key value correspond to the name of
  * colModel
  * @return boolean
  */
 public function setColModel(array $model = null, array $params = null, array $labels = null)
 {
     $goper = $this->oper ? $this->oper : 'nooper';
     // excel, nooper, !(in_array....)
     if ($goper == 'nooper' || $goper == $this->GridParams["excel"] || $goper == "pdf" || $goper == "csv") {
         $runme = true;
     } else {
         $runme = !in_array($goper, array_values($this->GridParams));
     }
     if ($runme) {
         if (is_array($model) && count($model) > 0) {
             $this->colModel = $model;
             return true;
         }
         $sql = null;
         $sqlId = $this->_setSQL();
         if (!$sqlId) {
             return false;
         }
         $nof = $this->dbtype == 'sqlite' || $this->dbtype == 'db2' || $this->dbtype == 'array' || $this->dbtype == 'mongodb' ? 1 : 0;
         //$sql = $this->parseSql($sqlId, $params);
         $ret = $this->execute($sqlId, $params, $sql, true, $nof, 0);
         //$this->execute($sqlId, $params, $sql, $limit, $nrows, $offset)
         if ($ret) {
             if (is_array($labels) && count($labels) > 0) {
                 $names = true;
             } else {
                 $names = false;
             }
             $colcount = jqGridDB::columnCount($sql);
             for ($i = 0; $i < $colcount; $i++) {
                 $meta = jqGridDB::getColumnMeta($i, $sql);
                 if (strtolower($meta['name']) == 'jqgrid_row') {
                     continue;
                 }
                 //Oracle, IBM DB2
                 if ($names && array_key_exists($meta['name'], $labels)) {
                     $this->colModel[] = array('label' => $labels[$meta['name']], 'name' => $meta['name'], 'index' => $meta['name'], 'sorttype' => jqGridDB::MetaType($meta, $this->dbtype));
                 } else {
                     $this->colModel[] = array('name' => $meta['name'], 'index' => $meta['name'], 'sorttype' => jqGridDB::MetaType($meta, $this->dbtype));
                 }
             }
             jqGridDB::closeCursor($sql);
             if ($this->primaryKey) {
                 $pk = $this->primaryKey;
             } else {
                 $pk = jqGridDB::getPrimaryKey($this->table, $this->pdo, $this->dbtype);
                 $this->primaryKey = $pk;
             }
             if ($pk) {
                 $this->setColProperty($pk, array("key" => true));
             } else {
                 $this->colModel[0] = array_merge($this->colModel[0], array("key" => true));
             }
         } else {
             $this->errorMessage = jqGridDB::errorMessage($sql);
             if ($this->showError) {
                 $this->sendErrorHeader();
             }
             return $ret;
         }
     }
     if ($goper == $this->GridParams["excel"]) {
         // notify all other set methods not to be executed
         $this->runSetCommands = false;
     } else {
         if (!$runme) {
             $this->runSetCommands = false;
         }
     }
     return true;
 }