PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/local/Ced/CsMarketplace/Model/Vendor/Attribute.php

https://gitlab.com/vincent.perdereau/picandparts
PHP | 277 lines | 182 code | 18 blank | 77 comment | 19 complexity | b2f064389d502c544c9cb2a8ce201143 MD5 | raw file
  1. <?php
  2. /**
  3. * CedCommerce
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. *
  12. * @category Ced
  13. * @package Ced_CsMarketplace
  14. * @author CedCommerce Core Team <coreteam@cedcommerce.com>
  15. * @copyright Copyright CedCommerce (http://cedcommerce.com/)
  16. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. */
  18. /**
  19. * Vendor attribute model
  20. *
  21. * @category Ced
  22. * @package Ced_CsMarketplace
  23. * @author CedCommerce Core Team <coreteam@cedcommerce.com>
  24. */
  25. class Ced_CsMarketplace_Model_Vendor_Attribute extends Mage_Eav_Model_Entity_Attribute
  26. {
  27. /**
  28. * Prefix of vendor attribute events names
  29. *
  30. * @var string
  31. */
  32. protected $_eventPrefix='csmarektplace_vendor_attribute';
  33. /**
  34. * Current scope (store Id)
  35. *
  36. * @var int
  37. */
  38. protected $_storeId;
  39. public function __construct() {
  40. parent::__construct();
  41. $this->setEntityTypeId(Mage::getModel('eav/entity')->setType('csmarketplace_vendor')->getTypeId());
  42. $setIds=Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($this->getEntityTypeId())->getAllIds();
  43. $this->setAttributeSetIds($setIds);
  44. return $this;
  45. }
  46. /**
  47. * Set store scope
  48. *
  49. * @param int|string|Mage_Core_Model_Store $store
  50. * @return Mage_Catalog_Model_Resource_Collection_Abstract
  51. */
  52. public function setStore($store) {
  53. $this->setStoreId(Mage::app()->getStore($store)->getId());
  54. return $this;
  55. }
  56. /**
  57. * Set store scope
  58. *
  59. * @param int|string|Mage_Core_Model_Store $storeId
  60. * @return Mage_Catalog_Model_Resource_Collection_Abstract
  61. */
  62. public function setStoreId($storeId) {
  63. if ($storeId instanceof Mage_Core_Model_Store) {
  64. $storeId=$storeId->getId();
  65. }
  66. $this->_storeId=(int)$storeId;
  67. return $this;
  68. }
  69. /**
  70. * Return current store id
  71. *
  72. * @return int
  73. */
  74. public function getStoreId() {
  75. if (is_null($this->_storeId)) {
  76. $this->setStoreId(Mage::app()->getStore()->getId());
  77. }
  78. return $this->_storeId;
  79. }
  80. /**
  81. * Retrieve default store id
  82. *
  83. * @return int
  84. */
  85. public function getDefaultStoreId() {
  86. return Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID;
  87. }
  88. /**
  89. * Load vendor's attributes into the object
  90. *
  91. * @param Mage_Core_Model_Abstract $object
  92. * @param integer $entityId
  93. * @param array|null $attributes
  94. * @return Ced_CsMarketplace_Model_Vendor_Attribute
  95. */
  96. public function load($entityId, $field=NULL) {
  97. parent::load($entityId, $field );
  98. if($this && $this->getId()) {
  99. $joinFields=$this->_vendorForm($this);
  100. if(count($joinFields) > 0) {
  101. foreach($joinFields as $joinField) {
  102. $this->setData('is_visible',$joinField->getIsVisible());
  103. $this->setData('position',$joinField->getSortOrder());
  104. $this->setData('use_in_registration',$joinField->getData('use_in_registration'));
  105. $this->setData('position_in_registration',$joinField->getData('position_in_registration'));
  106. $this->setData('use_in_left_profile',$joinField->getData('use_in_left_profile'));
  107. $this->setData('fontawesome_class_for_left_profile',$joinField->getData('fontawesome_class_for_left_profile'));
  108. $this->setData('position_in_left_profile',$joinField->getData('position_in_left_profile'));
  109. }
  110. }
  111. }
  112. return $this;
  113. }
  114. public function _vendorForm($attribute) {
  115. $store=$this->getStoreId();
  116. $fields=Mage::getModel('csmarketplace/vendor_form')
  117. ->getCollection()
  118. ->addFieldToFilter('attribute_id',array('eq'=>$attribute->getAttributeId()))
  119. ->addFieldToFilter('attribute_code',array('eq'=>$attribute->getAttributeCode()))
  120. ->addFieldToFilter('store_id',array('eq'=>$store));
  121. if(count($fields) == 0) {
  122. $data[]=array(
  123. 'attribute_id' => $attribute->getId(),
  124. 'attribute_code' => $attribute->getAttributeCode(),
  125. 'is_visible' => 0,
  126. 'sort_order' => 0,
  127. 'store_id' => $store,
  128. 'use_in_registration' => 0,
  129. 'position_in_registration' => 0,
  130. 'use_in_left_profile' => 0,
  131. 'fontawesome_class_for_left_profile' => 'fa fa-circle-thin',
  132. 'position_in_left_profile' => 0,
  133. );
  134. Mage::getModel('csmarketplace/vendor_form')->insertMultiple($data);
  135. return $this->_vendorForm($attribute);
  136. }
  137. return $fields;
  138. }
  139. /**
  140. * Retrive Vendor attribute collection
  141. *
  142. * @return Mage_Eav_Model_Resource_Entity_Collection
  143. */
  144. public function getCollection() {
  145. $collection=parent::getCollection();
  146. $typeId=Mage::getModel('csmarketplace/vendor')->getEntityTypeId();
  147. $collection=$collection->addFieldToFilter('entity_type_id',array('eq'=>$typeId));
  148. $labelTableName=Mage::getSingleton('core/resource')->getTableName('eav/attribute_label');
  149. $tableName=Mage::getSingleton('core/resource')->getTableName('csmarketplace/vendor_form');
  150. if($this->getStoreId()) {
  151. $availableStoreWiseIds=$this->getStoreWiseIds($this->getStoreId());
  152. $collection->getSelect()->join(array('vform'=>$tableName), 'main_table.attribute_id=vform.attribute_id', array('is_visible'=>'vform.is_visible','sort_order'=>'vform.sort_order','store_id'=>'vform.store_id','use_in_registration'=>'vform.use_in_registration', 'use_in_left_profile'=>'vform.use_in_left_profile','position_in_registration'=>'vform.position_in_registration', 'position_in_left_profile'=>'vform.position_in_left_profile', 'fontawesome_class_for_left_profile'=>'vform.fontawesome_class_for_left_profile'));
  153. $collection->getSelect()->where('(vform.attribute_id IN ("'.$availableStoreWiseIds.'") AND vform.store_id='.$this->getStoreId().') OR (vform.attribute_id NOT IN ("'.$availableStoreWiseIds.'") AND vform.store_id=0)');
  154. $collection->getSelect()->group('vform.attribute_id');
  155. $collection->getSelect()->joinLeft(array('vlabel'=>$labelTableName), 'main_table.attribute_id=vlabel.attribute_id && vlabel.store_id='.$this->getStoreId(), array('store_label'=>'vlabel.value'));
  156. } else {
  157. $collection->getSelect()->join(array('vform'=>$tableName), 'main_table.attribute_id=vform.attribute_id && vform.store_id=0', array('is_visible'=>'vform.is_visible','sort_order'=>'vform.sort_order','store_id'=>'vform.store_id','use_in_registration'=>'vform.use_in_registration', 'use_in_left_profile'=>'vform.use_in_left_profile','position_in_registration'=>'vform.position_in_registration', 'position_in_left_profile'=>'vform.position_in_left_profile', 'fontawesome_class_for_left_profile'=>'vform.fontawesome_class_for_left_profile'));
  158. $collection->getSelect()->joinLeft(array('vlabel'=>$labelTableName), 'main_table.attribute_id=vlabel.attribute_id && vlabel.store_id=0', array('store_label'=>'vlabel.value'));
  159. }
  160. /* $collection->addExpressionFieldToSelect("is_visible","(CASE WHEN `"."vform"."`.`is_visible` IS NULL THEN (SELECT `".$tableName."`.`is_visible` from `".$tableName."` WHERE `".$tableName."`.`store_id`=0 AND `".$tableName."`.`attribute_id`=`vform`.`attribute_id`) ELSE `"."vform"."`.`is_visible` END)", "");
  161. echo $collection->getSelect();die; */
  162. return $collection;
  163. }
  164. public function getStoreWiseIds($storeId=0) {
  165. if($storeId) {
  166. $allowed=array();
  167. foreach(Mage::getModel('csmarketplace/vendor_form')
  168. ->getCollection()
  169. ->addFieldToFilter('store_id',array('eq'=>$storeId))
  170. as $attribute
  171. ) {
  172. $allowed[]=$attribute->getAttributeId();
  173. }
  174. return implode(',',$allowed);
  175. }
  176. return array();
  177. }
  178. public function addToGroup($group=array()) {
  179. if (count($group) > 0) {
  180. $setIds=Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($this->getEntityTypeId())->getAllIds();
  181. /* print_r($setIds); */
  182. $setId=isset($setIds[0])?$setIds[0]:$this->getEntityTypeId();
  183. /* echo $setId;die; */
  184. $installer=Mage::getModel('csmarketplace/mysql4_setup','csmarketplace_setup');
  185. $installer->startSetup();
  186. if(!in_array($group , $this->getGroupOptions($setId,true))) {
  187. $installer->addAttributeGroup(
  188. 'csmarketplace_vendor',
  189. $setId,
  190. $group
  191. );
  192. }
  193. $installer->addAttributeToGroup(
  194. 'csmarketplace_vendor',
  195. $setId,
  196. $group, //Group Name
  197. $this->getAttributeId()
  198. );
  199. $installer->endSetup();
  200. }
  201. }
  202. protected function getGroupOptions($setId,$flag=false) {
  203. $groupCollection=Mage::getResourceModel('eav/entity_attribute_group_collection')
  204. ->setAttributeSetFilter($setId);
  205. if(version_compare(Mage::getVersion(), '1.6', '<')) {
  206. $groupCollection->getSelect()->order('main_table.sort_order');
  207. }
  208. else{
  209. $groupCollection->setSortOrder()
  210. ->load();
  211. }
  212. $options=array();
  213. if($flag) {
  214. foreach ($groupCollection as $group) {
  215. $options[]=$group->getAttributeGroupName();
  216. }
  217. } else {
  218. foreach ($groupCollection as $group) {
  219. $options[$group->getId()]=$group->getAttributeGroupName();
  220. }
  221. }
  222. return $options;
  223. }
  224. public function delete() {
  225. if ($this->getId()) {
  226. $joinFields=$this->_vendorForm($this);
  227. if(count($joinFields) > 0) {
  228. foreach($joinFields as $joinField) {
  229. $joinField->delete();
  230. }
  231. }
  232. }
  233. return parent::delete();;
  234. }
  235. /**
  236. * Processing vendor attribute after save data
  237. *
  238. * @return Ced_CsMarketplace_Model_Vendor_Attribute
  239. */
  240. protected function _afterSave() {
  241. parent::_afterSave();
  242. if ($this->getId()) {
  243. $joinFields=$this->_vendorForm($this);
  244. if(count($joinFields) > 0) {
  245. foreach($joinFields as $joinField) {
  246. $joinField->setData('is_visible',$this->getData('is_visible'));
  247. $joinField->setData('sort_order',$this->getData('position'));
  248. $joinField->setData('use_in_registration',$this->getData('use_in_registration'));
  249. $joinField->setData('position_in_registration',$this->getData('position_in_registration'));
  250. $joinField->setData('use_in_left_profile',$this->getData('use_in_left_profile'));
  251. $joinField->setData('fontawesome_class_for_left_profile',$this->getData('fontawesome_class_for_left_profile'));
  252. $joinField->setData('position_in_left_profile',$this->getData('position_in_left_profile'));
  253. $joinField->save();
  254. }
  255. }
  256. }
  257. return $this;
  258. }
  259. }