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

/app/code/core/Mage/Eav/Model/Mysql4/Form/Fieldset.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 161 lines | 92 code | 14 blank | 55 comment | 13 complexity | d29143722b427fd2585df3d6c5b1d86d MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  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. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Eav
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Eav Form Fieldset Resource Model
  28. *
  29. * @category Mage
  30. * @package Mage_Eav
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Eav_Model_Mysql4_Form_Fieldset extends Mage_Core_Model_Mysql4_Abstract
  34. {
  35. /**
  36. * Initialize connection and define main table
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('eav/form_fieldset', 'fieldset_id');
  42. $this->addUniqueField(array(
  43. 'field' => array('type_id', 'code'),
  44. 'title' => Mage::helper('eav')->__('Form Fieldset with the same code')
  45. ));
  46. }
  47. /**
  48. * After save (save labels)
  49. *
  50. * @param Mage_Eav_Model_Form_Fieldset $object
  51. * @return Mage_Eav_Model_Mysql4_Form_Fieldset
  52. */
  53. protected function _afterSave(Mage_Core_Model_Abstract $object)
  54. {
  55. if ($object->hasLabels()) {
  56. $new = $object->getLabels();
  57. $old = $this->getLabels($object);
  58. $write = $this->_getWriteAdapter();
  59. $insert = array_diff(array_keys($new), array_keys($old));
  60. $delete = array_diff(array_keys($old), array_keys($new));
  61. $update = array();
  62. foreach ($new as $storeId => $label) {
  63. if (isset($old[$storeId]) && $old[$storeId] != $label) {
  64. $update[$storeId] = $label;
  65. } else if (isset($old[$storeId]) && empty($label)) {
  66. $delete[] = $storeId;
  67. }
  68. }
  69. if (!empty($insert)) {
  70. $data = array();
  71. foreach ($insert as $storeId) {
  72. $label = $new[$storeId];
  73. if (empty($label)) {
  74. continue;
  75. }
  76. $data[] = array(
  77. 'fieldset_id' => (int)$object->getId(),
  78. 'store_id' => (int)$storeId,
  79. 'label' => $label
  80. );
  81. }
  82. if ($data) {
  83. $write->insertMultiple($this->getTable('eav/form_fieldset_label'), $data);
  84. }
  85. }
  86. if (!empty($delete)) {
  87. $where = join(' AND ', array(
  88. $write->quoteInto('fieldset_id=?', $object->getId()),
  89. $write->quoteInto('store_id IN(?)', $delete)
  90. ));
  91. $write->delete($this->getTable('eav/form_fieldset_label'), $where);
  92. }
  93. if (!empty($update)) {
  94. foreach ($update as $storeId => $label) {
  95. $bind = array(
  96. 'label' => $label
  97. );
  98. $where = join(' AND ', array(
  99. $write->quoteInto('fieldset_id=?', $object->getId()),
  100. $write->quoteInto('store_id=?', $storeId)
  101. ));
  102. $write->update($this->getTable('eav/form_fieldset_label'), $bind, $where);
  103. }
  104. }
  105. }
  106. return parent::_afterSave($object);
  107. }
  108. /**
  109. * Retrieve fieldset labels for stores
  110. *
  111. * @param Mage_Eav_Model_Form_Fieldset $object
  112. * @return array
  113. */
  114. public function getLabels($object)
  115. {
  116. if (!$object->getId()) {
  117. return array();
  118. }
  119. $select = $this->_getReadAdapter()->select()
  120. ->from($this->getTable('eav/form_fieldset_label'), array('store_id', 'label'))
  121. ->where('fieldset_id=?', $object->getId());
  122. return $this->_getReadAdapter()->fetchPairs($select);
  123. }
  124. /**
  125. * Retrieve select object for load object data
  126. *
  127. * @param string $field
  128. * @param mixed $value
  129. * @param Mage_Eav_Model_Form_Fieldset $object
  130. * @return Varien_Db_Select
  131. */
  132. protected function _getLoadSelect($field, $value, $object)
  133. {
  134. $select = parent::_getLoadSelect($field, $value, $object);
  135. $select->joinLeft(
  136. array('default_label' => $this->getTable('eav/form_fieldset_label')),
  137. $this->getMainTable().'fieldset_id=default_label.fieldset_id AND default_label.store_id=0',
  138. array())
  139. ->joinLeft(
  140. array('store_label' => $this->getTable('eav/form_fieldset_label')),
  141. $this->getMainTable().'fieldset_id=store_label.fieldset_id AND default_label.store_id='
  142. .(int)$object->getStoreId(),
  143. array('label' => new Zend_Db_Expr('IFNULL(store_label.label, default_label.label)'))
  144. );
  145. return $select;
  146. }
  147. }