array('title')); protected $_statuses = array( 'active' => 'Active', 'upcoming' => 'Not Active', 'expired' => 'Expired', '' => 'All', ); public function getStatuses() { return $this->_statuses; } protected function _filter(Zend_Db_Select $select) { if (!$this->hasFilter()) { return $this; } parent::_filter($select); if (!empty($this->_filter['status'])) { switch ($this->_filter['status']) { case 'active': $select->where('`startDate` <= CURRENT_DATE()'); $select->where('`hasNoExpirationDate` = "y" OR `endDate` >= CURRENT_DATE()'); break; case 'upcoming': $select->where('`startDate` > CURRENT_DATE()'); break; case 'expired': $select->where('`hasNoExpirationDate` = "n"'); $select->where('`endDate` < CURRENT_DATE()'); break; } } return $this; } public function getListSelect() { if (null === $this->_select) { parent::getListSelect(); $this->_select->joinLeft( $this->_getPair('Product'), '`Product`.`id` = `' . $this->_tableAlias . '`.`productId`', array('productTitle' => 'title') ); } return $this->_select; } public function getData4Autocomplete($filter) { $select = $this->_db->select(); $select->from($this->_getPair('Product'), array('value' => 'id', 'title')); Qs_Db_Filter::where($select, array('Product' => array('title')), $filter); $select->limit(30); return $this->_db->fetchAll($select); } public function getProductTitleById($productId) { $productId = (string) $productId; $select = $this->_db->select(); $select->from($this->_getPair('Product'), array('title')); $select->where('`Product`.`id` = ?', $productId, Qs_Db::INT_TYPE); return $this->_db->fetchOne($select); } /** * Return data for code * * @param string $code * @return array|bool */ public function getDataByCode($code) { return $this->_getTable()->searchBy(array('code' => $code)); } public function generateCode($iterations = self::CODE_GENERATION_ITERATIONS) { $codeValidator = new Qs_Validate_Unique(new Qs_Db_Table('Promo'), 'code', $this->getData('id')); $expirationWhere = '(`hasNoExpirationDate` = "y") OR (`hasNoExpirationDate` = "n" AND `endDate` < CURRENT_DATE())'; $codeValidator->setWhere($expirationWhere); $iterations = (int) $iterations; $iterations = ($iterations > 0) ? $iterations : static::CODE_GENERATION_ITERATIONS; $code = ''; $unique = false; while ($unique == false && $iterations > 0) { $code = strtoupper(substr(md5(rand().microtime(true).rand()), 0, static::DEFAULT_CODE_LENGTH)); $code = strtoupper($code); if ($codeValidator->isValid($code)) { $unique = true; } else { $code = ''; } $iterations--; } return $code; } public function getObjectInfo() { $data = parent::getObjectInfo(); $data['itemTitle'] = strip_tags($this->_renderDescription($data)); return $data; } protected function _renderDescription(array $data) { $text = ''; if (empty($data)) { return $text; } if ($data['type'] == App_ECommerce_Promo_Admin_View::PROMO_TYPE_PERCENT) { require_once 'lib/Smarty/app_plugins/modifier.money.php'; $text .= smarty_modifier_money($data['value'], 2, true) . '% '; } else { $text .= '$' . number_format($data['value'], 2, '.', ',') . ' '; } $text .= 'off '; if ('y' == $data['isRelated'] && $data['productId']) { if (!$data['productTitle']) { $data['productTitle'] = $this->getProductTitleById($data['productId']); } if ($data['productTitle']) { $product = htmlspecialchars($data['productTitle']); if ($data['productUrl']) { $product = '' . $product . ''; } } else { $product = 'UNKNOWN PRODUCT'; } $text .= 'for ' . $product . ' '; } if ($data['minOrderValue'] > 0) { $text .= 'for orders over $' . number_format($data['minOrderValue'], 2, '.', ','); } return $text; } protected function _prepareList(&$list) { if (!empty($list)) { $productUrl = Qs_SiteMap::findFirst(null, array('type' => 'ECommerce_Product_Admin_'), null, 'url') . '?action=edit&id='; foreach (array_keys($list) as $key) { $this->_prepareRow($list[$key]); if ($list[$key]['productId']) { $list[$key]['productUrl'] = $productUrl . $list[$key]['productId']; } $list[$key]['description'] = $this->_renderDescription($list[$key]); } } return $this; } }