/** * Get changes that were made to source model * * @param Varien_Object $source * @return array */ public function getChanges(Varien_Object $source) { $changes = array(); foreach ($this->_possibleChanges as $code => $label) { if ($source->getData($code) != $source->getOrigData($code)) { switch ($code) { case 'customer_id': $fromCustomer = Mage::getModel('customer/customer')->load($source->getOrigData($code)); $from = $fromCustomer->getFirstname() . ' ' . $fromCustomer->getLastname() . ' (ID: ' . $fromCustomer->getId() . ')'; $toCustomer = Mage::getModel('customer/customer')->load($source->getData($code)); $to = $toCustomer->getFirstname() . ' ' . $toCustomer->getLastname() . ' (ID: ' . $toCustomer->getId() . ')'; break; case 'customer_group_id': $fromGroup = Mage::getModel('customer/group')->load($source->getOrigData($code)); $from = $fromGroup->getCode(); $toGroup = Mage::getModel('customer/group')->load($source->getData($code)); $to = $toGroup->getCode(); break; case 'method': $from = Mage::helper('payment')->getMethodInstance($source->getOrigData('method'))->getTitle(); $to = Mage::helper('payment')->getMethodInstance($source->getData('method'))->getTitle(); break; default: $from = $source->getOrigData($code); $to = $source->getData($code); } $changes[$code] = array('from' => $from, 'to' => $to); } } return $changes; }
public function validate(Varien_Object $object) { /* @var Mirasvit_Helpdesk_Model_Ticket $object */ $attrCode = $this->getAttribute(); if (strpos($attrCode, 'old_') === 0) { $attrCode = str_replace('old_', '', $attrCode); $value = $object->getOrigData($attrCode); } elseif ($attrCode == 'last_message') { $value = $object->getLastMessagePlainText(); } elseif ($attrCode == 'last_reply_by') { $lastMessage = $object->getLastMessage(); if ($lastMessage->getUserId()) { $value = 'user'; } else { $value = 'customer'; } } elseif (strpos($attrCode, 'hours_since_') === 0) { $attrCode = str_replace('hours_since_', '', $attrCode); $timestamp = $object->getData($attrCode); $diff = abs(strtotime(Mage::getModel('core/date')->gmtDate()) - strtotime($timestamp)); $value = round($diff / 60 / 60); } elseif ($attrCode == 'tags') { $value = Mage::helper('helpdesk/tag')->getTagsAsString($object); } else { $value = $object->getData($attrCode); } if (strpos($attrCode, '_id')) { $value = (int) $value; //нам это нужно чтоб приводить пустое значение к нулю и далее сравнивать } return $this->validateAttribute($value); }
/** * Tests Varien_Object->setOrigData() */ public function testOrigData() { $data = array('key1' => 'value1', 'key2' => 'value2'); $this->_object->setData($data); $this->_object->setOrigData(); $this->_object->setData('key1', 'test'); $this->assertTrue($this->_object->dataHasChangedFor('key1')); $this->assertEquals($data, $this->_object->getOrigData()); $this->_object->setOrigData('key1', 'test'); $this->assertEquals('test', $this->_object->getOrigData('key1')); }
/** * Successful if $value is Varien_Object an all condition are fulfilled. * * If read-only properties are set than $value mustn't have changes in them. * * @param Varien_Object|mixed $value * @return bool * @throws InvalidArgumentException when $value is not instanceof Varien_Object */ public function isValid($value) { $this->_clearMessages(); if (!$value instanceof Varien_Object) { throw new InvalidArgumentException('Instance of Varien_Object is expected.'); } if ($this->_readOnlyProperties) { if (!$value->hasDataChanges()) { return true; } foreach ($this->_readOnlyProperties as $property) { if ($this->_hasChanges($value->getData($property), $value->getOrigData($property))) { $this->_messages[__CLASS__] = array($this->getTranslator()->__("Read-only property cannot be changed.")); break; } } } return !count($this->_messages); }
/** * Prepare entity object data for save * * result array structure: * array ( * 'newObject', 'entityRow', 'insert', 'update', 'delete' * ) * * @param Varien_Object $newObject * @return array */ protected function _collectSaveData($newObject) { $newData = $newObject->getData(); $entityId = $newObject->getData($this->getEntityIdField()); // define result data $entityRow = array(); $insert = array(); $update = array(); $delete = array(); if (!empty($entityId)) { $origData = $newObject->getOrigData(); /** * get current data in db for this entity if original data is empty */ if (empty($origData)) { $origData = $this->_getOrigObject($newObject)->getOrigData(); } /** * drop attributes that are unknown in new data * not needed after introduction of partial entity loading */ foreach ($origData as $k => $v) { if (!array_key_exists($k, $newData)) { unset($origData[$k]); } } } else { $origData = array(); } $staticFields = $this->_getWriteAdapter()->describeTable($this->getEntityTable()); $staticFields = array_keys($staticFields); $attributeCodes = array_keys($this->_attributesByCode); foreach ($newData as $k => $v) { /** * Check attribute information */ if (is_numeric($k) || is_array($v)) { continue; } /** * Check if data key is presented in static fields or attribute codes */ if (!in_array($k, $staticFields) && !in_array($k, $attributeCodes)) { continue; } $attribute = $this->getAttribute($k); if (empty($attribute)) { continue; } $attrId = $attribute->getAttributeId(); /** * if attribute is static add to entity row and continue */ if ($this->isAttributeStatic($k)) { $entityRow[$k] = $this->_prepareStaticValue($k, $v); continue; } /** * Check comparability for attribute value */ if ($this->_canUpdateAttribute($attribute, $v, $origData)) { if ($this->_isAttributeValueEmpty($attribute, $v)) { $delete[$attribute->getBackend()->getTable()][] = array('attribute_id' => $attrId, 'value_id' => $attribute->getBackend()->getEntityValueId($newObject)); } elseif ($v !== $origData[$k]) { $update[$attrId] = array('value_id' => $attribute->getBackend()->getEntityValueId($newObject), 'value' => $v); } } else { if (!$this->_isAttributeValueEmpty($attribute, $v)) { $insert[$attrId] = $v; } } } $result = compact('newObject', 'entityRow', 'insert', 'update', 'delete'); return $result; }
/** * Save product category relations * * @param Mage_Catalog_Model_Product $product * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product */ protected function _saveCategories(Varien_Object $object) { $categoryIds = $object->getCategoryIds(); $oldCategoryIds = $object->getOrigData('category_ids'); $oldCategoryIds = !empty($oldCategoryIds) ? explode(',', $oldCategoryIds) : array(); $object->setIsChangedCategories(false); $insert = array_diff($categoryIds, $oldCategoryIds); $delete = array_diff($oldCategoryIds, $categoryIds); $write = $this->_getWriteAdapter(); if (!empty($insert)) { $insertSql = array(); foreach ($insert as $v) { if (!empty($v)) { $insertSql[] = '(' . (int) $v . ',' . $object->getId() . ',0)'; } } if ($insertSql) { $write->query("insert into {$this->_productCategoryTable}\n (category_id, product_id, position) values " . join(',', $insertSql)); } } if (!empty($delete)) { $write->delete($this->_productCategoryTable, $write->quoteInto('product_id=?', $object->getId()) . ' and ' . $write->quoteInto('category_id in (?)', $delete)); } if (!empty($insert) || !empty($delete)) { $object->setIsChangedCategories(true); } return $this; }
/** * Prepare entity object data for save * * result array structure: * array ( * 'newObject', 'entityRow', 'insert', 'update', 'delete' * ) * * @param Varien_Object $newObject * @return array */ protected function _collectSaveData($newObject) { $newData = $newObject->getData(); $entityId = $newObject->getData($this->getEntityIdField()); // define result data $entityRow = array(); $insert = array(); $update = array(); $delete = array(); if (!empty($entityId)) { $origData = $newObject->getOrigData(); /** * get current data in db for this entity if original data is empty */ if (empty($origData) && $newObject->isNewObject() === FALSE) { $origData = $this->_getOrigObject($newObject)->getOrigData(); } /** * drop attributes that are unknown in new data * not needed after introduction of partial entity loading */ if ($origData) { foreach ($origData as $k => $v) { if (!array_key_exists($k, $newData)) { unset($origData[$k]); } } } } else { $origData = array(); } $attributeCodes = array_keys($this->_attributesByCode); foreach ($newData as $k => $v) { /** * Check attribute information */ if (is_numeric($k) || is_array($v)) { continue; } /** * Check if data key is presented in static fields or attribute codes */ if (!in_array($k, $attributeCodes)) { continue; } $attribute = $this->getAttribute($k); if (empty($attribute)) { continue; } $attrCode = $attribute->getAttributeCode(); /** * Check comparability for attribute value */ if (array_key_exists($k, $origData)) { if ($this->_isAttributeValueEmpty($attribute, $v)) { $delete[$attrCode] = 1; } else { if ($v !== $origData[$k]) { $update[$attrCode] = $v; } } } else { if (!$this->_isAttributeValueEmpty($attribute, $v)) { $insert[$attrCode] = $v; } } } $result = compact('newObject', 'insert', 'update', 'delete'); return $result; }
/** * Extract data from model into Mongo-compatible array * * @param Varien_Object $object * @param boolean $forUpdate * @return array */ public function dehydrate(Varien_Object $object, $forUpdate = FALSE) { $rawData = array(); $converter = $this->getPhpToMongoConverter(); foreach ($this->getFieldMappings() as $field => $mapping) { if (!$object->hasData($field) && !isset($mapping->required)) { continue; } $rawValue = $object->getData($field); // Skip fields that have not changed on updates if ($forUpdate && (!$object->hasData($field) || $rawValue === $object->getOrigData($field)) && !$rawValue instanceof Cm_Mongo_Model_Abstract && !$rawValue instanceof Cm_Mongo_Model_Resource_Collection_Embedded) { continue; } if ($rawValue === NULL && isset($mapping->required) && !isset($mapping->required->null)) { $rawValue = (string) $mapping->required; } $type = isset($mapping->type) ? (string) $mapping->type : 'string'; // Embedded object if ($type == 'embedded') { if ($rawValue instanceof Cm_Mongo_Model_Abstract) { $value = $rawValue->getResource()->dehydrate($rawValue, $forUpdate); } else { $value = NULL; } if ($forUpdate && empty($value)) { continue; } } else { if ($type == 'embeddedSet') { $value = array(); if ($rawValue instanceof Cm_Mongo_Model_Resource_Collection_Embedded) { $items = $rawValue->getItems(); } else { $items = (array) $rawValue; } $index = 0; foreach ($items as $item) { if ($item instanceof Cm_Mongo_Model_Abstract) { $_value = $item->getResource()->dehydrate($item, $forUpdate); } else { if ($item instanceof Varien_Object) { $_value = $item->getData(); } else { $_value = (array) $item; } } if (!$forUpdate || $_value) { $value[$index] = $_value; } $index++; } if ($forUpdate && empty($value)) { continue; } } else { if ($forUpdate && $type == 'hash' && $object->getOrigData($field)) { $value = array(); $data = $converter->{$type}($mapping, $rawValue); $origData = $object->getOrigData($field); foreach ($data as $k => $v) { if (!isset($origData[$k]) || $v !== $origData[$k]) { $value[$k] = $v; } } } else { $value = $converter->{$type}($mapping, $rawValue); } } } $rawData[$field] = $value; } return $rawData; }
/** * @param Varien_Object $object * @param array $magentoMarketoFields * @return bool */ public function hasMarketoDataChanged(Varien_Object $object, array $magentoMarketoFields) { $_origData = $object->getOrigData(); $_data = $object->getData(); $data = array(); foreach ($_data as $key => $value) { if (isset($magentoMarketoFields[$key]) && $_origData[$key] != $value) { $data[$key] = $value; } } return !empty($_data); }