';
protected $_columns4ExportCsv = array(
'added' => 'Submitted',
'billingFirstName' => 'Billing First Name',
'billingLastName' => 'Billing Last Name',
'billingAddress' => 'Billing Address',
'billingCity' => 'Billing City',
'billingState' => 'Billing State',
'billingZip' => 'Billing Zip',
'billingPhone' => 'Billing Phone',
'billingEmail' => 'Billing Email',
'shippingFirstName' => 'Shipping First Name',
'shippingLastName' => 'Shipping Last Name',
'shippingAddress' => 'Shipping Address',
'shippingCity' => 'Shipping City',
'shippingState' => 'Shipping State',
'shippingZip' => 'Shipping Zip',
'deliveryAddressType' => 'Delivery Address Type',
'shippingNameTitle' => 'Shipping Name',
'shippingServiceCodeTitle' => 'Shipping Service Code',
'shippingRate' => 'Shipping Rate',
'paymentTypeTitle' => 'Payment Type',
'paid' => 'Paid',
'status' => 'Status',
'total' => 'Total',
'paymentCardNumber' => 'Card Number',
'paymentExpirationDate' => 'Expiration Date',
'paymentPoNumber' => 'Po Number',
'adminAdditionalNote' => 'Staff Note',
'userAdditionalNote' => 'User Note'
);
protected $_exportFileName;
/** Каже на зміну яких ордер статусів слати юзеру мила */
protected $_orderStatusesForUserMail = array(
App_ECommerce_Order_Admin_Obj::STATUS_ORDER_SENT
);
/** Каже на зміну яких ордер статусів слати юзеру мила */
protected $_paidStatusesForUserMail = array(
App_ECommerce_Order_Admin_Obj::STATUS_PAID_YES
);
protected $_initStatuses = array();
protected function _init()
{
parent::_init();
$this->_messageTemplates[self::MSG_ORDER_STATUS_CHANGED] = 'Order status for selected item(s) has been changed';
$this->_messageTemplates[self::MSG_NO_DATA] = 'No selected items';
return $this;
}
/**
* Return tabs object
*
* @static
* @return App_ECommerce_Order_Admin_Tab
*/
public static function getTabs()
{
$options = array('sitemapSelector' => static::$_sitemapSelector);
return new App_ECommerce_Order_Admin_Tab($options);
}
protected function _doEdit()
{
$this->_postEdit();
$tabs = self::getTabs();
$url = BASE_URL_LANGUAGE . '/' . $tabs->getRootFullAlias() . '/' . $tabs->getDefaultTabAlias()
. '/edit/' . $this->_getDataObj()->getPrimaryKey();
$this->redirect($url);
}
protected function _doNew()
{
if (false === $this->_getDataObj()->createEmptyOrder()) {
$this->_setBackErrors($this->_getDataObj()->getErrors());
$this->_doBack();
}
$this->_postNew();
$tabs = self::getTabs();
$url = BASE_URL_LANGUAGE . '/' . $tabs->getRootFullAlias() . '/' . $tabs->getDefaultTabAlias()
. '/new/' . $this->_getDataObj()->getPrimaryKey();
$this->redirect($url);
}
protected function _addListItem($list = null)
{
$this->_doc->addScript('js/app/ECommerce/order/admin-action-form-toolbar.js');
return parent::_addListItem($list);
}
protected function _doExportCsv()
{
if ($this->_getDataObj()->hasFilter()) {
$form = $this->_getFilterForm();
if ($form->validate()) {
$this->_getDataObj()->addFilter($form->getValues());
}
}
$options = array();
$options['order'] = array($this->_getList()->getOrderBy());
$this->_dataObj->setSelectOptions($options);
$stmt = $this->_getDataObj()->getListStatement($options);
$fp = fopen('php://output', 'w');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D,d M YH:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-type: text/comma-separated-values');
header('Content-Disposition: attachment; filename="' . $this->_getExportFileName() . '"');
$columns = $this->_getColumns4ExportCsv();
fputcsv($fp, $columns);
$blankRow = array_combine(array_keys($columns), array_fill(0, count($columns), ''));
while (($row = $stmt->fetch())) {
$row = $this->_getRow4ExportCsv($row);
$row = array_intersect_key($row, $columns);
$row = array_merge($blankRow, $row);
fputcsv($fp, $row);
}
fclose($fp);
$this->_postExportCsv();
exit;
}
protected function _postExportCsv()
{
$this->_log();
return $this;
}
protected function _getExportFileName()
{
if (null == $this->_exportFileName) {
$this->_exportFileName = $this->getConfig('itemsName') . ' - ' . date('F d, Y') . '.csv';
}
return $this->_exportFileName;
}
protected function _getColumns4ExportCsv()
{
if (null === $this->_columns4ExportCsv) {
$this->_columns4ExportCsv = array('added' => 'Submitted');
$fields = array_keys(Qs_Array::excludeKey($this->_getDataObj()->getMetaData(), 'id', 'added', 'changed'));
foreach ($fields as $field) {
$this->_columns4ExportCsv[$field] = ucfirst(preg_replace('/([a-z])([A-Z])/', '$1 $2', $field));
}
}
return $this->_columns4ExportCsv;
}
protected function _getRow4ExportCsv(&$data)
{
$row = str_replace(array("\n", "\r"), ' ', $data);
return $row;
}
protected function _getDefaultLinks()
{
$links = parent::_getDefaultLinks();
$params = array();
if (($orderBy = Qs_Request::getGetValue('orderBy'))) {
$params['orderBy'] = $orderBy;
}
$filter = $this->_dataObj->getFilter();
foreach ($filter as $field => $value) {
if ($value) {
$params[$field] = $value;
}
}
$params['action'] = 'exportCsv';
$links['exportCsv'] = array('title' => 'Export to .csv', 'url' => $this->url($params));
return $links;
}
protected function _doView()
{
$item = $this->_getDataObj()->getData();
if (empty($item)) {
$this->_doc->display404();
}
$form = $this->_getViewForm(array(
'defaults' => $item,
'cancelUrl' => Qs_SiteMap::findFirst(static::$_sitemapSelector, 'url')
));
$form->setDefaults();
if ($item['status'] == App_ECommerce_Order_Admin_Obj::STATUS_ORDER_CLOSED) {
/** @var Zend_Form_Element $element */
foreach ($form->getElements() as $element) {
$element->setAttribs(array(
'disabled' => 'disabled',
'readonly' => 'readonly'
));
}
}
$item['adminForm'] = $form;
$item['config'] = $this->getConfig()->toArray();
$item['printVersion'] = $this->_printVersion;
$item['tpl'] = $this->getTemplate('view.tpl');
$item['backUrl'] = ($backUrl = $this->_getBackUrl(CURRENT_PAGE_FINAL)) ? $backUrl : CURRENT_PAGE_FINAL;
$headerAppend = ' #' . $item['id'] . ' on ' . date('m/d/Y g:i A') . ': '
. ($item['paid'] == 'y'
? 'Paid'
: 'Not Paid'
);
$this->_doc->appendHeader($headerAppend);
$this->_addItem($item);
$this->_postView();
return $this;
}
protected function _doChangeOrderStatus()
{
return $this->_do404();
}
protected function _doChangeOrderStatusAjax()
{
$result = $this->_changeStatus('status');
$this->_displayJson($result);
exit;
}
protected function _doChangePaidStatus()
{
return $this->_do404();
}
protected function _doChangePaidStatusAjax()
{
$result = $this->_changeStatus('paid');
$this->_displayJson($result);
exit;
}
protected function _changeStatus($field)
{
$status = Qs_Request::getPostValue('value');
$selectedPrimary = Qs_Request::getPostValue('selectedPrimary');
if ($status && $selectedPrimary && is_array($selectedPrimary) &&
false !== ($updatedPrimary = $this->_getDataObj()->changeStatuses($selectedPrimary, $field, $status))
) {
$isError = false;
$message = $this->_createMessage(self::MSG_ORDER_STATUS_CHANGED);
$this->_setBackMessage($message);
$this->_postChangeStatus($updatedPrimary, $field, $status);
} else {
$isError = true;
$message = $this->_createMessage(self::MSG_NO_DATA);
}
return array('isError' => $isError, 'message' => $message);
}
protected function _prepareMailData(array $data)
{
$data['orderId'] = htmlspecialchars($data['id']);
return $data;
}
protected function _postChangeStatus($updatedPrimary, $field, $status)
{
if (is_array($updatedPrimary) && sizeof($updatedPrimary)) {
if ($this->_checkNecessarySendMail($field, $status)) {
foreach ($updatedPrimary as $primary) {
if (!ctype_digit((string) $primary)) {
continue;
}
$this->_getDataObj()->setPrimaryKey($primary);
$order = $this->_getDataObj()->clearData()->getData();
if (is_array($order) && sizeof($order)) {
$mailSuffix = $this->_getMailSuffix($field, $status);
$this->sendUserNotification($order, $mailSuffix);
}
}
}
$this->_logChangeStatus($updatedPrimary);
}
return $this;
}
/**
* @param array $orders list of orders id
* @return App_ECommerce_Order_Admin_View
*/
protected function _logChangeStatus(array $orders)
{
if ($this->_hasLog && $orders) {
if (count($orders) > 1) {
$message = 'Updated %itemsName% ';
foreach ($orders as $id) {
$message .= '#' . $id . ', ';
}
$message = substr($message, 0, -2);
} else {
$message = 'Updated %itemName% #' . reset($orders);
}
$this->_getLog()->setAction('changeStatus', $message);
$this->_getLog()->write('changeStatus', $this->_getDataObj()->getObjectInfo());
}
return $this;
}
protected function _checkNecessarySendMail($field, $status)
{
if (($field == 'status' && in_array($status, $this->_orderStatusesForUserMail))
|| ($field == 'paid' && in_array($status, $this->_paidStatusesForUserMail))
) {
return true;
}
return false;
}
protected function _getMailSuffix($field, $status)
{
return ucfirst(($field == 'status' ? 'order' : 'paid')) . 'Status'
. ($field == 'paid' ? (ucfirst(($status == 'n' ? 'no' : 'yes'))) : ucfirst($status));
}
protected function _initFromForm(Qs_Form $form)
{
$this->_initStatuses = array_intersect_key(
$this->_getDataObj()->getData(),
array_flip(array('status', 'paid'))
);
return parent::_initFromForm($form);
}
protected function _postUpdate()
{
$data = $this->_getDataObj()->getData();
foreach (array('paid', 'status') as $field) {
if ($this->_initStatuses[$field] != $data[$field]) {
$this->_postChangeStatus(array($this->_getDataObj()->getPrimaryKey()), $field, $data[$field]);
}
}
return parent::_postUpdate();
}
public function getRootFullAlias()
{
if (empty($this->_rootFullAlias)) {
$this->_rootFullAlias = Qs_SiteMap::findFirst('', 'fullAlias');
}
return $this->_rootFullAlias;
}
protected function _doCancelOrder()
{
$this->_getDataObj()->cancelOrder();
$this->_doBack();
}
}