1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34: 35: 36:
37: abstract class Mage_Core_Controller_Varien_Action
38: {
39: const FLAG_NO_CHECK_INSTALLATION = 'no-install-check';
40: const FLAG_NO_DISPATCH = 'no-dispatch';
41: const FLAG_NO_PRE_DISPATCH = 'no-preDispatch';
42: const FLAG_NO_POST_DISPATCH = 'no-postDispatch';
43: const FLAG_NO_START_SESSION = 'no-startSession';
44: const FLAG_NO_DISPATCH_BLOCK_EVENT = 'no-beforeGenerateLayoutBlocksDispatch';
45: const FLAG_NO_COOKIES_REDIRECT = 'no-cookies-redirect';
46:
47: const PARAM_NAME_SUCCESS_URL = 'success_url';
48: const PARAM_NAME_ERROR_URL = 'error_url';
49: const PARAM_NAME_REFERER_URL = 'referer_url';
50: const PARAM_NAME_BASE64_URL = 'r64';
51: const PARAM_NAME_URL_ENCODED = 'uenc';
52:
53: const PROFILER_KEY = 'mage::dispatch::controller::action';
54:
55: 56: 57: 58: 59:
60: protected $_request;
61:
62: 63: 64: 65: 66:
67: protected $_response;
68:
69: 70: 71: 72: 73:
74: protected $_realModuleName;
75:
76: 77: 78: 79: 80: 81: 82:
83: protected $_flags = array();
84:
85: 86: 87: 88: 89:
90: protected $_cookieCheckActions = array();
91:
92: 93: 94: 95: 96:
97: protected $_currentArea;
98:
99: 100: 101: 102: 103: 104:
105: protected $_sessionNamespace;
106:
107: 108: 109: 110: 111: 112:
113: protected $_isLayoutLoaded = false;
114:
115: 116: 117: 118: 119: 120:
121: protected $_titles = array();
122:
123: 124: 125: 126: 127: 128:
129: protected $_removeDefaultTitle = false;
130:
131: 132: 133: 134: 135: 136: 137:
138: public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
139: {
140: $this->_request = $request;
141: $this->_response= $response;
142:
143: Mage::app()->getFrontController()->setAction($this);
144:
145: $this->_construct();
146: }
147:
148: protected function _construct()
149: {
150: }
151:
152: public function hasAction($action)
153: {
154: return is_callable(array($this, $this->getActionMethodName($action)));
155: }
156:
157: 158: 159: 160: 161:
162: public function getRequest()
163: {
164: return $this->_request;
165: }
166:
167: 168: 169: 170: 171:
172: public function getResponse()
173: {
174: return $this->_response;
175: }
176:
177: 178: 179: 180: 181: 182: 183:
184: public function getFlag($action, $flag='')
185: {
186: if (''===$action) {
187: $action = $this->getRequest()->getActionName();
188: }
189: if (''===$flag) {
190: return $this->_flags;
191: }
192: elseif (isset($this->_flags[$action][$flag])) {
193: return $this->_flags[$action][$flag];
194: }
195: else {
196: return false;
197: }
198: }
199:
200: 201: 202: 203: 204: 205: 206: 207:
208: public function setFlag($action, $flag, $value)
209: {
210: if (''===$action) {
211: $action = $this->getRequest()->getActionName();
212: }
213: $this->_flags[$action][$flag] = $value;
214: return $this;
215: }
216:
217: 218: 219: 220: 221: 222: 223:
224: public function getFullActionName($delimiter='_')
225: {
226: return $this->getRequest()->getRequestedRouteName().$delimiter.
227: $this->getRequest()->getRequestedControllerName().$delimiter.
228: $this->getRequest()->getRequestedActionName();
229: }
230:
231: 232: 233: 234: 235:
236: public function getLayout()
237: {
238: return Mage::getSingleton('core/layout');
239: }
240:
241: 242: 243: 244: 245: 246: 247: 248:
249: public function loadLayout($handles = null, $generateBlocks = true, $generateXml = true)
250: {
251:
252: if (false!==$handles && ''!==$handles) {
253: $this->getLayout()->getUpdate()->addHandle($handles ? $handles : 'default');
254: }
255:
256:
257: $this->addActionLayoutHandles();
258:
259: $this->loadLayoutUpdates();
260:
261: if (!$generateXml) {
262: return $this;
263: }
264: $this->generateLayoutXml();
265:
266: if (!$generateBlocks) {
267: return $this;
268: }
269: $this->generateLayoutBlocks();
270: $this->_isLayoutLoaded = true;
271:
272: return $this;
273: }
274:
275: public function addActionLayoutHandles()
276: {
277: $update = $this->getLayout()->getUpdate();
278:
279:
280: $update->addHandle('STORE_'.Mage::app()->getStore()->getCode());
281:
282:
283: $package = Mage::getSingleton('core/design_package');
284: $update->addHandle(
285: 'THEME_'.$package->getArea().'_'.$package->getPackageName().'_'.$package->getTheme('layout')
286: );
287:
288:
289: $update->addHandle(strtolower($this->getFullActionName()));
290:
291: return $this;
292: }
293:
294: public function loadLayoutUpdates()
295: {
296: $_profilerKey = self::PROFILER_KEY . '::' .$this->getFullActionName();
297:
298:
299: Mage::dispatchEvent(
300: 'controller_action_layout_load_before',
301: array('action'=>$this, 'layout'=>$this->getLayout())
302: );
303:
304:
305: Varien_Profiler::start("$_profilerKey::layout_load");
306: $this->getLayout()->getUpdate()->load();
307: Varien_Profiler::stop("$_profilerKey::layout_load");
308:
309: return $this;
310: }
311:
312: public function generateLayoutXml()
313: {
314: $_profilerKey = self::PROFILER_KEY . '::' . $this->getFullActionName();
315:
316: if(!$this->getFlag('', self::FLAG_NO_DISPATCH_BLOCK_EVENT)) {
317: Mage::dispatchEvent(
318: 'controller_action_layout_generate_xml_before',
319: array('action'=>$this, 'layout'=>$this->getLayout())
320: );
321: }
322:
323:
324: Varien_Profiler::start("$_profilerKey::layout_generate_xml");
325: $this->getLayout()->generateXml();
326: Varien_Profiler::stop("$_profilerKey::layout_generate_xml");
327:
328: return $this;
329: }
330:
331: public function generateLayoutBlocks()
332: {
333: $_profilerKey = self::PROFILER_KEY . '::' . $this->getFullActionName();
334:
335: if(!$this->getFlag('', self::FLAG_NO_DISPATCH_BLOCK_EVENT)) {
336: Mage::dispatchEvent(
337: 'controller_action_layout_generate_blocks_before',
338: array('action'=>$this, 'layout'=>$this->getLayout())
339: );
340: }
341:
342:
343: Varien_Profiler::start("$_profilerKey::layout_generate_blocks");
344: $this->getLayout()->generateBlocks();
345: Varien_Profiler::stop("$_profilerKey::layout_generate_blocks");
346:
347: if(!$this->getFlag('', self::FLAG_NO_DISPATCH_BLOCK_EVENT)) {
348: Mage::dispatchEvent(
349: 'controller_action_layout_generate_blocks_after',
350: array('action'=>$this, 'layout'=>$this->getLayout())
351: );
352: }
353:
354: return $this;
355: }
356:
357: 358: 359: 360: 361: 362:
363: public function renderLayout($output='')
364: {
365: $_profilerKey = self::PROFILER_KEY . '::' . $this->getFullActionName();
366:
367: if ($this->getFlag('', 'no-renderLayout')) {
368: return;
369: }
370:
371: if (Mage::app()->getFrontController()->getNoRender()) {
372: return;
373: }
374:
375: $this->_renderTitles();
376:
377: Varien_Profiler::start("$_profilerKey::layout_render");
378:
379:
380: if (''!==$output) {
381: $this->getLayout()->addOutputBlock($output);
382: }
383:
384: Mage::dispatchEvent('controller_action_layout_render_before');
385: Mage::dispatchEvent('controller_action_layout_render_before_'.$this->getFullActionName());
386:
387:
388: $this->getLayout()->setDirectOutput(false);
389:
390: $output = $this->getLayout()->getOutput();
391: Mage::getSingleton('core/translate_inline')->processResponseBody($output);
392: $this->getResponse()->appendBody($output);
393: Varien_Profiler::stop("$_profilerKey::layout_render");
394:
395: return $this;
396: }
397:
398: public function dispatch($action)
399: {
400: try {
401: $actionMethodName = $this->getActionMethodName($action);
402:
403: if (!is_callable(array($this, $actionMethodName))) {
404: $actionMethodName = 'norouteAction';
405: }
406:
407: Varien_Profiler::start(self::PROFILER_KEY.'::predispatch');
408: $this->preDispatch();
409: Varien_Profiler::stop(self::PROFILER_KEY.'::predispatch');
410:
411: if ($this->getRequest()->isDispatched()) {
412: 413: 414:
415: if (!$this->getFlag('', self::FLAG_NO_DISPATCH)) {
416: $_profilerKey = self::PROFILER_KEY.'::'.$this->getFullActionName();
417:
418: Varien_Profiler::start($_profilerKey);
419: $this->$actionMethodName();
420: Varien_Profiler::stop($_profilerKey);
421:
422: Varien_Profiler::start(self::PROFILER_KEY.'::postdispatch');
423: $this->postDispatch();
424: Varien_Profiler::stop(self::PROFILER_KEY.'::postdispatch');
425: }
426: }
427: }
428: catch (Mage_Core_Controller_Varien_Exception $e) {
429:
430: foreach ($e->getResultFlags() as $flagData) {
431: list($action, $flag, $value) = $flagData;
432: $this->setFlag($action, $flag, $value);
433: }
434:
435: list($method, $parameters) = $e->getResultCallback();
436: switch ($method) {
437: case Mage_Core_Controller_Varien_Exception::RESULT_REDIRECT:
438: list($path, $arguments) = $parameters;
439: $this->_redirect($path, $arguments);
440: break;
441: case Mage_Core_Controller_Varien_Exception::RESULT_FORWARD:
442: list($action, $controller, $module, $params) = $parameters;
443: $this->_forward($action, $controller, $module, $params);
444: break;
445: default:
446: $actionMethodName = $this->getActionMethodName($method);
447: $this->getRequest()->setActionName($method);
448: $this->$actionMethodName($method);
449: break;
450: }
451: }
452: }
453:
454: 455: 456: 457: 458: 459:
460: public function getActionMethodName($action)
461: {
462: return $action . 'Action';
463: }
464:
465: 466: 467: 468: 469:
470: public function preDispatch()
471: {
472: if (!$this->getFlag('', self::FLAG_NO_CHECK_INSTALLATION)) {
473: if (!Mage::isInstalled()) {
474: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
475: $this->_redirect('install');
476: return;
477: }
478: }
479:
480:
481: if (Mage::isInstalled() && !Mage::app()->getStore()->getIsActive()) {
482: Mage::app()->throwStoreException();
483: }
484:
485: if ($this->_rewrite()) {
486: return;
487: }
488:
489: if (!$this->getFlag('', self::FLAG_NO_START_SESSION)) {
490: $checkCookie = in_array($this->getRequest()->getActionName(), $this->_cookieCheckActions)
491: && !$this->getRequest()->getParam('nocookie', false);
492: $cookies = Mage::getSingleton('core/cookie')->get();
493:
494: $session = Mage::getSingleton('core/session', array('name' => $this->_sessionNamespace))->start();
495:
496: if (empty($cookies)) {
497: if ($session->getCookieShouldBeReceived()) {
498: $this->setFlag('', self::FLAG_NO_COOKIES_REDIRECT, true);
499: $session->unsCookieShouldBeReceived();
500: $session->setSkipSessionIdFlag(true);
501: } elseif ($checkCookie) {
502: if (isset($_GET[$session->getSessionIdQueryParam()]) && Mage::app()->getUseSessionInUrl()
503: && $this->_sessionNamespace != Mage_Adminhtml_Controller_Action::SESSION_NAMESPACE
504: ) {
505: $session->setCookieShouldBeReceived(true);
506: } else {
507: $this->setFlag('', self::FLAG_NO_COOKIES_REDIRECT, true);
508: }
509: }
510: }
511: }
512:
513: Mage::app()->loadArea($this->getLayout()->getArea());
514:
515: if ($this->getFlag('', self::FLAG_NO_COOKIES_REDIRECT)
516: && Mage::getStoreConfig('web/browser_capabilities/cookies')
517: ) {
518: $this->_forward('noCookies', 'index', 'core');
519: return;
520: }
521:
522: if ($this->getFlag('', self::FLAG_NO_PRE_DISPATCH)) {
523: return;
524: }
525:
526: Varien_Autoload::registerScope($this->getRequest()->getRouteName());
527:
528: Mage::dispatchEvent('controller_action_predispatch', array('controller_action' => $this));
529: Mage::dispatchEvent('controller_action_predispatch_' . $this->getRequest()->getRouteName(),
530: array('controller_action' => $this));
531: Mage::dispatchEvent('controller_action_predispatch_' . $this->getFullActionName(),
532: array('controller_action' => $this));
533: }
534:
535: 536: 537:
538: public function postDispatch()
539: {
540: if ($this->getFlag('', self::FLAG_NO_POST_DISPATCH)) {
541: return;
542: }
543:
544: Mage::dispatchEvent(
545: 'controller_action_postdispatch_'.$this->getFullActionName(),
546: array('controller_action'=>$this)
547: );
548: Mage::dispatchEvent(
549: 'controller_action_postdispatch_'.$this->getRequest()->getRouteName(),
550: array('controller_action'=>$this)
551: );
552: Mage::dispatchEvent('controller_action_postdispatch', array('controller_action'=>$this));
553: }
554:
555: public function norouteAction($coreRoute = null)
556: {
557: $status = ( $this->getRequest()->getParam('__status__') )
558: ? $this->getRequest()->getParam('__status__')
559: : new Varien_Object();
560:
561: Mage::dispatchEvent('controller_action_noroute', array('action'=>$this, 'status'=>$status));
562: if ($status->getLoaded() !== true
563: || $status->getForwarded() === true
564: || !is_null($coreRoute) ) {
565: $this->loadLayout(array('default', 'noRoute'));
566: $this->renderLayout();
567: } else {
568: $status->setForwarded(true);
569: $this->_forward(
570: $status->getForwardAction(),
571: $status->getForwardController(),
572: $status->getForwardModule(),
573: array('__status__' => $status));
574: }
575: }
576:
577: public function noCookiesAction()
578: {
579: $redirect = new Varien_Object();
580: Mage::dispatchEvent('controller_action_nocookies', array(
581: 'action' => $this,
582: 'redirect' => $redirect
583: ));
584:
585: if ($url = $redirect->getRedirectUrl()) {
586: $this->_redirectUrl($url);
587: }
588: elseif ($redirect->getRedirect()) {
589: $this->_redirect($redirect->getPath(), $redirect->getArguments());
590: }
591: else {
592: $this->loadLayout(array('default', 'noCookie'));
593: $this->renderLayout();
594: }
595:
596: $this->getRequest()->setDispatched(true);
597: }
598:
599: 600: 601: 602: 603: 604: 605: 606:
607: protected function _forward($action, $controller = null, $module = null, array $params = null)
608: {
609: $request = $this->getRequest();
610:
611: $request->initForward();
612:
613: if (isset($params)) {
614: $request->setParams($params);
615: }
616:
617: if (isset($controller)) {
618: $request->setControllerName($controller);
619:
620:
621: if (isset($module)) {
622: $request->setModuleName($module);
623: }
624: }
625:
626: $request->setActionName($action)
627: ->setDispatched(false);
628: }
629:
630: 631: 632: 633: 634: 635:
636: protected function _initLayoutMessages($messagesStorage)
637: {
638: if (!is_array($messagesStorage)) {
639: $messagesStorage = array($messagesStorage);
640: }
641: foreach ($messagesStorage as $storageName) {
642: $storage = Mage::getSingleton($storageName);
643: if ($storage) {
644: $block = $this->getLayout()->getMessagesBlock();
645: $block->addMessages($storage->getMessages(true));
646: $block->setEscapeMessageFlag($storage->getEscapeMessages(true));
647: $block->addStorageType($storageName);
648: }
649: else {
650: Mage::throwException(
651: Mage::helper('core')->__('Invalid messages storage "%s" for layout messages initialization', (string) $storageName)
652: );
653: }
654: }
655: return $this;
656: }
657:
658: 659: 660: 661: 662: 663:
664: public function initLayoutMessages($messagesStorage)
665: {
666: return $this->_initLayoutMessages($messagesStorage);
667: }
668:
669: 670: 671: 672: 673: 674:
675: protected function _redirectUrl($url)
676: {
677: $this->getResponse()->setRedirect($url);
678: return $this;
679: }
680:
681: 682: 683: 684: 685: 686: 687:
688: protected function _redirect($path, $arguments = array())
689: {
690: return $this->setRedirectWithCookieCheck($path, $arguments);
691: }
692:
693: 694: 695: 696: 697: 698: 699: 700:
701: public function setRedirectWithCookieCheck($path, array $arguments = array())
702: {
703:
704: $session = Mage::getSingleton('core/session', array('name' => $this->_sessionNamespace));
705: if ($session->getCookieShouldBeReceived() && Mage::app()->getUseSessionInUrl()
706: && $this->_sessionNamespace != Mage_Adminhtml_Controller_Action::SESSION_NAMESPACE
707: ) {
708: $arguments += array('_query' => array(
709: $session->getSessionIdQueryParam() => $session->getSessionId()
710: ));
711: }
712: $this->getResponse()->setRedirect(Mage::getUrl($path, $arguments));
713: return $this;
714: }
715:
716:
717: 718: 719: 720: 721: 722:
723: protected function _redirectSuccess($defaultUrl)
724: {
725: $successUrl = $this->getRequest()->getParam(self::PARAM_NAME_SUCCESS_URL);
726: if (empty($successUrl)) {
727: $successUrl = $defaultUrl;
728: }
729: if (!$this->_isUrlInternal($successUrl)) {
730: $successUrl = Mage::app()->getStore()->getBaseUrl();
731: }
732: $this->getResponse()->setRedirect($successUrl);
733: return $this;
734: }
735:
736: 737: 738: 739: 740: 741:
742: protected function _redirectError($defaultUrl)
743: {
744: $errorUrl = $this->getRequest()->getParam(self::PARAM_NAME_ERROR_URL);
745: if (empty($errorUrl)) {
746: $errorUrl = $defaultUrl;
747: }
748: if (!$this->_isUrlInternal($errorUrl)) {
749: $errorUrl = Mage::app()->getStore()->getBaseUrl();
750: }
751: $this->getResponse()->setRedirect($errorUrl);
752: return $this;
753: }
754:
755: 756: 757: 758: 759: 760:
761: protected function _redirectReferer($defaultUrl=null)
762: {
763:
764: $refererUrl = $this->_getRefererUrl();
765: if (empty($refererUrl)) {
766: $refererUrl = empty($defaultUrl) ? Mage::getBaseUrl() : $defaultUrl;
767: }
768:
769: $this->getResponse()->setRedirect($refererUrl);
770: return $this;
771: }
772:
773: 774: 775: 776: 777:
778: protected function _getRefererUrl()
779: {
780: $refererUrl = $this->getRequest()->getServer('HTTP_REFERER');
781: if ($url = $this->getRequest()->getParam(self::PARAM_NAME_REFERER_URL)) {
782: $refererUrl = $url;
783: }
784: if ($url = $this->getRequest()->getParam(self::PARAM_NAME_BASE64_URL)) {
785: $refererUrl = Mage::helper('core')->urlDecode($url);
786: }
787: if ($url = $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) {
788: $refererUrl = Mage::helper('core')->urlDecode($url);
789: }
790:
791: $refererUrl = Mage::helper('core')->escapeUrl($refererUrl);
792:
793: if (!$this->_isUrlInternal($refererUrl)) {
794: $refererUrl = Mage::app()->getStore()->getBaseUrl();
795: }
796: return $refererUrl;
797: }
798:
799: 800: 801: 802: 803: 804:
805: protected function _isUrlInternal($url)
806: {
807: if (strpos($url, 'http') !== false) {
808: 809: 810:
811: if ((strpos($url, Mage::app()->getStore()->getBaseUrl()) === 0)
812: || (strpos($url, Mage::app()->getStore()->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK, true)) === 0)
813: ) {
814: return true;
815: }
816: }
817: return false;
818: }
819:
820: 821: 822: 823: 824:
825: protected function _getRealModuleName()
826: {
827: if (empty($this->_realModuleName)) {
828: $class = get_class($this);
829: $this->_realModuleName = substr(
830: $class,
831: 0,
832: strpos(strtolower($class), '_' . strtolower($this->getRequest()->getControllerName() . 'Controller'))
833: );
834: }
835: return $this->_realModuleName;
836: }
837:
838: 839: 840: 841: 842: 843: 844: 845: 846: 847: 848: 849: 850: 851: 852: 853: 854: 855: 856: 857: 858: 859: 860: 861: 862: 863:
864: protected function _rewrite()
865: {
866: $route = $this->getRequest()->getRouteName();
867: $controller = $this->getRequest()->getControllerName();
868: $action = $this->getRequest()->getActionName();
869:
870: $rewrite = Mage::getConfig()->getNode('global/routers/'.$route.'/rewrite/'.$controller);
871: if (!$rewrite) {
872: return false;
873: }
874:
875: if (!($rewrite->actions && $rewrite->actions->$action) || $rewrite->is('override_actions')) {
876: $t = explode('/', (string)$rewrite->to);
877: if (sizeof($t)!==2 || empty($t[0]) || empty($t[1])) {
878: return false;
879: }
880: $t[2] = $action;
881: } else {
882: $t = explode('/', (string)$rewrite->actions->$action->to);
883: if (sizeof($t)!==3 || empty($t[0]) || empty($t[1]) || empty($t[2])) {
884: return false;
885: }
886: }
887:
888: $this->_forward(
889: $t[2]==='*' ? $action : $t[2],
890: $t[1]==='*' ? $controller : $t[1],
891: $t[0]==='*' ? $route : $t[0]
892: );
893:
894: return true;
895: }
896:
897: 898: 899: 900: 901:
902: protected function _validateFormKey()
903: {
904: if (!($formKey = $this->getRequest()->getParam('form_key', null))
905: || $formKey != Mage::getSingleton('core/session')->getFormKey()) {
906: return false;
907: }
908: return true;
909: }
910:
911: 912: 913: 914: 915: 916: 917: 918: 919: 920: 921: 922: 923: 924: 925: 926: 927: 928:
929: protected function _title($text = null, $resetIfExists = true)
930: {
931: if (is_string($text)) {
932: $this->_titles[] = $text;
933: } elseif (-1 === $text) {
934: if (empty($this->_titles)) {
935: $this->_removeDefaultTitle = true;
936: } else {
937: array_pop($this->_titles);
938: }
939: } elseif (empty($this->_titles) || $resetIfExists) {
940: if (false === $text) {
941: $this->_removeDefaultTitle = false;
942: $this->_titles = array();
943: } elseif (null === $text) {
944: $this->_removeDefaultTitle = true;
945: $this->_titles = array();
946: }
947: }
948: return $this;
949: }
950:
951: 952: 953: 954: 955: 956: 957: 958:
959: protected function _renderTitles()
960: {
961: if ($this->_isLayoutLoaded && $this->_titles) {
962: $titleBlock = $this->getLayout()->getBlock('head');
963: if ($titleBlock) {
964: if (!$this->_removeDefaultTitle) {
965: $title = trim($titleBlock->getTitle());
966: if ($title) {
967: array_unshift($this->_titles, $title);
968: }
969: }
970: $titleBlock->setTitle(implode(' / ', array_reverse($this->_titles)));
971: }
972: }
973: }
974:
975: 976: 977: 978: 979: 980: 981:
982: protected function _filterDates($array, $dateFields)
983: {
984: if (empty($dateFields)) {
985: return $array;
986: }
987: $filterInput = new Zend_Filter_LocalizedToNormalized(array(
988: 'date_format' => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)
989: ));
990: $filterInternal = new Zend_Filter_NormalizedToLocalized(array(
991: 'date_format' => Varien_Date::DATE_INTERNAL_FORMAT
992: ));
993:
994: foreach ($dateFields as $dateField) {
995: if (array_key_exists($dateField, $array) && !empty($dateField)) {
996: $array[$dateField] = $filterInput->filter($array[$dateField]);
997: $array[$dateField] = $filterInternal->filter($array[$dateField]);
998: }
999: }
1000: return $array;
1001: }
1002:
1003: 1004: 1005: 1006: 1007: 1008: 1009:
1010: protected function _filterDateTime($array, $dateFields)
1011: {
1012: if (empty($dateFields)) {
1013: return $array;
1014: }
1015: $filterInput = new Zend_Filter_LocalizedToNormalized(array(
1016: 'date_format' => Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)
1017: ));
1018: $filterInternal = new Zend_Filter_NormalizedToLocalized(array(
1019: 'date_format' => Varien_Date::DATETIME_INTERNAL_FORMAT
1020: ));
1021:
1022: foreach ($dateFields as $dateField) {
1023: if (array_key_exists($dateField, $array) && !empty($dateField)) {
1024: $array[$dateField] = $filterInput->filter($array[$dateField]);
1025: $array[$dateField] = $filterInternal->filter($array[$dateField]);
1026: }
1027: }
1028: return $array;
1029: }
1030:
1031: 1032: 1033: 1034: 1035: 1036: 1037: 1038: 1039: 1040:
1041: protected function _prepareDownloadResponse(
1042: $fileName,
1043: $content,
1044: $contentType = 'application/octet-stream',
1045: $contentLength = null)
1046: {
1047: $session = Mage::getSingleton('admin/session');
1048: if ($session->isFirstPageAfterLogin()) {
1049: $this->_redirect($session->getUser()->getStartupPageUrl());
1050: return $this;
1051: }
1052:
1053: $isFile = false;
1054: $file = null;
1055: if (is_array($content)) {
1056: if (!isset($content['type']) || !isset($content['value'])) {
1057: return $this;
1058: }
1059: if ($content['type'] == 'filename') {
1060: $isFile = true;
1061: $file = $content['value'];
1062: $contentLength = filesize($file);
1063: }
1064: }
1065:
1066: $this->getResponse()
1067: ->setHttpResponseCode(200)
1068: ->setHeader('Pragma', 'public', true)
1069: ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
1070: ->setHeader('Content-type', $contentType, true)
1071: ->setHeader('Content-Length', is_null($contentLength) ? strlen($content) : $contentLength, true)
1072: ->setHeader('Content-Disposition', 'attachment; filename="'.$fileName.'"', true)
1073: ->setHeader('Last-Modified', date('r'), true);
1074:
1075: if (!is_null($content)) {
1076: if ($isFile) {
1077: $this->getResponse()->clearBody();
1078: $this->getResponse()->sendHeaders();
1079:
1080: $ioAdapter = new Varien_Io_File();
1081: $ioAdapter->open(array('path' => $ioAdapter->dirname($file)));
1082: $ioAdapter->streamOpen($file, 'r');
1083: while ($buffer = $ioAdapter->streamRead()) {
1084: print $buffer;
1085: }
1086: $ioAdapter->streamClose();
1087: if (!empty($content['rm'])) {
1088: $ioAdapter->rm($file);
1089: }
1090:
1091: exit(0);
1092: } else {
1093: $this->getResponse()->setBody($content);
1094: }
1095: }
1096: return $this;
1097: }
1098: }
1099: