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: class Mage_Adminhtml_PollController extends Mage_Adminhtml_Controller_Action
35: {
36:
37: public function indexAction()
38: {
39: $this->_title($this->__('CMS'))->_title($this->__('Polls'));
40:
41: $this->loadLayout();
42: $this->_setActiveMenu('cms/poll');
43: $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Poll Manager'), Mage::helper('adminhtml')->__('Poll Manager'));
44:
45: $this->_addContent($this->getLayout()->createBlock('adminhtml/poll_poll'));
46: $this->renderLayout();
47: }
48:
49: public function editAction()
50: {
51: $this->_title($this->__('CMS'))->_title($this->__('Polls'));
52:
53: $pollId = $this->getRequest()->getParam('id');
54: $pollModel = Mage::getModel('poll/poll')->load($pollId);
55:
56: if ($pollModel->getId() || $pollId == 0) {
57: $this->_title($pollModel->getId() ? $pollModel->getPollTitle() : $this->__('New Poll'));
58:
59: Mage::register('poll_data', $pollModel);
60:
61: $this->loadLayout();
62: $this->_setActiveMenu('cms/poll');
63: $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Poll Manager'), Mage::helper('adminhtml')->__('Poll Manager'), $this->getUrl('*/*/'));
64: $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Edit Poll'), Mage::helper('adminhtml')->__('Edit Poll'));
65:
66: $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
67: $this->_addContent($this->getLayout()->createBlock('adminhtml/poll_edit'))
68: ->_addLeft($this->getLayout()->createBlock('adminhtml/poll_edit_tabs'));
69:
70: $this->renderLayout();
71: } else {
72: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('poll')->__('The poll does not exist.'));
73: $this->_redirect('*/*/');
74: }
75: }
76:
77: public function deleteAction()
78: {
79: if ($id = $this->getRequest()->getParam('id')) {
80: try {
81: $model = Mage::getModel('poll/poll');
82: $model->setId($id);
83: $model->delete();
84: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('The poll has been deleted.'));
85: $this->_redirect('*/*/');
86: return;
87: }
88: catch (Exception $e) {
89: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
90: $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
91: return;
92: }
93: }
94: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Unable to find a poll to delete.'));
95: $this->_redirect('*/*/');
96: }
97:
98: public function saveAction()
99: {
100: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('The poll has been saved.'));
101: Mage::getSingleton('adminhtml/session')->setPollData(false);
102: $this->_redirect('*/*/');
103: }
104:
105: public function newAction()
106: {
107: $this->getRequest()->setParam('id', 0);
108: $this->_forward('edit');
109: }
110:
111: public function validateAction()
112: {
113: $response = new Varien_Object();
114: $response->setError(false);
115:
116: if ( $this->getRequest()->getPost() ) {
117: try {
118: $pollModel = Mage::getModel('poll/poll');
119:
120: if( !$this->getRequest()->getParam('id') ) {
121: $pollModel->setDatePosted(now());
122: }
123:
124: if( $this->getRequest()->getParam('closed') && !$this->getRequest()->getParam('was_closed') ) {
125: $pollModel->setDateClosed(now());
126: }
127:
128: if( !$this->getRequest()->getParam('closed') ) {
129: $pollModel->setDateClosed(new Zend_Db_Expr('null'));
130: }
131:
132: $pollModel->setPollTitle($this->getRequest()->getParam('poll_title'))
133: ->setClosed($this->getRequest()->getParam('closed'));
134:
135: if( $this->getRequest()->getParam('id') > 0 ) {
136: $pollModel->setId($this->getRequest()->getParam('id'));
137: }
138:
139: $stores = $this->getRequest()->getParam('store_ids');
140: if (!is_array($stores) || count($stores) == 0) {
141: Mage::throwException(Mage::helper('adminhtml')->__('Please, select "Visible in Stores" for this poll first.'));
142: }
143:
144: if (is_array($stores)) {
145: $storeIds = array();
146: foreach ($stores as $storeIdList) {
147: $storeIdList = explode(',', $storeIdList);
148: if(!$storeIdList) {
149: continue;
150: }
151: foreach($storeIdList as $storeId) {
152: if( $storeId > 0 ) {
153: $storeIds[] = $storeId;
154: }
155: }
156: }
157: if (count($storeIds) === 0) {
158: Mage::throwException(Mage::helper('adminhtml')->__('Please, select "Visible in Stores" for this poll first.'));
159: }
160: $pollModel->setStoreIds($storeIds);
161: }
162:
163: $answers = $this->getRequest()->getParam('answer');
164:
165: if( !is_array($answers) || sizeof($answers) == 0 ) {
166: Mage::throwException(Mage::helper('adminhtml')->__('Please, add some answers to this poll first.'));
167: }
168:
169: if( is_array($answers) ) {
170: $_titles = array();
171: foreach( $answers as $key => $answer ) {
172: if( in_array($answer['title'], $_titles) ) {
173: Mage::throwException(Mage::helper('adminhtml')->__('Your answers contain duplicates.'));
174: }
175: $_titles[] = $answer['title'];
176:
177: $answerModel = Mage::getModel('poll/poll_answer');
178: if( intval($key) > 0 ) {
179: $answerModel->setId($key);
180: }
181: $answerModel->setAnswerTitle($answer['title'])
182: ->setVotesCount($answer['votes']);
183:
184: $pollModel->addAnswer($answerModel);
185: }
186: }
187:
188: $pollModel->save();
189:
190: Mage::register('current_poll_model', $pollModel);
191:
192: $answersDelete = $this->getRequest()->getParam('deleteAnswer');
193: if( is_array($answersDelete) ) {
194: foreach( $answersDelete as $answer ) {
195: $answerModel = Mage::getModel('poll/poll_answer');
196: $answerModel->setId($answer)
197: ->delete();
198: }
199: }
200: }
201: catch (Exception $e) {
202: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
203: $this->_initLayoutMessages('adminhtml/session');
204: $response->setError(true);
205: $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
206: }
207: }
208: $this->getResponse()->setBody($response->toJson());
209: }
210:
211: protected function _isAllowed()
212: {
213: return Mage::getSingleton('admin/session')->isAllowed('cms/poll');
214: }
215:
216: }
217: