| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /****************************************************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Wikipedia Account Creation Assistance tool                                 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  *                                                                            * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * All code in this file is released into the public domain by the ACC        * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Development Team. Please see team.json for a list of contributors.         * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  ******************************************************************************/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Waca\Pages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Waca\DataObjects\EmailTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Waca\DataObjects\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Waca\Exceptions\ApplicationLogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Waca\Helpers\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Waca\PdoDatabase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Waca\SessionAlert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Waca\Tasks\InternalPageBase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Waca\WebRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class PageEmailManagement extends InternalPageBase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * Main function for this page, when no specific actions are called. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     protected function main() | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $this->setHtmlTitle('Close Emails'); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         // Get all active email templates | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $activeTemplates = EmailTemplate::getAllActiveTemplates(null, $this->getDatabase()); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $inactiveTemplates = EmailTemplate::getAllInactiveTemplates($this->getDatabase()); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $this->assign('activeTemplates', $activeTemplates); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $this->assign('inactiveTemplates', $inactiveTemplates); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $user = User::getCurrent($this->getDatabase()); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $this->assign('canCreate', $this->barrierTest('create', $user)); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $this->assign('canEdit', $this->barrierTest('edit', $user)); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $this->setTemplate('email-management/main.tpl'); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     protected function view() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->setHtmlTitle('Close Emails'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $database = $this->getDatabase(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $template = $this->getTemplate($database); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $createdId = $this->getSiteConfiguration()->getDefaultCreatedTemplateId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $requestStates = $this->getSiteConfiguration()->getRequestStates(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->assign('id', $template->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->assign('emailTemplate', $template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->assign('createdid', $createdId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->assign('requeststates', $requestStates); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->setTemplate('email-management/view.tpl'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param PdoDatabase $database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return EmailTemplate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @throws ApplicationLogicException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     protected function getTemplate(PdoDatabase $database) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $templateId = WebRequest::getInt('id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if ($templateId === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             throw new ApplicationLogicException('Template not specified'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $template = EmailTemplate::getById($templateId, $database); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if ($template === false || !is_a($template, EmailTemplate::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             throw new ApplicationLogicException('Template not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     protected function edit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->setHtmlTitle('Close Emails'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $database = $this->getDatabase(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $template = $this->getTemplate($database); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $createdId = $this->getSiteConfiguration()->getDefaultCreatedTemplateId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $requestStates = $this->getSiteConfiguration()->getRequestStates(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         if (WebRequest::wasPosted()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $this->validateCSRFToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $this->modifyTemplateData($template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $other = EmailTemplate::getByName($template->getName(), $database); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             if ($other !== false && $other->getId() !== $template->getId()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 throw new ApplicationLogicException('A template with this name already exists'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             if ($template->getId() === $createdId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $template->setDefaultAction(EmailTemplate::CREATED); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 $template->setActive(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 $template->setPreloadOnly(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             // optimistically lock on load of edit form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $updateVersion = WebRequest::postInt('updateversion'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $template->setUpdateVersion($updateVersion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $template->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             Logger::editedEmail($database, $template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $this->getNotificationHelper()->emailEdited($template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             SessionAlert::success("Email template has been saved successfully."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             $this->redirect('emailManagement'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $this->assignCSRFToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $this->assign('id', $template->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $this->assign('emailTemplate', $template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             $this->assign('createdid', $createdId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $this->assign('requeststates', $requestStates); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $this->setTemplate('email-management/edit.tpl'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param EmailTemplate $template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @throws ApplicationLogicException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     private function modifyTemplateData(EmailTemplate $template) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $name = WebRequest::postString('name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         if ($name === null || $name === '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             throw new ApplicationLogicException('Name not specified'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $template->setName($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $text = WebRequest::postString('text'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         if ($text === null || $text === '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             throw new ApplicationLogicException('Text not specified'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $template->setText($text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $template->setJsquestion(WebRequest::postString('jsquestion')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $template->setDefaultAction(WebRequest::postString('defaultaction')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $template->setActive(WebRequest::postBoolean('active')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $template->setPreloadOnly(WebRequest::postBoolean('preloadonly')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     protected function create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $this->setHtmlTitle('Close Emails'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $database = $this->getDatabase(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         $requestStates = $this->getSiteConfiguration()->getRequestStates(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         if (WebRequest::wasPosted()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             $this->validateCSRFToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             $template = new EmailTemplate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             $template->setDatabase($database); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             $this->modifyTemplateData($template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             $other = EmailTemplate::getByName($template->getName(), $database); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             if ($other !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                 throw new ApplicationLogicException('A template with this name already exists'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             $template->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             Logger::createEmail($database, $template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             $this->getNotificationHelper()->emailCreated($template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             SessionAlert::success("Email template has been saved successfully."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             $this->redirect('emailManagement'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             $this->assignCSRFToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             $this->assign('requeststates', $requestStates); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |             $this->setTemplate('email-management/create.tpl'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 193 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 194 |  |  |  |