shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

attribute.php (4380B)


      1 <?php
      2 class ModelCatalogAttribute extends Model {
      3 	public function addAttribute($data) {
      4 		$this->db->query("INSERT INTO " . DB_PREFIX . "attribute SET attribute_group_id = '" . (int)$data['attribute_group_id'] . "', sort_order = '" . (int)$data['sort_order'] . "'");
      5 
      6 		$attribute_id = $this->db->getLastId();
      7 
      8 		foreach ($data['attribute_description'] as $language_id => $value) {
      9 			$this->db->query("INSERT INTO " . DB_PREFIX . "attribute_description SET attribute_id = '" . (int)$attribute_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "'");
     10 		}
     11 
     12 		return $attribute_id;
     13 	}
     14 
     15 	public function editAttribute($attribute_id, $data) {
     16 		$this->db->query("UPDATE " . DB_PREFIX . "attribute SET attribute_group_id = '" . (int)$data['attribute_group_id'] . "', sort_order = '" . (int)$data['sort_order'] . "' WHERE attribute_id = '" . (int)$attribute_id . "'");
     17 
     18 		$this->db->query("DELETE FROM " . DB_PREFIX . "attribute_description WHERE attribute_id = '" . (int)$attribute_id . "'");
     19 
     20 		foreach ($data['attribute_description'] as $language_id => $value) {
     21 			$this->db->query("INSERT INTO " . DB_PREFIX . "attribute_description SET attribute_id = '" . (int)$attribute_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "'");
     22 		}
     23 	}
     24 
     25 	public function deleteAttribute($attribute_id) {
     26 		$this->db->query("DELETE FROM " . DB_PREFIX . "attribute WHERE attribute_id = '" . (int)$attribute_id . "'");
     27 		$this->db->query("DELETE FROM " . DB_PREFIX . "attribute_description WHERE attribute_id = '" . (int)$attribute_id . "'");
     28 	}
     29 
     30 	public function getAttribute($attribute_id) {
     31 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "attribute a LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE a.attribute_id = '" . (int)$attribute_id . "' AND ad.language_id = '" . (int)$this->config->get('config_language_id') . "'");
     32 
     33 		return $query->row;
     34 	}
     35 
     36 	public function getAttributes($data = array()) {
     37 		$sql = "SELECT *, (SELECT agd.name FROM " . DB_PREFIX . "attribute_group_description agd WHERE agd.attribute_group_id = a.attribute_group_id AND agd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS attribute_group FROM " . DB_PREFIX . "attribute a LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE ad.language_id = '" . (int)$this->config->get('config_language_id') . "'";
     38 
     39 		if (!empty($data['filter_name'])) {
     40 			$sql .= " AND ad.name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
     41 		}
     42 
     43 		if (!empty($data['filter_attribute_group_id'])) {
     44 			$sql .= " AND a.attribute_group_id = '" . $this->db->escape($data['filter_attribute_group_id']) . "'";
     45 		}
     46 
     47 		$sort_data = array(
     48 			'ad.name',
     49 			'attribute_group',
     50 			'a.sort_order'
     51 		);
     52 
     53 		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
     54 			$sql .= " ORDER BY " . $data['sort'];
     55 		} else {
     56 			$sql .= " ORDER BY attribute_group, ad.name";
     57 		}
     58 
     59 		if (isset($data['order']) && ($data['order'] == 'DESC')) {
     60 			$sql .= " DESC";
     61 		} else {
     62 			$sql .= " ASC";
     63 		}
     64 
     65 		if (isset($data['start']) || isset($data['limit'])) {
     66 			if ($data['start'] < 0) {
     67 				$data['start'] = 0;
     68 			}
     69 
     70 			if ($data['limit'] < 1) {
     71 				$data['limit'] = 20;
     72 			}
     73 
     74 			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
     75 		}
     76 
     77 		$query = $this->db->query($sql);
     78 
     79 		return $query->rows;
     80 	}
     81 
     82 	public function getAttributeDescriptions($attribute_id) {
     83 		$attribute_data = array();
     84 
     85 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "attribute_description WHERE attribute_id = '" . (int)$attribute_id . "'");
     86 
     87 		foreach ($query->rows as $result) {
     88 			$attribute_data[$result['language_id']] = array('name' => $result['name']);
     89 		}
     90 
     91 		return $attribute_data;
     92 	}
     93 
     94 	public function getTotalAttributes() {
     95 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "attribute");
     96 
     97 		return $query->row['total'];
     98 	}
     99 
    100 	public function getTotalAttributesByAttributeGroupId($attribute_group_id) {
    101 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "attribute WHERE attribute_group_id = '" . (int)$attribute_group_id . "'");
    102 
    103 		return $query->row['total'];
    104 	}
    105 }