1: <?php
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: class Mage_Tag_Model_Resource_Tag_Relation extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('tag/relation', 'tag_relation_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: public function loadByTagCustomer($model)
53: {
54: if ($model->getTagId() && $model->getCustomerId()) {
55: $read = $this->_getReadAdapter();
56: $bind = array(
57: 'tag_id' => $model->getTagId(),
58: 'customer_id' => $model->getCustomerId()
59: );
60:
61: $select = $read->select()
62: ->from($this->getMainTable())
63: ->join(
64: $this->getTable('tag/tag'),
65: $this->getTable('tag/tag') . '.tag_id = ' . $this->getMainTable() . '.tag_id'
66: )
67: ->where($this->getMainTable() . '.tag_id = :tag_id')
68: ->where('customer_id = :customer_id');
69:
70: if ($model->getProductId()) {
71: $select->where($this->getMainTable() . '.product_id = :product_id');
72: $bind['product_id'] = $model->getProductId();
73: }
74:
75: if ($model->hasStoreId()) {
76: $select->where($this->getMainTable() . '.store_id = :sore_id');
77: $bind['sore_id'] = $model->getStoreId();
78: }
79: $data = $read->fetchRow($select, $bind);
80: $model->setData(( is_array($data) ) ? $data : array());
81: }
82:
83: return $this;
84: }
85:
86: 87: 88: 89: 90: 91:
92: public function getProductIds($model)
93: {
94: $bind = array(
95: 'tag_id' => $model->getTagId()
96: );
97: $select = $this->_getReadAdapter()->select()
98: ->from($this->getMainTable(), 'product_id')
99: ->where($this->getMainTable() . '.tag_id=:tag_id');
100:
101: if (!is_null($model->getCustomerId())) {
102: $select->where($this->getMainTable() . '.customer_id= :customer_id');
103: $bind['customer_id'] = $model->getCustomerId();
104: }
105:
106: if ($model->hasStoreId()) {
107: $select->where($this->getMainTable() . '.store_id = :store_id');
108: $bind['store_id'] = $model->getStoreId();
109: }
110:
111: if (!is_null($model->getStatusFilter())) {
112: $select->join(
113: $this->getTable('tag/tag'),
114: $this->getTable('tag/tag') . '.tag_id = ' . $this->getMainTable() . '.tag_id'
115: )
116: ->where($this->getTable('tag/tag') . '.status = :t_status');
117: $bind['t_status'] = $model->getStatusFilter();
118: }
119:
120: return $this->_getReadAdapter()->fetchCol($select, $bind);
121: }
122:
123: 124: 125: 126: 127: 128:
129: public function getRelatedTagIds($model)
130: {
131: $productIds = (is_array($model->getProductId())) ? $model->getProductId() : array($model->getProductId());
132: $select = $this->_getReadAdapter()->select()
133: ->from($this->getMainTable(), 'tag_id')
134: ->where("product_id IN(?)", $productIds)
135: ->order('tag_id');
136: return $this->_getReadAdapter()->fetchCol($select);
137: }
138:
139: 140: 141: 142: 143: 144: 145:
146: public function deactivate($tagId, $customerId)
147: {
148: $condition = array(
149: 'tag_id = ?' => $tagId,
150: 'customer_id = ?' => $customerId
151: );
152:
153: $data = array('active' => Mage_Tag_Model_Tag_Relation::STATUS_NOT_ACTIVE);
154: $this->_getWriteAdapter()->update($this->getMainTable(), $data, $condition);
155: return $this;
156: }
157:
158: 159: 160: 161: 162: 163:
164: public function addRelations($model)
165: {
166: $addedIds = $model->getAddedProductIds();
167:
168: $bind = array(
169: 'tag_id' => $model->getTagId(),
170: 'store_id' => $model->getStoreId()
171: );
172: $write = $this->_getWriteAdapter();
173:
174: $select = $write->select()
175: ->from($this->getMainTable(), 'product_id')
176: ->where('tag_id = :tag_id')
177: ->where('store_id = :store_id');
178: $oldRelationIds = $write->fetchCol($select, $bind);
179:
180: $insert = array_diff($addedIds, $oldRelationIds);
181: $delete = array_diff($oldRelationIds, $addedIds);
182:
183: if (!empty($insert)) {
184: $insertData = array();
185: foreach ($insert as $value) {
186: $insertData[] = array(
187: 'tag_id' => $model->getTagId(),
188: 'store_id' => $model->getStoreId(),
189: 'product_id' => $value,
190: 'customer_id' => $model->getCustomerId(),
191: 'created_at' => $this->formatDate(time())
192: );
193: }
194: $write->insertMultiple($this->getMainTable(), $insertData);
195: }
196:
197: if (!empty($delete)) {
198: $write->delete($this->getMainTable(), array(
199: 'product_id IN (?)' => $delete,
200: 'store_id = ?' => $model->getStoreId(),
201: ));
202: }
203:
204: return $this;
205: }
206: }
207: