00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Adminhtml_PollController extends Mage_Adminhtml_Controller_Action
00035 {
00036
00037 public function indexAction()
00038 {
00039 $this->loadLayout();
00040 $this->_setActiveMenu('cms/poll');
00041 $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Poll Manager'), Mage::helper('adminhtml')->__('Poll Manager'));
00042
00043 $this->_addContent($this->getLayout()->createBlock('adminhtml/poll_poll'));
00044 $this->renderLayout();
00045 }
00046
00047 public function editAction()
00048 {
00049 $pollId = $this->getRequest()->getParam('id');
00050 $pollModel = Mage::getModel('poll/poll')->load($pollId);
00051
00052 if ($pollModel->getId() || $pollId == 0) {
00053
00054 Mage::register('poll_data', $pollModel);
00055
00056 $this->loadLayout();
00057 $this->_setActiveMenu('cms/poll');
00058 $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Poll Manager'), Mage::helper('adminhtml')->__('Poll Manager'), $this->getUrl('*/*/'));
00059 $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Edit Poll'), Mage::helper('adminhtml')->__('Edit Poll'));
00060
00061 $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
00062 $this->_addContent($this->getLayout()->createBlock('adminhtml/poll_edit'))
00063 ->_addLeft($this->getLayout()->createBlock('adminhtml/poll_edit_tabs'));
00064
00065 $this->renderLayout();
00066 } else {
00067 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('poll')->__('Poll not exists'));
00068 $this->_redirect('*/*/');
00069 }
00070 }
00071
00072 public function deleteAction()
00073 {
00074 if ($id = $this->getRequest()->getParam('id')) {
00075 try {
00076 $model = Mage::getModel('poll/poll');
00077 $model->setId($id);
00078 $model->delete();
00079 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Poll was successfully deleted'));
00080 $this->_redirect('*/*/');
00081 return;
00082 }
00083 catch (Exception $e) {
00084 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00085 $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
00086 return;
00087 }
00088 }
00089 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Unable to find a poll to delete'));
00090 $this->_redirect('*/*/');
00091 }
00092
00093 public function saveAction()
00094 {
00095 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Poll was successfully saved'));
00096 Mage::getSingleton('adminhtml/session')->setPollData(false);
00097 $this->_redirect('*/*/');
00098 }
00099
00100 public function newAction()
00101 {
00102 $this->getRequest()->setParam('id', 0);
00103 $this->_forward('edit');
00104 }
00105
00106 public function validateAction()
00107 {
00108 $response = new Varien_Object();
00109 $response->setError(false);
00110
00111 if ( $this->getRequest()->getPost() ) {
00112 try {
00113 $pollModel = Mage::getModel('poll/poll');
00114
00115 if( !$this->getRequest()->getParam('id') ) {
00116 $pollModel->setDatePosted(now());
00117 }
00118
00119 if( $this->getRequest()->getParam('closed') && !$this->getRequest()->getParam('was_closed') ) {
00120 $pollModel->setDateClosed(now());
00121 }
00122
00123 if( !$this->getRequest()->getParam('closed') ) {
00124 $pollModel->setDateClosed(new Zend_Db_Expr('null'));
00125 }
00126
00127 $pollModel->setPollTitle($this->getRequest()->getParam('poll_title'))
00128 ->setClosed($this->getRequest()->getParam('closed'));
00129
00130 if( $this->getRequest()->getParam('id') > 0 ) {
00131 $pollModel->setId($this->getRequest()->getParam('id'));
00132 }
00133
00134 $stores = $this->getRequest()->getParam('store_ids');
00135 if (!is_array($stores) || count($stores) == 0) {
00136 Mage::throwException(Mage::helper('adminhtml')->__('Please, select visible in stores to this poll first'));
00137 }
00138
00139 if (is_array($stores)) {
00140 $storeIds = array();
00141 foreach ($stores as $storeId) {
00142 $storeIds[] = $storeId;
00143 }
00144 $pollModel->setStoreIds($storeIds);
00145 }
00146
00147 $answers = $this->getRequest()->getParam('answer');
00148
00149 if( !is_array($answers) || sizeof($answers) == 0 ) {
00150 Mage::throwException(Mage::helper('adminhtml')->__('Please, add a few answers to this poll first'));
00151 }
00152
00153 if( is_array($answers) ) {
00154 $_titles = array();
00155 foreach( $answers as $key => $answer ) {
00156 if( in_array($answer['title'], $_titles) ) {
00157 Mage::throwException(Mage::helper('adminhtml')->__('Your answers contain duplicates.'));
00158 }
00159 $_titles[] = $answer['title'];
00160
00161 $answerModel = Mage::getModel('poll/poll_answer');
00162 if( intval($key) > 0 ) {
00163 $answerModel->setId($key);
00164 }
00165 $answerModel->setAnswerTitle($answer['title'])
00166 ->setVotesCount($answer['votes']);
00167
00168 $pollModel->addAnswer($answerModel);
00169 }
00170 }
00171
00172 $pollModel->save();
00173
00174 $answersDelete = $this->getRequest()->getParam('deleteAnswer');
00175 if( is_array($answersDelete) ) {
00176 foreach( $answersDelete as $answer ) {
00177 $answerModel = Mage::getModel('poll/poll_answer');
00178 $answerModel->setId($answer)
00179 ->delete();
00180 }
00181 }
00182 }
00183 catch (Exception $e) {
00184 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00185 $this->_initLayoutMessages('adminhtml/session');
00186 $response->setError(true);
00187 $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
00188 }
00189 }
00190 $this->getResponse()->setBody($response->toJson());
00191 }
00192
00193 protected function _isAllowed()
00194 {
00195 return Mage::getSingleton('admin/session')->isAllowed('cms/poll');
00196 }
00197
00198 }