/** * Retrieve filter array * * @param Enterprise_Search_Model_Resource_Collection $collection * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @param string|array $value * @return array */ protected function _getSearchParam($collection, $attribute, $value) { if (!is_string($value) && empty($value) || is_string($value) && strlen(trim($value)) == 0 || is_array($value) && isset($value['from']) && empty($value['from']) && isset($value['to']) && empty($value['to'])) { return array(); } if (!is_array($value)) { $value = array($value); } $field = Mage::getResourceSingleton('enterprise_search/engine')->getSearchEngineFieldName($attribute, 'nav'); if ($attribute->getBackendType() == 'datetime') { $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT); foreach ($value as &$val) { if (!is_empty_date($val)) { $date = new Zend_Date($val, $format); $val = $date->toString(Zend_Date::ISO_8601) . 'Z'; } } unset($val); } if (empty($value)) { return array(); } else { return array($field => $value); } }
/** * Serialize fields for database storage * @return AW_Autorelated_Model_Blocks */ protected function _beforeSave() { if ($this->getData('currently_viewed') && is_array($this->getData('currently_viewed'))) { $this->setData('currently_viewed', @serialize($this->getData('currently_viewed'))); } if ($this->getData('related_products') && is_array($this->getData('related_products'))) { $this->setData('related_products', @serialize($this->getData('related_products'))); } if ($this->getData('customer_groups') !== null && is_array($this->getData('customer_groups'))) { $this->setData('customer_groups', @implode(',', $this->getData('customer_groups'))); } if ($this->getData('date_to') && !is_empty_date($this->getData('date_to'))) { $this->setData('date_to', date(AW_Autorelated_Model_Mysql4_Blocks::MYSQL_DATETIME_FORMAT, strtotime($this->getData('date_to')))); } else { $this->setData('date_to', null); } if ($this->getData('date_from') && !is_empty_date($this->getData('date_from'))) { $this->setData('date_from', date(AW_Autorelated_Model_Mysql4_Blocks::MYSQL_DATETIME_FORMAT, strtotime($this->getData('date_from')))); } else { $this->setData('date_from', null); } if ($this->getData('store') !== null && is_array($this->getData('store'))) { $this->setData('store', @implode(',', $this->getData('store'))); } return parent::_beforeSave(); }
protected function _beforeSave() { if (!$this->getCategory()) { $this->setCategory('general'); } if ($this->isDataEmptyFor(array('extension', 'category', 'subject', 'body'))) { Mage::throwException($this->__('Missing data for item')); } if (Mage::getSingleton('ewcore/module')->exists($this->getExtension()) === false) { Mage::throwException($this->__('Extensions %s does not exist', $this->getExtension())); } $this->setUpdatedAt(now()); if (is_empty_date($this->getCreatedAt())) { $this->setCreatedAt(now()); } return parent::_beforeSave(); }
protected function _beforeSave() { $this->searchReplaceFieldValues(array('url', 'body'), '', null); if ($this->isDataEmptyFor(array('severity', 'category', 'subject', 'reference_id'))) { Mage::throwException($this->__('Missing data for item')); } if (!$this->getStatus()) { $this->setStatus('enabled'); } if (!$this->getState()) { $this->setState('unread'); } if ($this->isDataEmptyFor(array('sent_at'), 'date')) { Mage::throwException($this->__('Missing data for item')); } $this->setUpdatedAt(now()); if (is_empty_date($this->getCreatedAt())) { $this->setCreatedAt(now()); } return parent::_beforeSave(); }
protected function mapDirectiveSalePriceEffectiveDate($params = array()) { /** @var $product Mage_Catalog_Model_Product */ $product = $this->getProduct(); $assocMapArr = $this->getAssocMaps(); $cDate = Mage::app()->getLocale()->date(null, null, Mage::app()->getLocale()->getDefaultLocale()); $timezone = Mage::app()->getStore($this->getStoreId())->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE); $cell = ""; if (!$this->hasSpecialPrice($product, $this->getSpecialPrice($product))) { return $cell; } $has_default_qty = $this->_hasDefaultQty($assocMapArr); $display_min = $this->getConfigVar('grouped_price_display_mode', 'columns'); if ($has_default_qty) { if ($display_min == RocketWeb_GoogleBaseFeedGenerator_Model_Source_Pricegroupedmode::PRICE_SUM_DEFAULT_QTY) { $product_from_date = $product_to_date = null; $from_time = -1; $to_time = PHP_INT_MAX; foreach ($assocMapArr as $assocMap) { $associatedProduct = $assocMap->getProduct(); if ($associatedProduct->getQty() > 0) { if ($assocMap->hasSpecialPrice($associatedProduct, $assocMap->getSpecialPrice($associatedProduct))) { if ($assocMap->hasPriceByCatalogRules($associatedProduct)) { // Current date to cdate + ttl $fromDate = clone $cDate; $fromDate->setTime('00:00:00', 'HH:mm:ss'); $toDate = new Zend_Date(null, null, Mage::app()->getLocale()->getDefaultLocale()); if ($timezone) { $toDate->setTimezone($timezone); } $toDate->setDate($cDate->toString('yyyy-MM-dd'), 'yyyy-MM-dd'); $toDate->setTime('23:59:59', 'HH:mm:ss'); $toDate->add((int) $this->getConfigVar('ttl', 'columns'), Zend_Date::DAY); $compare_from_date = $fromDate->toString('yyyy-MM-dd 00:00:00'); $compare_to_date = $toDate->toString('yyyy-MM-dd 23:59:59'); } else { $compare_from_date = $associatedProduct->getSpecialFromDate(); $compare_to_date = $associatedProduct->getSpecialToDate(); } if ($from_time < $this->dateToTime($compare_from_date)) { $product_from_date = $compare_from_date; $from_time = $this->dateToTime($product_from_date); } if (!is_empty_date($compare_to_date) && $to_time > $this->dateToTime($compare_to_date)) { $product_to_date = $compare_to_date; $to_time = $this->dateToTime($product_to_date); } } } } } else { // RocketWeb_GoogleBaseFeedGenerator_Model_Source_Pricegroupedmode::PRICE_START_AT $minAssocMap = $this->findMinimalPriceProduct($assocMapArr); if ($minAssocMap === false) { return $cell; } $associatedProduct = $minAssocMap->getProduct(); if ($minAssocMap->hasPriceByCatalogRules($associatedProduct)) { // Current date to cdate + ttl $fromDate = clone $cDate; $fromDate->setTime('00:00:00', 'HH:mm:ss'); $toDate = new Zend_Date(null, null, Mage::app()->getLocale()->getDefaultLocale()); if ($timezone) { $toDate->setTimezone($timezone); } $toDate->setDate($cDate->toString('yyyy-MM-dd'), 'yyyy-MM-dd'); $toDate->setTime('23:59:59', 'HH:mm:ss'); $toDate->add((int) $this->getConfigVar('ttl', 'columns'), Zend_Date::DAY); $compare_from_date = $fromDate->toString('yyyy-MM-dd 00:00:00'); $compare_to_date = $toDate->toString('yyyy-MM-dd 23:59:59'); } else { $compare_from_date = $associatedProduct->getSpecialFromDate(); $compare_to_date = $associatedProduct->getSpecialToDate(); } $product_from_date = $compare_from_date; $product_to_date = $compare_to_date; } } else { // RocketWeb_GoogleBaseFeedGenerator_Model_Source_Pricegroupedmode::PRICE_START_AT $minAssocMap = $this->findMinimalPriceProduct($assocMapArr); if ($minAssocMap === false) { return $cell; } $associatedProduct = $minAssocMap->getProduct(); $product_from_date = $associatedProduct->getSpecialFromDate(); $product_to_date = $associatedProduct->getSpecialToDate(); } $fromDate = new Zend_Date(null, null, Mage::app()->getLocale()->getDefaultLocale()); if ($timezone) { $fromDate->setTimezone($timezone); } if ($product_from_date) { $fromDate->setDate(substr($product_from_date, 0, 10), 'yyyy-MM-dd'); $fromDate->setTime(substr($product_from_date, 11, 8), 'HH:mm:ss'); } $toDate = new Zend_Date(null, null, Mage::app()->getLocale()->getDefaultLocale()); if (!is_empty_date($product_to_date)) { if ($timezone) { $toDate->setTimezone($timezone); } $toDate->setDate(substr($product_to_date, 0, 10), 'yyyy-MM-dd'); $toDate->setTime('23:59:59', 'HH:mm:ss'); } else { if ($timezone) { $toDate->setTimezone($timezone); } $toDate->setDate($cDate->toString('yyyy-MM-dd'), 'yyyy-MM-dd'); $toDate->setTime('23:59:59', 'HH:mm:ss'); $toDate->add((int) $this->getConfigVar('ttl', 'columns'), Zend_Date::DAY); } $cell = $fromDate->toString(Zend_Date::ISO_8601) . "/" . $toDate->toString(Zend_Date::ISO_8601); return $cell; }
/** * Retrieve Date value for store * * @param int $storeId * @param string $date * @return string */ protected function _getStoreDate($storeId, $date = null) { if (!isset($this->_dates[$storeId])) { $timezone = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE, $storeId); $locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $storeId); $locale = new Zend_Locale($locale); $dateObj = new Zend_Date(null, null, $locale); $dateObj->setTimezone($timezone); $this->_dates[$storeId] = array($dateObj, $locale->getTranslation(null, 'date', $locale)); } if (!is_empty_date($date)) { list($dateObj, $format) = $this->_dates[$storeId]; $dateObj->setDate($date, Varien_Date::DATETIME_INTERNAL_FORMAT); return $dateObj->toString($format); } return null; }
/** * Transforms specified date to basic YYYY-MM-dd format. * * @param int $storeId * @param string $date * @return null|string */ protected function _addStoreDateFormat($storeId, $date = null) { if (!isset($this->_dateFormats[$storeId])) { $timezone = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE, $storeId); $locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $storeId); $locale = new Zend_Locale($locale); $dateObj = new Zend_Date(null, null, $locale); $dateObj->setTimezone($timezone); $this->_dateFormats[$storeId] = array($dateObj, $locale->getTranslation(null, 'date', $locale)); } if (is_empty_date($date)) { return null; } list($dateObj, $localeDateFormat) = $this->_dateFormats[$storeId]; $dateObj->setDate($date, $localeDateFormat); return $dateObj->toString('YYYY-MM-dd'); }
/** * Checks if current date of the given store (in the store timezone) is within the range * * @param int|string|Mage_Core_Model_Store $store * @param string|null $dateFrom * @param string|null $dateTo * @return bool */ public function isStoreDateInInterval($store, $dateFrom = null, $dateTo = null) { if (!$store instanceof Mage_Core_Model_Store) { $store = Mage::app()->getStore($store); } $storeTimeStamp = $this->storeTimeStamp($store); $fromTimeStamp = strtotime($dateFrom); $toTimeStamp = strtotime($dateTo); if ($dateTo) { // fix date YYYY-MM-DD 00:00:00 to YYYY-MM-DD 23:59:59 $toTimeStamp += 86400; } $result = false; if (!is_empty_date($dateFrom) && $storeTimeStamp < $fromTimeStamp) { } elseif (!is_empty_date($dateTo) && $storeTimeStamp > $toTimeStamp) { } else { $result = true; } return $result; }
/** * Returns an array of searched parameter. * * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @param mixed $value * @return array */ public function getParams($attribute, $value) { if (empty($value) || isset($value['from']) && empty($value['from']) && isset($value['to']) && empty($value['to'])) { return false; } $fieldName = $this->getFieldName($attribute); $backendType = $attribute->getBackendType(); if ($backendType == 'datetime') { $dateFormat = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT); if (is_array($value)) { foreach ($value as &$val) { if (!is_empty_date($val)) { $date = new Zend_Date($val, $dateFormat); $val = $date->toString(Zend_Date::ISO_8601) . 'Z'; } } unset($val); } else { if (!is_empty_date($value)) { $date = new Zend_Date($value, $dateFormat); $value = $date->toString(Zend_Date::ISO_8601) . 'Z'; } } } if ($attribute->usesSource()) { $attribute->setStoreId(Mage::app()->getStore()->getId()); } return array($fieldName => $value); }
/** * Retrieve filter array * * @deprecated since 1.12.0.0 * * @param Enterprise_Search_Model_Resource_Collection $collection * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @param string|array $value * @return array */ public function getSearchParam($collection, $attribute, $value) { if (empty($value) || isset($value['from']) && empty($value['from']) && isset($value['to']) && empty($value['to'])) { return false; } $locale = Mage::app()->getStore()->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE); $languageSuffix = $this->getLanguageSuffix($locale); $field = $attribute->getAttributeCode(); $backendType = $attribute->getBackendType(); $frontendInput = $attribute->getFrontendInput(); if ($frontendInput == 'multiselect') { $field = 'attr_multi_' . $field; } elseif ($backendType == 'decimal') { $field = 'attr_decimal_' . $field; } elseif ($frontendInput == 'select' || $frontendInput == 'boolean') { $field = 'attr_select_' . $field; } elseif ($backendType == 'datetime') { $field = 'attr_datetime_' . $field; $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT); if (is_array($value)) { foreach ($value as &$val) { if (!is_empty_date($val)) { $date = new Zend_Date($val, $format); $val = $date->toString(Zend_Date::ISO_8601) . 'Z'; } } } else { if (!is_empty_date($value)) { $date = new Zend_Date($value, $format); $value = $date->toString(Zend_Date::ISO_8601) . 'Z'; } } } elseif (in_array($backendType, $this->_textFieldTypes)) { $field .= $languageSuffix; } if ($attribute->usesSource()) { $attribute->setStoreId(Mage::app()->getStore()->getId()); } return array($field => $value); }
protected function mapDirectiveSalePriceEffectiveDate($params = array()) { $map = $params['map']; $product = $this->getProduct(); /* @var $product Mage_Catalog_Model_Product */ $cell = ""; if (!$this->hasSpecialPrice()) { return $cell; } $cDate = Mage::app()->getLocale()->date(null, null, Mage::app()->getLocale()->getDefaultLocale()); $timezone = Mage::app()->getStore($this->getStoreId())->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE); $fromDate = new Zend_Date(null, null, Mage::app()->getLocale()->getDefaultLocale()); if ($timezone) { $fromDate->setTimezone($timezone); } $fromDate->setDate(substr($product->getSpecialFromDate(), 0, 10), 'yyyy-MM-dd'); $fromDate->setTime(substr($product->getSpecialFromDate(), 11, 8), 'HH:mm:ss'); $toDate = new Zend_Date(null, null, Mage::app()->getLocale()->getDefaultLocale()); if (!is_empty_date($product->getSpecialToDate())) { if ($timezone) { $toDate->setTimezone($timezone); } $toDate->setDate(substr($product->getSpecialToDate(), 0, 10), 'yyyy-MM-dd'); $toDate->setTime('23:59:59', 'HH:mm:ss'); } else { if ($timezone) { $toDate->setTimezone($timezone); } $toDate->setDate($cDate->toString('yyyy-MM-dd'), 'yyyy-MM-dd'); $toDate->setTime('23:59:59', 'HH:mm:ss'); $toDate->add(7, Zend_Date::DAY); } $cell = $fromDate->toString(Zend_Date::ISO_8601) . "/" . $toDate->toString(Zend_Date::ISO_8601); return $cell; }
/** * actual export of formated product data to file */ public function export() { $tune = Mage::getStoreConfig('urapidflow/finetune'); if (!empty($tune['export_page_size']) && $tune['export_page_size'] > 0) { $this->_pageRowCount = (int) $tune['export_page_size']; } if (!empty($tune['page_sleep_delay'])) { $this->_pageSleepDelay = (int) $tune['page_sleep_delay']; } $profile = $this->_profile; $logger = $profile->getLogger(); $this->_entityTypeId = $this->_getEntityType($this->_entityType, 'entity_type_id'); $this->_profile->activity('Preparing data'); $this->_prepareAttributes($profile->getAttributeCodes()); $this->_prepareSystemAttributes(); $storeId = $profile->getStoreId(); $this->_storeId = $storeId; $pUrl = Mage::getSingleton('catalog/url'); $baseUrl = Mage::app()->getStore($storeId)->getBaseUrl('web'); $mediaUrl = Mage::app()->getStore($storeId)->getBaseUrl('media'); $mediaDir = Mage::getBaseDir('media'); $imgModel = Mage::getModel('catalog/product_image'); if ($storeId) { $this->_rootCatId = Mage::app()->getStore($storeId)->getGroup()->getRootCategoryId(); } else { $this->_rootCatId = $this->_read->fetchOne("select g.root_category_id from {$this->_t('core/website')} w inner join {$this->_t('core/store_group')} g on g.group_id=w.default_group_id where w.is_default=1"); } $rootPath = $this->_rootCatId ? '1/' . $this->_rootCatId : '1'; // main product table $table = $this->_t('catalog/category'); // start select $upAttrId = $this->_attr('url_path', 'attribute_id'); $select = $this->_read->select()->from(array('e' => $table))->join(array('up' => $table . '_varchar'), "up.entity_id=e.entity_id and up.attribute_id={$upAttrId} and up.value<>'' and up.value is not null and up.store_id=0", array())->order('path')->where($this->_read->quoteInto('path=?', $rootPath) . $this->_read->quoteInto(' OR path like ?', $rootPath . '/%')); if ($storeId != 0) { $select->joinLeft(array('ups' => $table . '_varchar'), "ups.entity_id=e.entity_id and ups.attribute_id={$upAttrId} and ups.value<>'' and up.value is not null and ups.store_id='{$storeId}'", array()); $select->columns(array('url_path' => 'IFNULL(ups.value, up.value)')); } else { $select->columns(array('url_path' => 'up.value')); } $this->_attrJoined = array($upAttrId); $columns = $profile->getColumns(); $defaultSeparator = $profile->getData('options/csv/multivalue_separator'); if (!$defaultSeparator) { $defaultSeparator = '; '; } $this->_fields = array(); $this->_fieldsCodes = array(); if ($columns) { foreach ($columns as $i => &$f) { if (empty($f['alias'])) { $f['alias'] = $f['field']; } if (!empty($f['default']) && is_array($f['default'])) { $f['default'] = join(!empty($f['separator']) ? $f['separator'] : $defaultSeparator, $f['default']); } $this->_fields[$f['alias']] = $f; $this->_fieldsCodes[$f['field']] = true; } unset($f); } else { foreach ($this->_attributesByCode as $k => $a) { $this->_fields[$k] = array('field' => $k, 'title' => $k, 'alias' => $k); $this->_fieldsCodes[$k] = true; } } $condProdIds = $profile->getConditionsProductIds(); if (is_array($condProdIds)) { $select->where('entity_id in (?)', $condProdIds); } $countSelect = clone $select; $countSelect->reset(Zend_Db_Select::FROM)->reset(Zend_Db_Select::COLUMNS)->from(array('e' => $table), array('count(*)')); $count = $this->_read->fetchOne($countSelect); unset($countSelect); $profile->setRowsFound($count)->setStartedAt(now())->sync(true, array('rows_found', 'started_at'), false); $profile->activity('Exporting'); #memory_get_usage(); // open export file $profile->ioOpenWrite(); // write headers to the file $headers = array(); foreach ($this->_fields as $k => $f) { $headers[] = !empty($f['alias']) ? $f['alias'] : $k; } $profile->ioWriteHeader($headers); // batch size // repeat until data available // data will loaded page by page to conserve memory for ($page = 0;; $page++) { // set limit for current page $select->limitPage($page + 1, $this->_pageRowCount); // retrieve product entity data and attributes in filters $rows = $this->_read->fetchAll($select); if (!$rows) { break; } // fill $this->_entities associated by product id $this->_entities = array(); foreach ($rows as $p) { $this->_entities[$p['entity_id']][0] = $p; } unset($rows); $this->_entityIds = array_keys($this->_entities); $this->_attrValueIds = array(); $this->_attrValuesFetched = array(); $this->_fetchAttributeValues($storeId, true); memory_get_usage(true); // format product data as needed foreach ($this->_entities as $id => $p) { $csvRow = array(); $value = null; foreach ($this->_fields as $k => $f) { $attr = $f['field']; $inputType = $this->_attr($attr, 'frontend_input'); // retrieve correct value for current row and field if ($v = $this->_attr($attr, 'force_value')) { $value = $v; } elseif (!empty($this->_fieldAttributes[$attr])) { $a = $this->_fieldAttributes[$attr]; $value = isset($p[$storeId][$a]) ? $p[$storeId][$a] : (isset($p[0][$a]) ? $p[0][$a] : null); } else { $value = isset($p[$storeId][$attr]) ? $p[$storeId][$attr] : (isset($p[0][$attr]) ? $p[0][$attr] : null); } // replace raw numeric values with source option labels if (($inputType == 'select' || $inputType == 'multiselect') && ($options = $this->_attr($attr, 'options'))) { if (!is_array($value)) { $value = explode(',', $value); } foreach ($value as &$v) { if ($v === '') { continue; } if (!isset($options[$v])) { $profile->addValue('num_warnings'); $logger->warning($this->__("Unknown option '%s' for category '%s' attribute '%s'", $v, $p[0]['url_path'], $attr)); continue; } $v = $options[$v]; } unset($v); } // combine multiselect values if (is_array($value)) { $value = join(!empty($f['separator']) ? $f['separator'] : $defaultSeparator, $value); } // process special cases of loaded attributes switch ($attr) { // product url case 'url_path': if (!empty($f['format']) && $f['format'] == 'url') { $value = $baseUrl . $value; } break; case 'const.value': $value = isset($f['default']) ? $f['default'] : ''; break; } switch ($this->_attr($attr, 'backend_type')) { case 'decimal': if (!is_null($value) && !empty($f['format'])) { $value = sprintf($f['format'], $value); } break; case 'datetime': if (!is_empty_date($value)) { $value = date(!empty($f['format']) ? $f['format'] : 'Y-m-d H:i:s', strtotime($value)); } break; } switch ($this->_attr($attr, 'frontend_input')) { case 'media_image': if ($value == 'no_selection') { $value = ''; } if (!empty($f['format']) && $f['format'] == 'url' && !empty($value)) { try { $path = $imgModel->setBaseFile($value)->getBaseFile(); $path = str_replace($mediaDir . DS, "", $path); $value = $mediaUrl . str_replace(DS, '/', $path); } catch (Exception $e) { $value = ''; } } break; } if ((is_null($value) || $value === '') && !empty($f['default'])) { $value = $f['default']; } $csvRow[] = $value; } $csvRow = $this->_convertEncoding($csvRow); $profile->ioWrite($csvRow); $profile->addValue('rows_processed')->addValue('rows_success'); } // foreach ($this->_entities as $id=>&$p) $profile->setMemoryUsage(memory_get_usage(true))->setMemoryPeakUsage(memory_get_peak_usage(true))->setSnapshotAt(now())->sync(); $this->_checkLock(); // stop repeating if this is the last page if (sizeof($this->_entities) < $this->_pageRowCount) { break; } if ($this->_pageSleepDelay) { sleep($this->_pageSleepDelay); } } // while (true) $profile->ioClose(); }
/** * @param $product * @param $special_price * @param $rules * @return bool */ public function hasSpecialPrice($product, $special_price, $rules = true) { if ($rules && $this->hasPriceByCatalogRules($product)) { return true; } if ($special_price <= 0) { return false; } $cDate = Mage::app()->getLocale()->date(null, null, Mage::app()->getLocale()->getDefaultLocale()); $timezone = Mage::app()->getStore($this->getStoreId())->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE); // From Date $fromDate = new Zend_Date(null, null, Mage::app()->getLocale()->getDefaultLocale()); if ($timezone) { $fromDate->setTimezone($timezone); } $special_from_date = $product->getSpecialFromDate(); if (is_empty_date($special_from_date)) { $special_from_date = $cDate->toString('yyyy-MM-dd HH:mm:ss'); } $fromDate->setDate(substr($special_from_date, 0, 10), 'yyyy-MM-dd'); $fromDate->setTime(substr($special_from_date, 11, 8), 'HH:mm:ss'); // To Date $toDate = new Zend_Date(null, null, Mage::app()->getLocale()->getDefaultLocale()); if ($timezone) { $toDate->setTimezone($timezone); } $special_to_date = $product->getSpecialToDate(); if (is_empty_date($special_to_date)) { $special_to_date = $cDate->toString('yyyy-MM-dd HH:mm:ss'); } if (is_empty_date($special_to_date)) { $toDate->add(365, Zend_Date::DAY); } $toDate->setDate(substr($special_to_date, 0, 10), 'yyyy-MM-dd'); $toDate->setTime(substr($special_to_date, 11, 8), 'HH:mm:ss'); if (($fromDate->compare($cDate) == -1 || $fromDate->compare($cDate) == 0) && ($toDate->compare($cDate) == 1 || $toDate->compare($cDate) == 0)) { return true; } return false; }
public function render(Varien_Object $row) { $data = $row->getData($this->getColumn()->getIndex()); return is_empty_date($data) ? '' : parent::render($row); }
/** * actual export of formated product data to file */ public function export() { $benchmark = false; $tune = Mage::getStoreConfig('urapidflow/finetune'); if (!empty($tune['export_page_size']) && $tune['export_page_size'] > 0) { $this->_pageRowCount = (int) $tune['export_page_size']; } if (!empty($tune['page_sleep_delay'])) { $this->_pageSleepDelay = (int) $tune['page_sleep_delay']; } $profile = $this->_profile; $logger = $profile->getLogger(); $this->_entityTypeId = $this->_getEntityType($this->_entityType, 'entity_type_id'); $this->_profile->activity('Preparing data'); $this->_prepareAttributes($profile->getAttributeCodes()); $this->_prepareSystemAttributes(); $storeId = $profile->getStoreId(); $this->_storeId = $storeId; $manageStock = Mage::getStoreConfig('cataloginventory/item_options/manage_stock', $storeId); $pUrl = Mage::getSingleton('catalog/url'); $secure = $profile->getData('options/export/image_https'); $baseUrl = Mage::app()->getStore($storeId)->getBaseUrl('web', $secure); $mediaUrl = Mage::app()->getStore($storeId)->getBaseUrl('media', $secure); $mediaDir = Mage::getBaseDir('media'); $imgModel = Mage::getModel('catalog/product_image'); $exportImageFiles = $profile->getData('options/export/image_files'); $imagesFromDir = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'product'; $imagesToDir = $profile->getImagesBaseDir(); $this->_profile->activity($this->__('Loading products')); // main product table $table = $this->_t('catalog/product'); // start select $select = $this->_read->select()->from(array('e' => $table)); $this->_attrJoined = array(); $columns = $profile->getColumns(); $exportInvalidValues = $profile->getData('options/export/invalid_values'); $exportInternalValues = $profile->getData('options/export/internal_values'); $skipOutOfStock = $profile->getData('options/export/skip_out_of_stock'); $manageStock = (int) Mage::getStoreConfig('cataloginventory/item_options/manage_stock', $storeId); $defaultSeparator = $profile->getData('options/csv/multivalue_separator'); if (!$defaultSeparator) { $defaultSeparator = '; '; } $this->_fields = array(); $this->_fieldsCodes = array(); if ($columns) { foreach ($columns as $i => &$f) { if (empty($f['alias'])) { $f['alias'] = $f['field']; } if (!empty($f['default']) && is_array($f['default'])) { $f['default'] = join(!empty($f['separator']) ? $f['separator'] : $defaultSeparator, $f['default']); } $f['column_num'] = $i + 1; $this->_fields[$f['alias']] = $f; $this->_fieldsCodes[$f['field']] = true; } unset($f); } else { $columns = array(); $i = 1; foreach ($this->_attributesByCode as $k => $a) { if ($k == 'product.entity_id') { continue; } $columns[$i - 1] = array('field' => $k, 'title' => $k, 'alias' => $k, 'default' => ''); $this->_fields[$k] = array('field' => $k, 'title' => $k, 'alias' => $k, 'column_num' => $i++); $this->_fieldsCodes[$k] = true; } } if ($this->_hasColumnsLike('category.')) { $this->_prepareCategories(); #memory_get_usage(true); } if ($skipOutOfStock) { $select->where("entity_id in (select product_id from {$this->_t('cataloginventory_stock_item')} where (qty>0 && is_in_stock=1) or not if(use_config_manage_stock,{$manageStock},manage_stock))"); } $condProdIds = $profile->getConditionsProductIds(); if (is_array($condProdIds)) { $select->where('entity_id in (?)', $condProdIds); } $countSelect = clone $select; $countSelect->reset(Zend_Db_Select::FROM)->reset(Zend_Db_Select::COLUMNS)->from(array('e' => $table), array('count(*)')); $count = $this->_read->fetchOne($countSelect); unset($countSelect); $profile->setRowsFound($count)->setStartedAt(now())->sync(true, array('rows_found', 'started_at'), false); $profile->activity($this->__('Exporting')); #memory_get_usage(true); if ($benchmark) { Mage::log("============================= IMPORT START: " . memory_get_usage() . ', ' . memory_get_peak_usage()); } // open export file $profile->ioOpenWrite(); // write headers to the file $headers = array(); foreach ($columns as $c) { $headers[] = !empty($c['alias']) ? $c['alias'] : $c['field']; } $profile->ioWriteHeader($headers); $rowNum = 1; // batch size // repeat until data available // data will loaded page by page to conserve memory for ($page = 0;; $page++) { #memory_get_usage(true); if ($benchmark) { Mage::log("================ PAGE START: " . memory_get_usage() . ', ' . memory_get_peak_usage()); } // set limit for current page $select->limitPage($page + 1, $this->_pageRowCount); // retrieve product entity data and attributes in filters $rows = $this->_read->fetchAll($select); if (!$rows) { break; } $this->_importResetPageData(); unset($this->_products); // fill $this->_products associated by product id $this->_products = array(); foreach ($rows as $p) { $this->_products[$p['entity_id']][0] = $p; } unset($rows); #memory_get_usage(true); if ($benchmark) { Mage::log("_readRows: " . memory_get_usage() . ', ' . memory_get_peak_usage()); } $this->_productIds = array_keys($this->_products); $this->_fetchAttributeValues($storeId, true); #memory_get_usage(true); if ($benchmark) { Mage::log("_fetchAttributeValues: " . memory_get_usage() . ', ' . memory_get_peak_usage()); } $this->_fetchWebsiteValues(); #memory_get_usage(true); if ($benchmark) { Mage::log("_fetchWebsiteValues: " . memory_get_usage() . ', ' . memory_get_peak_usage()); } $this->_fetchStockValues(); #memory_get_usage(true); if ($benchmark) { Mage::log("_fetchStockValues: " . memory_get_usage() . ', ' . memory_get_peak_usage()); } $this->_fetchCategoryValues(); #memory_get_usage(true); if ($benchmark) { Mage::log("_fetchCategoryValues: " . memory_get_usage() . ', ' . memory_get_peak_usage()); } $this->_csvRows = array(); $this->_exportProcessPrice(); Mage::dispatchEvent('urapidflow_catalog_product_export_before_format', array('vars' => array('profile' => $this->_profile, 'products' => &$this->_products, 'fields' => &$this->_fields))); // format product data as needed foreach ($this->_products as $id => $p) { $logger->setLine(++$rowNum)->setColumn(0); $csvRow = array(); $value = null; foreach ($columns as $c) { $attr = $c['field']; $f = $this->_fields[$c['alias']]; $inputType = $this->_attr($attr, 'frontend_input'); $sourceModel = $this->_attr($attr, 'source_model'); // retrieve correct value for current row and field if ($v = $this->_attr($attr, 'force_value')) { $value = $v; } elseif (!empty($this->_fieldAttributes[$attr])) { $a = $this->_fieldAttributes[$attr]; $value = isset($p[$storeId][$a]) ? $p[$storeId][$a] : (isset($p[0][$a]) ? $p[0][$a] : null); } else { $value = isset($p[$storeId][$attr]) ? $p[$storeId][$attr] : (isset($p[0][$attr]) ? $p[0][$attr] : null); } if ((is_null($value) || $value === '') && !empty($c['default'])) { $value = $c['default']; } // replace raw numeric values with source option labels if ((!$exportInternalValues || strpos($attr, 'category.') === 0) && ($inputType == 'select' || $inputType == 'multiselect' || $sourceModel) && ($options = $this->_attr($attr, 'options'))) { if (!is_array($value) && $inputType == 'multiselect') { $value = explode(',', $value); } elseif (!is_array($value)) { $value = array($value); } foreach ($value as $k => &$v) { if ($v === '') { continue; } if (!isset($options[$v])) { $profile->addValue('num_warnings'); $logger->setColumn($f['column_num'])->warning($this->__("Unknown option '%s' for product '%s' attribute '%s'", $v, $p[0]['sku'], $attr)); if (!$exportInvalidValues) { unset($value[$k]); } continue; } $v = $options[$v]; } unset($v); } // combine multiselect values if (is_array($value)) { $value = join(!empty($f['separator']) ? $f['separator'] : $defaultSeparator, $value); } // process special cases of loaded attributes switch ($attr) { // product url case 'url_path': if (!empty($f['format']) && $f['format'] == 'url') { $value = $baseUrl . $value; } break; case 'const.value': $value = isset($c['default']) ? $c['default'] : ''; break; case 'const.function': $value = ''; if (!empty($c['default'])) { try { list($class, $func) = explode('::', $c['default']); if (strpos($class, '/') !== false) { $model = Mage::getSingleton($class); } else { $model = $class; } $value = call_user_func(array($model, $func), $p, $c, $storeId); } catch (Exception $e) { $logger->setColumn($f['column_num'])->warning($this->__("Exception for product '%s' attribute '%s': %s", $p[0]['sku'], $attr, $e->getMessage())); } } break; } switch ($this->_attr($attr, 'backend_type')) { case 'decimal': if (!is_null($value) && !empty($f['format'])) { $value = sprintf($f['format'], $value); } break; case 'datetime': if (!is_empty_date($value)) { $value = date(!empty($f['format']) ? $f['format'] : 'Y-m-d H:i:s', strtotime($value)); } break; } switch ($this->_attr($attr, 'frontend_input')) { case 'media_image': if ($value == 'no_selection') { $value = ''; } if ($value !== '' && $exportImageFiles) { $logger->setColumn($f['column_num']); $this->_copyImageFile($imagesFromDir, $imagesToDir, $value); } if (!empty($f['format']) && $f['format'] == 'url' && !empty($value)) { try { $path = $imgModel->setBaseFile($value)->getBaseFile(); $path = str_replace($mediaDir . DS, "", $path); $value = $mediaUrl . str_replace(DS, '/', $path); } catch (Exception $e) { $value = ''; } } break; } $csvRow[] = $value; } $csvRow = $this->_convertEncoding($csvRow); #$profile->ioWrite($csvRow); $this->_csvRows[] = $csvRow; $profile->addValue('rows_processed'); } // foreach ($this->_products as $id=>&$p) Mage::dispatchEvent('urapidflow_catalog_product_export_before_output', array('vars' => array('profile' => $this->_profile, 'products' => &$this->_products, 'fields' => &$this->_fields, 'rows' => &$this->_csvRows))); foreach ($this->_csvRows as $row) { $profile->ioWrite($row); $profile->addValue('rows_success'); } $profile->setMemoryUsage(memory_get_usage(true))->setMemoryPeakUsage(memory_get_peak_usage(true))->setSnapshotAt(now())->sync(); $this->_checkLock(); // stop repeating if this is the last page if (sizeof($this->_products) < $this->_pageRowCount) { break; } if ($this->_pageSleepDelay) { sleep($this->_pageSleepDelay); } } // while (true) $profile->ioClose(); }
/** * @param array $params * @return string */ protected function mapDirectiveSalePriceEffectiveDate($params = array()) { /** @var $product Mage_Catalog_Model_Product */ $product = $this->getProduct(); $cell = ""; $cDate = Mage::app()->getLocale()->date(null, null, Mage::app()->getLocale()->getDefaultLocale()); $timezone = Mage::app()->getStore($this->getStoreId())->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE); if ($this->hasPriceByCatalogRules($product)) { // Current date to cdate + ttl $fromDate = clone $cDate; $fromDate->setTime('00:00:00', 'HH:mm:ss'); $toDate = new Zend_Date(null, null, Mage::app()->getLocale()->getDefaultLocale()); if ($timezone) { $toDate->setTimezone($timezone); } $toDate->setDate($cDate->toString('yyyy-MM-dd'), 'yyyy-MM-dd'); $toDate->setTime('23:59:59', 'HH:mm:ss'); $toDate->add((int) $this->getConfigVar('ttl', 'columns'), Zend_Date::DAY); $cell = $fromDate->toString(Zend_Date::ISO_8601) . "/" . $toDate->toString(Zend_Date::ISO_8601); return $cell; } $special_from_date = $product->getSpecialFromDate(); $special_to_date = $product->getSpecialToDate(); if (!$this->hasSpecialPrice($product, $this->getSpecialPrice($product)) || empty($special_from_date) && empty($special_to_date)) { return $cell; } // From Date if (is_empty_date($special_from_date)) { $special_from_date = $cDate->toString('yyyy-MM-dd HH:mm:ss'); } $fromDate = new Zend_Date(null, null, Mage::app()->getLocale()->getDefaultLocale()); if ($timezone) { $fromDate->setTimezone($timezone); } $fromDate->setDate(substr($special_from_date, 0, 10), 'yyyy-MM-dd'); $fromDate->setTime(substr($special_from_date, 11, 8), 'HH:mm:ss'); // To Date if (is_empty_date($product->getSpecialToDate())) { $special_to_date = $cDate->toString('yyyy-MM-dd HH:mm:ss'); } $toDate = new Zend_Date(null, null, Mage::app()->getLocale()->getDefaultLocale()); if ($timezone) { $toDate->setTimezone($timezone); } $toDate->setDate(substr($special_to_date, 0, 10), 'yyyy-MM-dd'); $toDate->setTime('23:59:59', 'HH:mm:ss'); if (is_empty_date($product->getSpecialToDate())) { $toDate->add((int) $this->getConfigVar('ttl', 'columns'), Zend_Date::DAY); } $cell = $fromDate->toString(Zend_Date::ISO_8601) . "/" . $toDate->toString(Zend_Date::ISO_8601); $this->_findAndReplace($cell, $params['map']['column']); return $cell; }