aboutsummaryrefslogtreecommitdiffstats
path: root/public/admin/model/extension/openbay/ebay_profile.php
blob: 0099039c1fac0a9f874f48c55f9c13645e80a593 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
class ModelExtensionOpenBayEbayProfile extends Model{
	public function add($data) {
		if ($data['default'] == 1) {
			$this->clearDefault($data['type']);
		}

		$this->db->query("INSERT INTO `" . DB_PREFIX . "ebay_profile` SET `name` = '" . $this->db->escape($data['name']) . "', `description` = '" . $this->db->escape($data['description']) . "', `type` = '" . (int)$data['type'] . "', `default` = '" . (int)$data['default'] . "', `data` = '" . $this->db->escape(serialize($data['data'])) . "'");

		return $this->db->getLastId();
	}

	public function edit($id, $data) {
		if ($data['default'] == 1) {
			$this->clearDefault($data['type']);
		}

		$this->db->query("UPDATE `" . DB_PREFIX . "ebay_profile` SET `name` = '" . $this->db->escape($data['name']) . "', `description` = '" . $this->db->escape($data['description']) . "', `data` = '" . $this->db->escape(serialize($data['data'])) . "', `default` = '" . (int)$data['default'] . "' WHERE `ebay_profile_id` = '" . (int)$id . "' LIMIT 1");
	}

	public function delete($id) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "ebay_profile` WHERE `ebay_profile_id` = '" . (int)$id . "' LIMIT 1");

		if ($this->db->countAffected() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public function get($id) {
		$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "ebay_profile` WHERE `ebay_profile_id` = '" . (int)$id . "' LIMIT 1");

		if ($qry->num_rows) {
			$row                = $qry->row;
			$row['link_edit']   = $this->url->link('extension/openbay/ebay_profile/edit', 'user_token=' . $this->session->data['user_token'] . '&ebay_profile_id=' . $row['ebay_profile_id'], true);
			$row['link_delete'] = $this->url->link('extension/openbay/ebay_profile/delete', 'user_token=' . $this->session->data['user_token'] . '&ebay_profile_id=' . $row['ebay_profile_id'], true);
			$row['data']        = unserialize($row['data']);

			return $row;
		} else {
			return false;
		}
	}

	public function getAll($type = '') {
		$sql = "SELECT * FROM `" . DB_PREFIX . "ebay_profile`";

		if ($type !== '') {
			$sql .= " WHERE `type` = '" . (int)$type . "'";
		}

		$qry = $this->db->query($sql);

		if ($qry->num_rows) {
			$profiles = array();
			foreach ($qry->rows as $row) {
				$row['link_edit']   = $this->url->link('extension/openbay/ebay_profile/edit', 'user_token=' . $this->session->data['user_token'] . '&ebay_profile_id=' . $row['ebay_profile_id'], true);
				$row['link_delete'] = $this->url->link('extension/openbay/ebay_profile/delete', 'user_token=' . $this->session->data['user_token'] . '&ebay_profile_id=' . $row['ebay_profile_id'], true);
				$row['data']        = !empty($row['data']) ? unserialize($row['data']) : array();
				$profiles[]         = $row;
			}

			return $profiles;
		} else {
			return false;
		}
	}

	public function getTypes() {
		$types = array(
			0 => array(
				'name'          => $this->language->get('text_type_shipping'),
				'template'      => 'extension/openbay/ebay_profile_form_shipping'
			),
			1 => array(
				'name'          => $this->language->get('text_type_returns'),
				'template'      => 'extension/openbay/ebay_profile_form_returns'
			),
			2 => array(
				'name'          => $this->language->get('text_type_template'),
				'template'      => 'extension/openbay/ebay_profile_form_template'
			),
			3 => array(
				'name'          => $this->language->get('text_type_general'),
				'template'      => 'extension/openbay/ebay_profile_form_generic'
			)
		);

		return $types;
	}

	public function getDefault($type) {
		$qry = $this->db->query("SELECT `ebay_profile_id` FROM `" . DB_PREFIX . "ebay_profile` WHERE `type` = '" . (int)$type . "' AND `default` = '1'LIMIT 1");

		if ($qry->num_rows) {
			return (int)$qry->row['ebay_profile_id'];
		} else {
			return false;
		}
	}

	private function clearDefault($type) {
		$this->db->query("UPDATE `" . DB_PREFIX . "ebay_profile` SET `default` = '0' WHERE `type` = '" . (int)$type . "'");
	}
}