table = $wpdb->prefix . 'e2pdf_elements'; } public function get_table() { return $this->table; } public function set($key, $value) { $this->element[$key] = $value; } public function get($key) { if (isset($this->element[$key])) { $value = $this->element[$key]; return $value; } else { switch ($key) { case 'top': case 'left': case 'width': case 'height': case 'page_id': case 'template_id': $value = 0; break; case 'properties': case 'actions': $value = array(); break; default: $value = ''; break; } return $value; } } public function get_element() { return $this->element; } public function before_save() { $element = array( 'type' => $this->get('type'), 'page_id' => $this->get('page_id'), 'template_id' => $this->get('template_id'), 'element_id' => $this->get('element_id'), 'top' => $this->get('top'), 'left' => $this->get('left'), 'width' => $this->get('width'), 'height' => $this->get('height'), 'value' => $this->get('value'), 'properties' => serialize($this->get('properties')), 'actions' => serialize($this->get('actions')), ); return $element; } public function save() { global $wpdb; $element = $this->before_save(); if ($this->get('ID')) { $where = array( 'ID' => $this->get('ID') ); $wpdb->update($this->get_table(), $element, $where); return $this->get('ID'); } else { $wpdb->insert($this->get_table(), $element); $this->set('ID', $wpdb->insert_id); } } public function load($element_id) { global $wpdb; $element = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE ID = %d", $element_id), ARRAY_A); if ($element) { $this->element = $element; $this->set('properties', unserialize($element['properties'])); $this->set('actions', unserialize($element['actions'])); return true; } return false; } public function get_last_element_id($template_id) { global $wpdb; $element = false; if ($template_id) { $element = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE template_id = %d ORDER BY element_id DESC", $template_id), ARRAY_A); } if ($element) { return $element['element_id']; } else { return 0; } } public function delete() { global $wpdb; if ($this->get('ID')) { $where = array( 'ID' => $this->get('ID') ); $wpdb->delete($this->get_table(), $where); } } public function get_elements($page_id) { global $wpdb; $elements = $wpdb->get_results($wpdb->prepare("SELECT * FROM `{$this->get_table()}` WHERE page_id = %d", $page_id), ARRAY_A); if ($elements) { $elements_list = array(); foreach ($elements as $element_key => $element) { $this->load($element['ID']); $elements_list[] = $this->get_element(); } return $elements_list; } return array(); } }