예제 #1
0
 public function loadRecords(array $params)
 {
     $config = $this->getConfig();
     $records = array();
     $rows = array();
     $iTotalRecords = 0;
     $iFilteredTotal = 0;
     $sOrder = "";
     // Get the common information in order to read the records and the columns of the list
     $columns = !empty($config['datagrid']['columns']) ? $config['datagrid']['columns'] : null;
     $dq = !empty($config['datagrid']['dqrecordset']) && is_object($config['datagrid']['dqrecordset']) ? $config['datagrid']['dqrecordset'] : null;
     $recordset = !empty($config['datagrid']['recordset']) ? $config['datagrid']['recordset'] : null;
     // Check if the doctrine object is active
     if (!empty($dq) && is_object($dq)) {
         $iTotalRecords = $dq->count();
         $iFilteredTotal = $dq->count();
         $mainsearchvalue = !empty($params['sSearch']) ? $params['sSearch'] : null;
         if (!empty($params['iColumns'])) {
             // Filter the records per each column field
             for ($i = 0; $i < intval($params['iColumns']); $i++) {
                 if ($params['bSearchable_' . $i] == "true") {
                     $colsearchvalue = !empty($params['sSearch_' . $i]) ? $params['sSearch_' . $i] : null;
                     if (!empty($columns[$i]['field'])) {
                         if ($mainsearchvalue) {
                             if (Shineisp_Commons_Utilities::is_valid_date($mainsearchvalue)) {
                                 $mainsearchvalue = Shineisp_Commons_Utilities::formatDateIn($mainsearchvalue);
                                 $dq->orWhere($columns[$i]['field'] . " = ?", $mainsearchvalue);
                             } else {
                                 $dq->orWhere($columns[$i]['field'] . " like ?", "%{$mainsearchvalue}%");
                             }
                         } else {
                             if ($colsearchvalue) {
                                 if (Shineisp_Commons_Utilities::is_valid_date($colsearchvalue)) {
                                     $colsearchvalue = Shineisp_Commons_Utilities::formatDateIn($colsearchvalue);
                                     $dq->andWhere($columns[$i]['field'] . " = ?", $colsearchvalue);
                                 } else {
                                     $dq->andWhere($columns[$i]['field'] . " like ?", "%{$colsearchvalue}%");
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $query = $dq->getSqlQuery();
         // Count the filtered records
         $temprs = $dq->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
         $iFilteredTotal = count($temprs);
         // Paging of the records
         if (isset($params['iDisplayStart']) && $params['iDisplayLength'] != '-1') {
             $dq->offset($params['iDisplayStart']);
             $dq->limit($params['iDisplayLength']);
         }
         // Sorting of the records
         if (isset($params['iSortCol_0'])) {
             for ($i = 0; $i < intval($params['iSortingCols']); $i++) {
                 $j = 0;
                 if ($params['bSortable_' . intval($params['iSortCol_' . $i])] == "true") {
                     foreach ($columns as $column) {
                         if (!empty($column['sortable']) && $column['sortable']) {
                             if ($j == $params['iSortCol_0'] - 1) {
                                 $sOrder .= $column['field'] . " " . $params['sSortDir_' . $i] . ", ";
                             }
                             $j++;
                         }
                     }
                 }
             }
             $sOrder = substr_replace($sOrder, "", -2);
             if (!empty($sOrder)) {
                 $dq->orderBy($sOrder);
             }
         }
         #print_r($columns);
         #Zend_Debug::dump($sOrder);
         #print_r($dq->getDql());
         #Zend_Debug::dump($rs);
         #die;
         // Execute the doctrine object to get the record array
         $rs = $dq->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
         // Complete the recordset with external values
         $rs = self::injectData($rs);
     } elseif (!empty($recordset) && is_array($recordset)) {
         $iFilteredTotal = count($recordset);
         $rs = $recordset;
     }
     $i = 0;
     // For each record do ...
     foreach ($rs as $record) {
         $row = array();
         $row['Row'] = "row_{$i}";
         foreach ($columns as $column) {
             if (!empty($column['alias'])) {
                 if (!empty($column['type'])) {
                     if ($column['type'] == "boolean") {
                         $badge = $record[$column['alias']] == 1 ? "badge-success" : "badge-important";
                         $label = $record[$column['alias']] == 1 ? $this->translator->translate("Yes") : $this->translator->translate("No");
                         $row[] = "<span class=\"badge {$badge}\">" . $label . "</span>";
                     } elseif ($column['type'] == "index") {
                         $badge = "badge-" . strtolower($record[$column['alias']]);
                         $label = $this->translator->translate($record[$column['alias']]);
                         $row[] = "<span class=\"badge {$badge}\">{$label}</span>";
                     } elseif ($column['type'] == "link" && !empty($column['link'])) {
                         if (is_array($column['link'])) {
                             $link = str_replace("%s", $record[$column['link']['idx']], $column['link']['href']);
                         } else {
                             $link = str_replace("%s", "", $column['link']);
                         }
                         $label = $this->translator->translate($record[$column['alias']]);
                         $row[] = "<a href=\"{$link}\" class=\"\">{$label}</span>";
                     } else {
                         $row[] = $record[$column['alias']];
                     }
                 } else {
                     $row[] = $record[$column['alias']];
                 }
             }
         }
         $records[] = $row;
         $i++;
     }
     $output = array("sEcho" => !empty($params['sEcho']) ? intval($params['sEcho']) : 0, "iTotalRecords" => $iTotalRecords, "iTotalDisplayRecords" => $iFilteredTotal, "aaData" => $records, "query" => $query);
     die(json_encode($output));
     return $this;
 }