示例#1
0
 function fetch_record($sql, $filter = null, $filter_msg = '', $reset = false)
 {
     debug_mode(__FILE__, __LINE__, $sql, 'function fetch_record');
     if (preg_match("/.group by/i", $sql) > 0) {
         $is_group_by = true;
     } else {
         $is_group_by = false;
     }
     debug_mode(__FILE__, __LINE__, $is_group_by, 'is_group_by');
     $val_ = new OrSysvalue();
     $val_msg = $val_->message;
     $db_event = $val_->db_event;
     $val_compare = $val_->compare;
     /* ตรวจสอบการกดปุ่มเพิ่มรายการใหม่ */
     /* if($val_msg[btn_new] == 'New' ){
        header("Location:".$this->OP_[edit_page_url]->get() . '?evt_form_db[navigator]=New');
        } */
     foreach ($this->controls as $control_id => $control) {
         if ($control->OP_[db_field]->get()) {
             $this->caption_fields[$control->OP_[caption]->get()] = $control_id;
             debug_mode(__FILE__, __LINE__, $control->OP_[caption]->get(), 'caption');
         }
     }
     /**
      * ตรวจสอบ mode การทำงาน ถ้าเป็น Query ไม่ต้องดึงข้อมูล
      * */
     if ($db_event[on_load] == '' and $this->OP_[default_mode]->get() == 'query' and $val_msg[btn_filter] != 'Filter' or $val_msg[btn_query] == 'Query') {
         $this->form_mode = 'query';
     } else {
         /**
          * ตรวจสอบการคัดกรองข้อมูล ตามมาตรฐาน
          * */
         if (is_null($filter)) {
             $filter = new OrSql();
         }
         /**
          * ตรวจสอบคำสั่ง Group By
          */
         if (preg_match("/.group by/i", $sql) > 0) {
             $is_group_by = true;
         } else {
             $is_group_by = false;
         }
         if ($val_msg[btn_filter] == 'Filter') {
             if ($this->filter_value[filter_by] != '') {
                 $filter_msg .= 'ค้นหา ' . $this->filter_value[filter_by] . ' <i>ทุกข้อมูล</i> ';
                 $new_filter = true;
                 //foreach ($this->caption_fields AS $caption => $id) {
                 foreach ($this->controls as $control_id => $control) {
                     /**
                      * ตรวจสอบค่าประเภทฟิลด์ข้อมูลต้องเป็นข้อความเท่านั้น จึงจะนำมาคัดข้อมูล
                      */
                     if ($control->OP_[db_field]->get() and $control->OP_[db_type]->get() == 'text') {
                         $id = $control_id;
                         if ($is_group_by) {
                             if ($this->is_group_filter($this->get_filter_name($id))) {
                                 debug_mode(__FILE__, __LINE__, $this->get_filter_name($id), 'filter_name');
                                 $filter->set_cmd_group_filter($this->get_filter_name($id), 'LIKE', $this->filter_value[filter_by], 'OR');
                                 //$filter_msg .= ' ' . $caption . ' ';
                             } else {
                                 debug_mode(__FILE__, __LINE__, $this->get_filter_name($id), 'filter_name');
                                 /**
                                  * ยกเว้นการค้นหาในกรณี Group By
                                  */
                                 //$filter->set_cmd_filter($this->get_filter_name($id), 'LIKE', $this->filter_value[filter_by], 'OR');
                             }
                         } else {
                             $filter->set_cmd_filter($this->get_filter_name($id), 'LIKE', $this->filter_value[filter_by], 'OR', $new_filter);
                             //$filter_msg .= ' ' . $caption . ' ';
                         }
                         $new_filter = false;
                     }
                 }
             }
             /*                 * ค้นจาก Query* */
             if (is_array($this->filter_value)) {
                 $filter_msg .= ' จาก ';
                 $new_filter = true;
                 foreach ($this->filter_value as $id => $value) {
                     if ($id != 'filter_by') {
                         $EV_[EV_field_name] = $id;
                         $EV_[EV_field_value] = $value;
                         $this->OE_set_filter($EV_);
                         if ($value != '') {
                             debug_mode(__FILE__, __LINE__, $this->filter_value[$id], 'Filter_value ' . $id);
                             switch ($val_compare[$id]) {
                                 case 'BETWEEN':
                                     if ($val_msg[$id . '_II'] != '') {
                                         $value = $value . ' - ' . $val_msg[$id . '_II'];
                                     }
                                     break;
                                 case '':
                                     $val_compare[$id] = '=';
                                 default:
                             }
                             /* if ($val_compare[$id] == '') {
                                $val_compare[$id] = 'LIKE';
                                } */
                             if ($is_group_by) {
                                 if ($this->is_group_filter($this->get_filter_name($id))) {
                                     $filter->set_cmd_group_filter($this->get_filter_name($id), $val_compare[$id], $value);
                                     $filter_msg .= ' ' . $this->controls[$id]->OP_[caption]->get() . ' ' . $val_compare[$id] . ' ' . $value;
                                 } else {
                                     $filter->set_cmd_filter($this->get_filter_name($id), $val_compare[$id], $value);
                                     $filter_msg .= ' ' . $this->controls[$id]->OP_[caption]->get() . ' ' . $val_compare[$id] . ' ' . $value;
                                 }
                             } else {
                                 $filter->set_cmd_filter($this->get_filter_name($id), $val_compare[$id], $value, 'AND', $new_filter);
                                 $filter_msg .= ' ' . $this->controls[$id]->OP_[caption]->get() . ' ' . $val_compare[$id] . ' ' . $value;
                             }
                             $new_filter = false;
                         }
                     }
                 }
             }
             if ($val_msg['order_by'] != '') {
                 $sort_field = $val_msg['order_by'];
                 $filter_msg .= ' เรียงตาม ';
                 foreach ($this->caption_fields as $caption => $id) {
                     if ($val_msg['order_by'] == $id) {
                         $filter->set_cmd_order($id);
                         $filter_msg .= ' ' . $caption . ' ';
                     }
                 }
             }
         } else {
             if ($val_msg[btn_filter] == 'No Filter') {
                 $reset = true;
             }
         }
         parent::fetch_record($sql, $filter, $filter_msg, $reset);
     }
     return null;
 }