1<?php 2 3 4namespace GO\Email\Controller; 5 6 7class PortletController extends \GO\Base\Controller\AbstractModelController { 8 9 /** 10 * The full name of the used model in this controller 11 * 12 * @var StringHelper 13 */ 14 protected $model = 'GO\email\Model\PortletFolder'; 15 16 /** 17 * The state of the portlet folders tree (This is the same state as the tree in the email tab) 18 * 19 * @var StringHelper 20 */ 21 private $_treeState; 22 23 /** 24 * Load the folders that need to be displayed in the portlet 25 * 26 * @param array $params 27 * @return array $response 28 */ 29 protected function actionPortletFoldersByUser($params){ 30 31 $findCriteria = \GO\Base\Db\FindCriteria::newInstance() 32 ->addCondition('user_id', \GO::user()->id); 33 34 $findParams = \GO\Base\Db\FindParams::newInstance() 35 ->joinRelation('account') 36 ->criteria($findCriteria); 37 38 $portletFoldersStatement = \GO\email\Model\PortletFolder::model()->find($findParams); 39 40 $portletFoldersStore = \GO\Base\Data\Store::newInstance(\GO::getModel($this->model)); 41 42 $cm = $portletFoldersStore->getColumnModel(); 43 $cm->formatColumn('email','$model->account->getDefaultAlias()->email'); 44 45 $portletFoldersStore->setStatement($portletFoldersStatement); 46 47 return $portletFoldersStore->getData(); 48 } 49 50 /** 51 * Enable a folder to be displayed in the portlet 52 * 53 * @param array $params 54 * @return array $response 55 */ 56 protected function actionEnablePortletFolder($params){ 57 $response = array(); 58 59 if(!isset($params['account_id']) || !isset($params['account_id'])){ 60 $response['success'] = false; 61 } else { 62 $accountId = $params['account_id']; 63 $mailboxName = $params['mailbox']; 64 65 $portletFolder = $this->_loadPortletFolder($accountId,$mailboxName); 66 67 if(!$portletFolder){ 68 $portletFolder = new \GO\email\Model\PortletFolder(); 69 $portletFolder->user_id = \GO::user()->id; 70 $portletFolder->account_id = $accountId; 71 $portletFolder->folder_name = $mailboxName; 72 $portletFolder->save(); 73 } 74 75 $response['success'] = true; 76 } 77 78 return $response; 79 } 80 81 82 private function _loadPortletFolder($accountId,$mailboxName){ 83 $portletFolder = \GO\email\Model\PortletFolder::model()->findByPk(array('account_id'=>$accountId,'folder_name'=>$mailboxName,'user_id'=>\GO::user()->id)); 84 85 if(!$portletFolder) 86 return false; 87 else 88 return $portletFolder; 89 } 90 91 /** 92 * Disable a folder to be disabled from the portlet 93 * 94 * @param array $params 95 * @return array $response 96 */ 97 protected function actionDisablePortletFolder($params){ 98 $response = array(); 99 100 if(!isset($params['account_id']) || !isset($params['account_id'])){ 101 $response['success'] = false; 102 } else { 103 $accountId = $params['account_id']; 104 $mailboxName = $params['mailbox']; 105 106 $portletFolder = $this->_loadPortletFolder($accountId,$mailboxName); 107 108 if($portletFolder) 109 $portletFolder->delete(); 110 111 $response['success'] = true; 112 } 113 114 $response['success'] = true; 115 116 return $response; 117 } 118 119 /** 120 * Build the tree for the portlet folders 121 * 122 * @param array $params 123 * @return array $response 124 */ 125 public function actionPortletTree($params) { 126 \GO::session()->closeWriting(); 127 128 $response = array(); 129 130 if ($params['node'] == 'root') { 131 132 $findParams = \GO\Base\Db\FindParams::newInstance() 133 ->select('t.*') 134 ->joinModel(array( 135 'model' => 'GO\Email\Model\AccountSort', 136 'foreignField' => 'account_id', //defaults to primary key of the remote model 137 'localField' => 'id', //defaults to primary key of the model 138 'type' => 'LEFT', 139 'tableAlias'=>'s', 140 'criteria'=> \GO\Base\Db\FindCriteria::newInstance()->addCondition('user_id', \GO::user()->id,'=','s') 141 )) 142 ->ignoreAdminGroup() 143 ->order('order', 'DESC'); 144 145 $stmt = \GO\Email\Model\Account::model()->find($findParams); 146 147 148 // Loop throught the found accounts and build the accounts root node. 149 while ($account = $stmt->fetch()) { 150 151 $alias = $account->getDefaultAlias(); 152 if($alias){ 153 $nodeId='account_' . $account->id; 154 155 $node = array( 156 'text' => $alias->email, 157 'name' => $alias->email, 158 'id' => $nodeId, 159 'isAccount'=>true, 160 'hasError'=>false, 161 'iconCls' => 'folder-account', 162 'expanded' => $this->_isExpanded($nodeId), 163 'noselect' => false, 164 'account_id' => $account->id, 165 'mailbox' => '', 166 'noinferiors' => false 167 ); 168 169 // Try to find the children 170 try{ 171 $account->openImapConnection(); 172 if($node['expanded']) 173 $node['children']=$this->_getMailboxTreeNodes($account->getRootMailboxes(true)); 174 175 }catch(\Exception $e){ 176// $this->_checkImapConnectException($e,$node); 177 if (strpos($e->getMessage(),'Authentication failed')==0) { 178 $node['isAccount'] = false; 179 $node['hasError'] = true; 180 $node['text'] .= ' ('.\GO::t("Error").')'; 181 $node['children']=array(); 182 $node['expanded']=true; 183 $node['qtipCfg'] = array('title'=>\GO::t("Error"), 'text' =>htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8')); 184 } else { 185 throw $e; 186 } 187 } 188 189 $response[] = $node; 190 } 191 } 192 } else { 193 $parts = explode('_', $params['node']); 194 $type = array_shift($parts); 195 $accountId = array_shift($parts); 196 $mailboxName = implode('_', $parts); 197 198 $account = \GO\Email\Model\Account::model()->findByPk($accountId); 199 200 if($type=="account"){ 201 $response=$this->_getMailboxTreeNodes($account->getRootMailboxes(true)); 202 }else{ 203 $mailbox = new \GO\Email\Model\ImapMailbox($account, array('name' => $mailboxName)); 204 $response = $this->_getMailboxTreeNodes($mailbox->getChildren()); 205 } 206 } 207 208 return $response; 209 } 210 211 /** 212 * Get the tree result from the given mailboxes 213 * 214 * @param array $mailboxes 215 * @return StringHelper 216 */ 217 private function _getMailboxTreeNodes($mailboxes) { 218 $nodes = array(); 219 foreach ($mailboxes as $mailbox) { 220 221 $nodeId = 'f_' . $mailbox->getAccount()->id . '_' . $mailbox->name; 222 223 $text = $mailbox->getDisplayName(); 224 225 $node = array( 226 'text' => $text, 227 'mailbox' => $mailbox->name, 228 'account_id' => $mailbox->getAccount()->id, 229 'iconCls' => 'folder-default', 230 'id' => $nodeId, 231 'noselect' => $mailbox->noselect, 232 'disabled' =>$mailbox->noselect, 233 'noinferiors' => $mailbox->noinferiors, 234 'children' => !$mailbox->haschildren ? array() : null, 235 'expanded' => !$mailbox->haschildren, 236 'cls'=>$mailbox->noselect==1 ? 'em-tree-node-noselect' : null 237 ); 238 239 if ($mailbox->haschildren && $this->_isExpanded($nodeId)) { 240 $node['children'] = $this->_getMailboxTreeNodes($mailbox->getChildren(false, false)); 241 $node['expanded'] = true; 242 } 243 244 $node['checked']= $this->_showInPortlet($mailbox->getAccount()->id,$mailbox->name); 245 246 $sortIndex = 5; 247 248 switch ($mailbox->name) { 249 case 'INBOX': 250 $node['iconCls'] = 'email-folder-inbox'; 251 $sortIndex = 0; 252 break; 253 case $mailbox->getAccount()->sent: 254 $node['iconCls'] = 'email-folder-sent'; 255 $sortIndex = 1; 256 break; 257 case $mailbox->getAccount()->trash: 258 $node['iconCls'] = 'email-folder-trash'; 259 $sortIndex = 3; 260 break; 261 case $mailbox->getAccount()->drafts: 262 $node['iconCls'] = 'email-folder-drafts'; 263 $sortIndex = 2; 264 break; 265 case 'Spam': 266 $node['iconCls'] = 'email-folder-spam'; 267 $sortIndex = 4; 268 break; 269 } 270 271 $nodes[$sortIndex . $mailbox->name] = $node; 272 } 273 ksort($nodes); 274 275 return array_values($nodes); 276 } 277 278 /** 279 * Check if the node is opened in the email module or not 280 * 281 * @param int $nodeId 282 * @return boolean 283 */ 284 private function _isExpanded($nodeId) { 285 if (!isset($this->_treeState)) { 286 $state = \GO::config()->get_setting("email_accounts_tree", \GO::user()->id); 287 288 if(empty($state)){ 289 //account and inbox nodes are expanded by default 290 if((stristr($nodeId, 'account') || substr($nodeId,-6)=='_INBOX')){ 291 return true; 292 }else 293 { 294 return false; 295 } 296 } 297 298 $this->_treeState = json_decode($state); 299 } 300 301 return in_array($nodeId, $this->_treeState); 302 } 303 304 /** 305 * Check if the mailbox is enabled to show in the email portlet 306 * 307 * @param StringHelper $mailboxName 308 * @return boolean 309 */ 310 private function _showInPortlet($accountId,$mailboxName){ 311 312 if(!$this->_loadPortletFolder($accountId, $mailboxName)) 313 return false; 314 else 315 return true; 316 } 317 318} 319