# e2pdf/1.02.02/classes/model/e2pdf-element.php

E2Pdf – Export Pdf Tool for WordPress, version 1.02.02. 153 lines.

- Page: https://pluginprobe.com/plugins/e2pdf/1.02.02/code/classes/model/e2pdf-element.php
- Raw: https://pluginprobe.com/plugins/e2pdf/1.02.02/raw/classes/model/e2pdf-element.php
- Modified: 2018-12-07T12:07:02+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/e2pdf/1.02.02/code/classes/model/e2pdf-element.php#L10-L20`.

```php
<?php

/**
 * E2pdf Template Model
 * 
 * @copyright  Copyright 2017 https://e2pdf.com
 * @license    GPL v2
 * @version    1
 * @link       https://e2pdf.com
 * @since      0.00.01
 */
if (!defined('ABSPATH')) {
    die('Access denied.');
}

class Model_E2pdf_Element extends Model_E2pdf_Model {

    private $element = array();
    private $table;

    function __construct() {
        global $wpdb;
        parent::__construct();
        $this->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();
    }

}

```
