Ejemplo n.º 1
0
 /**
  * Получение данных из базы
  * @return array
  */
 public function fetchData()
 {
     if (!$this->is_used_fetch) {
         $this->is_used_fetch = true;
         $select = new Utils\SqlParser\Select($this->sql);
         if (!empty($this->search) && !empty($this->sessData['search'])) {
             foreach ($this->sessData['search'] as $key => $search_value) {
                 $search_column = $this->search[$key];
                 if ($search_column instanceof Search) {
                     $search_field = $search_column->getField();
                     switch ($search_column->getType()) {
                         case 'date':
                         case 'datetime':
                             if (!empty($search_value[0]) && empty($search_value[1])) {
                                 $quoted_value = $this->db->quote($search_value[0]);
                                 $select->addWhere("{$search_field} >= {$quoted_value}");
                             } elseif (empty($search_value[0]) && !empty($search_value[1])) {
                                 $quoted_value = $this->db->quote($search_value[1]);
                                 $select->addWhere("{$search_field} <= {$quoted_value}");
                             } elseif (!empty($search_value[0]) && !empty($search_value[1])) {
                                 $quoted_value1 = $this->db->quote($search_value[0]);
                                 $quoted_value2 = $this->db->quote($search_value[1]);
                                 $select->addWhere("{$search_field} BETWEEN {$quoted_value1} AND {$quoted_value2}");
                             }
                             break;
                         case 'select':
                             if ($search_value != '') {
                                 $quoted_value = $this->db->quote($search_value);
                                 $select->addWhere("{$search_field} = {$quoted_value}");
                             }
                             break;
                         case 'multiselect':
                             if (!empty($search_value)) {
                                 $quoted_value = $this->db->quote($search_value);
                                 $select->addWhere("{$search_field} IN ({$quoted_value})");
                             }
                             break;
                         case 'text':
                             if ($search_value != '') {
                                 $quoted_value = $this->db->quote('%' . $search_value . '%');
                                 $select->addWhere("{$search_field} LIKE {$quoted_value}");
                             }
                             break;
                         case 'radio':
                             if ($search_value != '') {
                                 $quoted_value = $this->db->quote($search_value);
                                 $select->addWhere("{$search_field} = {$quoted_value}");
                             }
                             break;
                         case 'checkbox':
                             if (!empty($search_value)) {
                                 $quoted_value = $this->db->quote($search_value);
                                 $select->addWhere("{$search_field} IN ({$quoted_value})");
                             }
                             break;
                     }
                 }
             }
         }
         if (!empty($this->sessData['order'])) {
             $select->setOrderBy($this->sessData['order'] + 1 . ' ' . $this->sessData['order_type']);
         }
         if (!empty($this->current_page)) {
             if ($this->current_page == 1) {
                 $select->setLimit($this->records_per_page);
             } elseif ($this->current_page > 1) {
                 $offset = ($this->current_page - 1) * $this->records_per_page;
                 $select->setLimit($this->records_per_page, $offset);
             }
         }
         if (!$this->table) {
             $this->setTable($select->getTable());
         }
         $sql = $select->getSql();
         if ($this->round_record_count) {
             $explain = $this->db->fetchAll('EXPLAIN ' . $sql, $this->sql_params);
             $this->record_count = 0;
             foreach ($explain as $value) {
                 if ($value['rows'] > $this->record_count) {
                     $this->record_count = $value['rows'];
                 }
             }
             $result = $this->db->fetchAll($sql, $this->sql_params);
         } else {
             $result = $this->db->fetchAll("SELECT SQL_CALC_FOUND_ROWS " . substr(trim($sql), 6), $this->sql_params);
             $this->record_count = $this->db->fetchOne("SELECT FOUND_ROWS()");
         }
         if (!empty($result)) {
             foreach ($result as $key => $row) {
                 $this->data[$key] = new Row($row);
             }
         }
     }
     return $this->data;
 }
Ejemplo n.º 2
0
 /**
  * @return string
  */
 public function render()
 {
     if (empty($this->table)) {
         $select = new Utils\SqlParser\Select($this->query);
         $this->setTable($select->getTable());
     }
     if (empty($this->primary_key)) {
         $quoted_table = $this->db->quoteIdentifier($this->table);
         $index = $this->db->fetchRow("SHOW INDEX FROM {$quoted_table} where Key_name = 'PRIMARY'");
         $primary_key = !empty($index['Column_name']) ? $index['Column_name'] : '';
         $this->setPrimaryKey($primary_key, '');
     }
     $data = $this->fetchData();
     $token = sha1(uniqid());
     $this->setSessData('__csrf_token', $token);
     $this->attributes['data-csrf-token'] = $token;
     $this->attributes['data-resource'] = $this->resource;
     $attributes = array();
     if ($this->ajax_request && !isset($this->attributes['onsubmit'])) {
         $this->attributes['onsubmit'] = 'return combine.form.submit(this);';
     }
     if (!empty($this->attributes)) {
         foreach ($this->attributes as $attr_name => $value) {
             $attributes[] = "{$attr_name}=\"{$value}\"";
         }
     }
     if (!empty($this->positions)) {
         $template = $this->template;
         foreach ($this->positions as $name => $position) {
             $controls_html = '';
             if (!empty($position['controls'])) {
                 foreach ($position['controls'] as $control) {
                     if ($control instanceof Control) {
                         if ($control instanceof Control\Text || $control instanceof Control\Number || $control instanceof Control\Date || $control instanceof Control\Datetime || $control instanceof Control\Hidden || $control instanceof Control\Email || $control instanceof Control\Password) {
                             if (isset($data[$control->getAttr('name')])) {
                                 $control->setAttr('value', $data[$control->getAttr('name')]);
                             }
                         } elseif ($control instanceof Control\Textarea || $control instanceof Control\Wysiwyg || $control instanceof Control\Markdown) {
                             if (isset($data[$control->getAttr('name')])) {
                                 $control->setValue($data[$control->getAttr('name')]);
                             }
                         } elseif ($control instanceof Control\Select) {
                             if (isset($data[$control->getAttr('name')])) {
                                 $explode_value = explode(',', $data[$control->getAttr('name')]);
                                 $control->setSelected($explode_value);
                             }
                         } elseif ($control instanceof Control\Checkbox || $control instanceof Control\Radio) {
                             if (isset($data[$control->getAttr('name')])) {
                                 $explode_value = explode(',', $data[$control->getAttr('name')]);
                                 $control->setChecked($explode_value);
                             }
                         }
                         $controls_html .= $control->render();
                     }
                 }
             }
             $buttons_html = '';
             if (!empty($position['buttons'])) {
                 $buttons_controls = array();
                 foreach ($position['buttons'] as $button) {
                     if ($button instanceof Button) {
                         if ($button instanceof Button\Switched && isset($data[$button->getAttr('name')])) {
                             $button->setAttr('value', $data[$button->getAttr('name')]);
                         }
                         $buttons_controls[] = $button->render();
                     }
                 }
                 $buttons_wrapper = $this->buttons_wrapper !== null ? $this->buttons_wrapper : file_get_contents($this->theme_location . '/html/form/wrappers/button.html');
                 $buttons_html = str_replace('[BUTTONS]', implode(' ', $buttons_controls), $buttons_wrapper);
             }
             $template = str_replace("[{$name}]", $controls_html . $buttons_html, $template);
         }
     } else {
         $template = '';
     }
     // Скрипты
     $scripts_js = array();
     $main_js = "{$this->theme_src}/js/form.js?theme_src={$this->theme_src}";
     if (!isset(self::$scripts_js[$main_js])) {
         self::$scripts_js[$main_js] = false;
         $scripts_js[] = "<script src=\"{$main_js}\"></script>";
     }
     // Стили
     $scripts_css = array();
     $main_css = "{$this->theme_src}/css/form.css";
     if (!isset(self::$scripts_css[$main_css])) {
         self::$scripts_css[$main_css] = false;
         $scripts_css[] = "<link href=\"{$main_css}\" rel=\"stylesheet\"/>";
     }
     $form = file_get_contents($this->theme_location . '/html/form.html');
     $form = str_replace('[ATTRIBUTES]', implode(' ', $attributes), $form);
     $form = str_replace('[CONTROLS]', $template, $form);
     $form = str_replace('[RESOURCE]', $this->resource, $form);
     $form = str_replace('[CSS]', implode('', $scripts_css), $form);
     $form = str_replace('[JS]', implode('', $scripts_js), $form);
     return $form;
 }