Example #1
0
 /**
  * @param  array $data
  * @throws Exception
  */
 protected function deleteData($data)
 {
     if (!$this->isValidRequest('table')) {
         throw new Exception('Not valid request');
     }
     $session = new SessionNamespace($this->resource);
     if ($_SERVER['REQUEST_METHOD'] !== 'DELETE') {
         throw new Exception('Invalid request method, need DELETE');
     } elseif (empty($data['id_rows']) || !is_array($data['id_rows'])) {
         throw new Exception('Records id not valid');
     } elseif (empty($session->db) || empty($session->db->table)) {
         throw new Exception('No set table');
     } elseif (empty($session->access) || empty($session->access->delete) || !$session->access->delete) {
         throw new Exception('Access denied');
     }
     $table = $session->db->table;
     $primary_key = !empty($session->db->primary_id) ? $session->db->primary_id : 'id';
     $primary_key = $this->db->quoteIdentifier($primary_key);
     $where = $this->db->quoteInto("{$primary_key} IN(?)", $data['id_rows']);
     $is_delete = $this->db->delete($table, $where);
     if (!$is_delete) {
         throw new Exception('Error delete data');
     }
 }
Example #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;
 }