/** * Set current attribute to entry (for specified product) * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Framework\Gdata\Gshopping\Entry $entry * @return \Magento\Framework\Gdata\Gshopping\Entry */ public function convertAttribute($product, $entry) { $effectiveDateFrom = $this->getGroupAttributeSalePriceEffectiveDateFrom(); $fromValue = $effectiveDateFrom->getProductAttributeValue($product); $effectiveDateTo = $this->getGroupAttributeSalePriceEffectiveDateTo(); $toValue = $effectiveDateTo->getProductAttributeValue($product); $from = $to = null; if (!empty($fromValue) && \Zend_Date::isDate($fromValue, \Zend_Date::ATOM)) { $from = new \Magento\Framework\Stdlib\DateTime\Date($fromValue, \Zend_Date::ATOM); } if (!empty($toValue) && \Zend_Date::isDate($toValue, \Zend_Date::ATOM)) { $to = new \Magento\Framework\Stdlib\DateTime\Date($toValue, \Zend_Date::ATOM); } $dateString = null; // if we have from an to dates, and if these dates are correct if (!is_null($from) && !is_null($to) && $from->isEarlier($to)) { $dateString = $from->toString(\Zend_Date::ATOM) . '/' . $to->toString(\Zend_Date::ATOM); } // if we have only "from" date, send "from" day if (!is_null($from) && is_null($to)) { $dateString = $from->toString('YYYY-MM-dd'); } // if we have only "to" date, use "now" date for "from" if (is_null($from) && !is_null($to)) { $from = new \Magento\Framework\Stdlib\DateTime\Date(); // if "now" date is earlier than "to" date if ($from->isEarlier($to)) { $dateString = $from->toString(\Zend_Date::ATOM) . '/' . $to->toString(\Zend_Date::ATOM); } } if (!is_null($dateString)) { $this->_setAttribute($entry, 'sale_price_effective_date', self::ATTRIBUTE_TYPE_TEXT, $dateString); } return $entry; }
/** * Retrieve array of intervals * * @param string $from * @param string $to * @param string $period * @return array */ public function getIntervals($from, $to, $period = self::REPORT_PERIOD_TYPE_DAY) { $intervals = array(); if (!$from && !$to) { return $intervals; } $start = new \Magento\Framework\Stdlib\DateTime\Date($from, \Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT); if ($period == self::REPORT_PERIOD_TYPE_DAY) { $dateStart = $start; } if ($period == self::REPORT_PERIOD_TYPE_MONTH) { $dateStart = new \Magento\Framework\Stdlib\DateTime\Date(date("Y-m", $start->getTimestamp()), DateTime::DATE_INTERNAL_FORMAT); } if ($period == self::REPORT_PERIOD_TYPE_YEAR) { $dateStart = new \Magento\Framework\Stdlib\DateTime\Date(date("Y", $start->getTimestamp()), DateTime::DATE_INTERNAL_FORMAT); } $dateEnd = new \Magento\Framework\Stdlib\DateTime\Date($to, DateTime::DATE_INTERNAL_FORMAT); while ($dateStart->compare($dateEnd) <= 0) { switch ($period) { case self::REPORT_PERIOD_TYPE_DAY: $t = $dateStart->toString('yyyy-MM-dd'); $dateStart->addDay(1); break; case self::REPORT_PERIOD_TYPE_MONTH: $t = $dateStart->toString('yyyy-MM'); $dateStart->addMonth(1); break; case self::REPORT_PERIOD_TYPE_YEAR: $t = $dateStart->toString('yyyy'); $dateStart->addYear(1); break; } $intervals[] = $t; } return $intervals; }
/** * Get current attribute value for specified product * * @param \Magento\Catalog\Model\Product $product * @return null|string */ public function getProductAttributeValue($product) { if (is_null($this->getAttributeId())) { return null; } $productAttribute = $this->_gsProduct->getProductAttribute($product, $this->getAttributeId()); if (is_null($productAttribute)) { return null; } if ($productAttribute->getFrontendInput() == 'date' || $productAttribute->getBackendType() == 'date') { $value = $product->getData($productAttribute->getAttributeCode()); if (empty($value) || !\Zend_Date::isDate($value, \Zend_Date::ISO_8601)) { return null; } $date = new \Magento\Framework\Stdlib\DateTime\Date($value, \Zend_Date::ISO_8601); $value = $date->toString(\Zend_Date::ATOM); } else { $value = $productAttribute->getFrontend()->getValue($product); } return $value; }
/** * Check attribute unique value * * @param AbstractAttribute $attribute * @param \Magento\Framework\Object $object * @return bool */ public function checkAttributeUniqueValue(AbstractAttribute $attribute, $object) { $adapter = $this->_getReadAdapter(); $select = $adapter->select(); if ($attribute->getBackend()->getType() === 'static') { $value = $object->getData($attribute->getAttributeCode()); $bind = array('entity_type_id' => $this->getTypeId(), 'attribute_code' => trim($value)); $select->from($this->getEntityTable(), $this->getEntityIdField())->where('entity_type_id = :entity_type_id')->where($attribute->getAttributeCode() . ' = :attribute_code'); } else { $value = $object->getData($attribute->getAttributeCode()); if ($attribute->getBackend()->getType() == 'datetime') { $date = new \Magento\Framework\Stdlib\DateTime\Date($value, \Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT); $value = $date->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT); } $bind = array('entity_type_id' => $this->getTypeId(), 'attribute_id' => $attribute->getId(), 'value' => trim($value)); $select->from($attribute->getBackend()->getTable(), $attribute->getBackend()->getEntityIdField())->where('entity_type_id = :entity_type_id')->where('attribute_id = :attribute_id')->where('value = :value'); } $data = $adapter->fetchCol($select, $bind); if ($object->getId()) { if (isset($data[0])) { return $data[0] == $object->getId(); } return true; } return !count($data); }
/** * Apply sorting and filtering to collection * * @return $this */ protected function _prepareCollection() { $filter = $this->getParam($this->getVarNameFilter(), null); if (is_null($filter)) { $filter = $this->_defaultFilter; } if (is_string($filter)) { $data = array(); $filter = base64_decode($filter); parse_str(urldecode($filter), $data); if (!isset($data['report_from'])) { // getting all reports from 2001 year $date = new \Magento\Framework\Stdlib\DateTime\Date(mktime(0, 0, 0, 1, 1, 2001)); $data['report_from'] = $date->toString($this->_localeDate->getDateFormat('short')); } if (!isset($data['report_to'])) { // getting all reports from 2001 year $date = new \Magento\Framework\Stdlib\DateTime\Date(); $data['report_to'] = $date->toString($this->_localeDate->getDateFormat('short')); } $this->_setFilterValues($data); } elseif ($filter && is_array($filter)) { $this->_setFilterValues($filter); } elseif (0 !== sizeof($this->_defaultFilter)) { $this->_setFilterValues($this->_defaultFilter); } /** @var $collection \Magento\Reports\Model\Resource\Report\Collection */ $collection = $this->getCollection(); if ($collection) { $collection->setPeriod($this->getFilter('report_period')); if ($this->getFilter('report_from') && $this->getFilter('report_to')) { /** * Validate from and to date */ try { $from = $this->_localeDate->date($this->getFilter('report_from'), \Zend_Date::DATE_SHORT, null, false); $to = $this->_localeDate->date($this->getFilter('report_to'), \Zend_Date::DATE_SHORT, null, false); $collection->setInterval($from, $to); } catch (\Exception $e) { $this->_errors[] = __('Invalid date specified'); } } $collection->setStoreIds($this->_getAllowedStoreIds()); if ($this->getSubReportSize() !== null) { $collection->setPageSize($this->getSubReportSize()); } $this->_eventManager->dispatch('adminhtml_widget_grid_filter_collection', array('collection' => $this->getCollection(), 'filter_values' => $this->_filterValues)); } return $this; }
/** * Retrieve transitions for offsets of given timezone * * @param string $timezone * @param mixed $from * @param mixed $to * @return array */ protected function _getTZOffsetTransitions($timezone, $from = null, $to = null) { $tzTransitions = array(); try { if (!empty($from)) { $from = new \Magento\Framework\Stdlib\DateTime\Date($from, \Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT); $from = $from->getTimestamp(); } $to = new \Magento\Framework\Stdlib\DateTime\Date($to, \Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT); $nextPeriod = $this->_getWriteAdapter()->formatDate($to->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT)); $to = $to->getTimestamp(); $dtz = new \DateTimeZone($timezone); $transitions = $dtz->getTransitions(); $dateTimeObject = new \Magento\Framework\Stdlib\DateTime\Date('c'); for ($i = count($transitions) - 1; $i >= 0; $i--) { $tr = $transitions[$i]; try { $this->timezoneValidator->validate($tr['ts'], $to); } catch (\Magento\Framework\Stdlib\DateTime\Timezone\ValidationException $e) { continue; } $dateTimeObject->set($tr['time']); $tr['time'] = $this->_getWriteAdapter()->formatDate($dateTimeObject->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT)); $tzTransitions[$tr['offset']][] = array('from' => $tr['time'], 'to' => $nextPeriod); if (!empty($from) && $tr['ts'] < $from) { break; } $nextPeriod = $tr['time']; } } catch (\Exception $e) { $this->_logger->logException($e); } return $tzTransitions; }
/** * Parse user input value and return cart prepared value * * @param string $optionValue * @param array $productOptionValues Values for product option * @return string|null */ public function parseOptionValue($optionValue, $productOptionValues) { $timestamp = strtotime($optionValue); if ($timestamp === false || $timestamp == -1) { return null; } $date = new \Magento\Framework\Stdlib\DateTime\Date($timestamp); return $date->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT); }
/** * Determine nearest possible payment start date * * @param \Magento\Framework\Stdlib\DateTime\DateInterface $minAllowed * @return $this */ protected function setNearestStartDatetime(\Magento\Framework\Stdlib\DateTime\DateInterface $minAllowed = null) { // TODO: implement proper logic with invoking payment method instance $date = $minAllowed; if (!$date || $date->getTimestamp() < time()) { $date = new \Magento\Framework\Stdlib\DateTime\Date(time()); } $this->setStartDatetime($date->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT)); return $this; }
/** * Parse CSV file and collect report rows * * @param string $localCsv Path to CSV file * @param string $format CSV format(column names) * @return $this */ public function parseCsv($localCsv, $format = 'new') { $this->_rows = array(); $sectionColumns = $this->_csvColumns[$format]['section_columns']; $rowMap = $this->_csvColumns[$format]['rowmap']; $flippedSectionColumns = array_flip($sectionColumns); $stream = $this->_tmpDirectory->openFile($localCsv); while ($line = $stream->readCsv()) { if (empty($line)) { // The line was empty, so skip it. continue; } $lineType = $line[0]; switch ($lineType) { case 'RH': // Report header. $lastModified = new \Magento\Framework\Stdlib\DateTime\Date($line[1]); $this->setReportLastModified($lastModified->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT)); //$this->setAccountId($columns[2]); -- probably we'll just take that from the section header... break; case 'FH': // File header. // Nothing interesting here, move along break; case 'SH': // Section header. $this->setAccountId($line[3]); $this->loadByAccountAndDate(); break; case 'CH': // Section columns. // In case ever the column order is changed, we will have the items recorded properly // anyway. We have named, not numbered columns. for ($i = 1; $i < count($line); $i++) { $sectionColumns[$line[$i]] = $i; } $flippedSectionColumns = array_flip($sectionColumns); break; case 'SB': // Section body. $bodyItem = array(); for ($i = 1; $i < count($line); $i++) { $bodyItem[$rowMap[$flippedSectionColumns[$i]]] = $line[$i]; } $this->_rows[] = $bodyItem; break; case 'SC': // Section records count. // Section records count. case 'RC': // Report records count. // Report records count. case 'SF': // Section footer. // Section footer. case 'FF': // File footer. // File footer. case 'RF': // Report footer. // Nothing to see here, move along break; default: break; } } return $this; }