From e6d799c94c09c87ac70ba46dad7ee33bdae1589d Mon Sep 17 00:00:00 2001 From: Tim Steiner <tsteiner2@unl.edu> Date: Wed, 3 Mar 2010 16:47:13 +0000 Subject: [PATCH] New approval actions Email and EmailCourseInfo. The second allows dynmic emails to be generated with course information in them. --- .../ApprovalActionEmailCourseInfoModel.php | 200 ++++++++++++++++++ .../edit-action/emailCourseInfo.phtml | 15 ++ .../models/ApprovalActionEmailModel.php | 195 +++++++++++++++++ .../edit-action/email.phtml | 15 ++ 4 files changed, 425 insertions(+) create mode 100644 application/modules/courses/models/ApprovalActionEmailCourseInfoModel.php create mode 100644 application/modules/courses/views/scripts/approval-chain-manager/edit-action/emailCourseInfo.phtml create mode 100644 application/modules/requests/models/ApprovalActionEmailModel.php create mode 100644 application/modules/requests/views/scripts/approval-chain-manager/edit-action/email.phtml diff --git a/application/modules/courses/models/ApprovalActionEmailCourseInfoModel.php b/application/modules/courses/models/ApprovalActionEmailCourseInfoModel.php new file mode 100644 index 00000000..086c795b --- /dev/null +++ b/application/modules/courses/models/ApprovalActionEmailCourseInfoModel.php @@ -0,0 +1,200 @@ +<?php + +class Courses_ApprovalActionEmailCourseInfoModel extends Requests_ApprovalActionEmailModel +{ + public static function find($id) + { + $db = Zend_Registry::get('db'); + + $select = new Zend_Db_Select($db); + $select->from(array('a' => 'creqApprovalActions')); + $select->join(array('e' => 'creqApprovalActionsEmail'), 'a.approvalActionId = e.approvalActionId'); + $select->join(array('i' => 'creqApprovalActionsEmailCourseInfo'), 'e.approvalActionId = i.approvalActionId'); + if (Unl_Util::isArray($id)) { + $select->where('a.approvalActionId IN(?)', $id); + } else { + $select->where('a.approvalActionId = ?', $id); + } + + $records = $db->query($select)->fetchAll(); + $objects = new Unl_Model_Collection(__CLASS__); + foreach ($records as $record) { + $object = Unl_Model_Registry::getInstance()->getOrAdd(new self($record)); + $objectId = $object->getId(); + $objects[$objectId] = $object; + } + + + if (Unl_Util::isArray($objects)) { + return $objects; + } else { + return array_pop($objects); + } + + } + + static public function fetchNew() + { + $newParent = parent::fetchNew(); + $data = $newParent->_data; + + $new = new self($data); + $new->_setClean(); + + $new->_data['className'] = 'ApprovalActionEmailCourseInfo'; + + return $new; + } + + static public function save($models) + { + $modelsToInsert = new Unl_Model_Collection(__CLASS__); + $modelsToUpdate = new Unl_Model_Collection(__CLASS__); + + if (!Unl_Util::isArray($models)) { + $model = $models; + $models = new Unl_Model_Collection(__CLASS__); + $models[$model->getId()] = $model; + } + + foreach ($models as $model) { + if ($model->_cleanData['approvalActionId'] && $model->getId()) { + if ($model->_cleanData != $model->_data) { + $modelsToUpdate[] = $model; + } + } else { + $modelsToInsert[] = $model; + } + } + + if (count($modelsToInsert) > 0) { + self::_insert($modelsToInsert); + } + + if (count($modelsToUpdate) > 0) { + self::_update($modelsToUpdate); + } + + parent::save($models); + } + + static protected function _update(Unl_Model_Collection $models) + { + //until this table has more fields, there's no reason to ever have to update it. + return; + + + $db = Zend_Registry::get('db'); + + $sql = 'CREATE TEMPORARY TABLE creqApprovalActionsEmailCourseInfoUpdate ' + . 'SELECT * FROM creqApprovalActionsEmailCourseInfo LIMIT 0'; + $db->query($sql); + + $sql = 'INSERT INTO creqApprovalActionsEmailCourseInfoUpdate VALUES '; + $sqlParts = array(); + foreach ($models as $model) { + $sqlParts[] = $db->quoteInto('(?, ', $model->_data['approvalActionId']) + . $db->quoteInto('?,' , $model->_data['alpha']) + . $db->quoteInto('?)' , $model->_data['bravo']); + } + $sql .= implode(', ', $sqlParts); + $db->query($sql); + + $sql = 'UPDATE creqApprovalActionsEmailCourseInfo AS a, ' + . ' creqApprovalActionsEmailCourseInfoUpdate AS b ' + . 'SET a.alpha = b.alpha, ' + . ' a.bravo = b.bravo ' + . 'WHERE a.approvalActionId = b.approvalActionId '; + $db->query($sql); + + $db->query('DROP TABLE creqApprovalActionsEmailCourseInfoUpdate'); + } + + static protected function _insert(Unl_Model_Collection $models) + { + $db = Zend_Registry::get('db'); + + $sql = 'INSERT INTO creqApprovalActionsEmailCourseInfo (approvalActionId) VALUES '; + $sqlParts = array(); + foreach ($models as $model) { + $sqlParts[] = $db->quoteInto('(?)', $model->_data['approvalActionId']); + } + $sql .= implode(', ', $sqlParts); + $db->query($sql); + } + + protected $_requestCourses; + protected $_requestParentCourses; + protected $_requestPreviousRequests; + protected $_owners; + + /* + * This doesn't actually do anything other than pre-load data that will be used in the _substitute call. + */ + public function consider($requests) + { + $this->_requestCourses = Courses_CourseModel::findLatestOfRequest($requests); + $this->_requestParentCourses = Courses_CourseModel::findParentOfRequest($requests); + $previousRequestIds = array(); + foreach ($this->_requestParentCourses as $requestId => $course) { + $previousRequestIds[$requestId] = $course->getRequest(); + } + $previousRequests = Requests_RequestModel::find($previousRequestIds); + $this->_requestPreviousRequests = new Unl_Model_Collection('Requests_RequestModel'); + foreach ($previousRequestIds as $childId => $parentId) { + $this->_requestPreviousRequests[$childId] = $previousRequests[$parentId]; + } + + $ownerIds = array(); + foreach ($requests as $request) { + $ownerIds[] = $request->getOwner(); + } + foreach ($previousRequests as $request) { + $ownerIds[] = $request->getOwner(); + } + $this->_owners = Auth_UserModel::find($ownerIds); + + return parent::consider($requests); + } + + protected function _substitute($text, $request) + { + $course = $this->_requestCourses[$request->getId()]; + $owner = $this->_owners[$request->getOwner()]; + $parentCourse = $this->_requestParentCourses[$request->getId()]; + $parentRequest = $this->_requestPreviousRequests[$request->getId()]; + if ($parentRequest) { + $parentOwner = $this->_owners[$parentRequest->getOwner()]; + } + + $text = parent::_substitute($text, $request); + $text = strtr($text, array( + '%cc%' => $course->getCourseCode(), + '%ri%' => $request->getId(), + '%rj%' => $request->getJustification(), + '%rse%' => $owner->getEmail(), + )); + + if ($parentCourse) { + $text = strtr($text, array( + '%cc-%' => $parentCourse->getCourseCode(), + )); + } + + if ($parentRequest) { + $text = strtr($text, array( + '%ri-%' => $parentRequest->getId(), + '%rj-%' => $parentRequest->getJustification() + )); + } + + if ($parentOwner) { + $text = strtr($text, array( + '%rse-%' => $parentOwner->getEmail() + )); + } + + return $text; + } + +} diff --git a/application/modules/courses/views/scripts/approval-chain-manager/edit-action/emailCourseInfo.phtml b/application/modules/courses/views/scripts/approval-chain-manager/edit-action/emailCourseInfo.phtml new file mode 100644 index 00000000..bda812fb --- /dev/null +++ b/application/modules/courses/views/scripts/approval-chain-manager/edit-action/emailCourseInfo.phtml @@ -0,0 +1,15 @@ +<?php +if ($this->approvalAction->getId()) { + $prefix = 'edit[' . $this->approvalAction->getId() . ']['; + $postifx = ']'; +} +?> + +Recipient:<br /> +<?php echo $this->formText($prefix . 'recipient' . $postifx, $this->approvalAction->getRecipient()); ?><br /> + +Subject:<br /> +<?php echo $this->formText($prefix . 'subject' . $postifx, $this->approvalAction->getSubject()); ?><br /> + +Body:<br /> +<?php echo $this->formTextarea($prefix . 'body' . $postifx, $this->approvalAction->getBody()); ?><br /> diff --git a/application/modules/requests/models/ApprovalActionEmailModel.php b/application/modules/requests/models/ApprovalActionEmailModel.php new file mode 100644 index 00000000..cb638018 --- /dev/null +++ b/application/modules/requests/models/ApprovalActionEmailModel.php @@ -0,0 +1,195 @@ +<?php + +class Requests_ApprovalActionEmailModel extends Requests_ApprovalActionModel +{ + static public function find($id) + { + $db = Zend_Registry::get('db'); + + $select = new Zend_Db_Select($db); + $select->from(array('a' => 'creqApprovalActions')); + $select->join(array('d' => 'creqApprovalActionsEmail'), 'a.approvalActionId = d.approvalActionId'); + if (Unl_Util::isArray($id)) { + $select->where('a.approvalActionId IN(?)', $id); + } else { + $select->where('a.approvalActionId = ?', $id); + } + + $records = $db->query($select)->fetchAll(); + $objects = new Unl_Model_Collection(__CLASS__); + foreach ($records as $record) { + $object = Unl_Model_Registry::getInstance()->getOrAdd(new self($record)); + $objectId = $object->getId(); + $objects[$objectId] = $object; + } + + if (Unl_Util::isArray($objects)) { + return $objects; + } else { + return array_pop($objects); + } + } + + static public function fetchNew() + { + $data = array( + 'approvalActionId' => NULL, + 'name' => '', + 'approvalChain' => NULL, + 'className' => '', + 'participatingRoles' => array(), + 'editingRoles' => array(), + 'recipient' => '', + 'subject' => '', + 'body' => '' + ); + + $new = new self($data); + $new->_setClean(); + + $new->_data['className'] = 'ApprovalActionEmail'; + + return $new; + } + + static public function save($models) + { + $modelsToInsert = new Unl_Model_Collection(__CLASS__); + $modelsToUpdate = new Unl_Model_Collection(__CLASS__); + + if (!Unl_Util::isArray($models)) { + $model = $models; + $models = new Unl_Model_Collection(__CLASS__); + $models[$model->getId()] = $model; + } + + foreach ($models as $model) { + if ($model->_cleanData['approvalActionId'] && $model->getId()) { + if ($model->_cleanData != $model->_data) { + $modelsToUpdate[] = $model; + } + } else { + $modelsToInsert[] = $model; + } + } + + if (count($modelsToInsert) > 0) { + self::_insert($modelsToInsert); + } + + if (count($modelsToUpdate) > 0) { + self::_update($modelsToUpdate); + } + } + + static protected function _update(Unl_Model_Collection $models) + { + $db = Zend_Registry::get('db'); + + $sql = 'CREATE TEMPORARY TABLE creqApprovalActionsEmailUpdate ' + . 'SELECT * FROM creqApprovalActionsEmail LIMIT 0'; + $db->query($sql); + + $sql = 'INSERT INTO creqApprovalActionsEmailUpdate VALUES '; + $sqlParts = array(); + foreach ($models as $model) { + $sqlParts[] = $db->quoteInto('(?, ', $model->_data['approvalActionId']) + . $db->quoteInto('?, ', $model->_data['recipient']) + . $db->quoteInto('?, ', $model->_data['subject']) + . $db->quoteInto('?) ', $model->_data['body']); + } + $sql .= implode(', ', $sqlParts); + $db->query($sql); + + $sql = 'UPDATE creqApprovalActionsEmail AS a, ' + . ' creqApprovalActionsEmailUpdate AS b ' + . 'SET a.recipient = b.recipient, ' + . ' a.subject = b.subject, ' + . ' a.body = b.body ' + . 'WHERE a.approvalActionId = b.approvalActionId '; + $db->query($sql); + } + + static protected function _insert(Unl_Model_Collection $models) + { + $db = Zend_Registry::get('db'); + + $sql = 'INSERT INTO creqApprovalActionsEmail (approvalActionId, recipient, subject, body) VALUES '; + $sqlParts = array(); + foreach ($models as $model) { + $sqlParts[] = $db->quoteInto('(?, ', $model->_data['approvalActionId']) + . $db->quoteInto('?, ', $model->_data['recipient']) + . $db->quoteInto('?, ', $model->_data['subject']) + . $db->quoteInto('?)' , $model->_data['body']); + } + $sql .= implode(', ', $sqlParts); + $db->query($sql); + } + + public function consider($requests) + { + foreach ($requests as $request) { + $mail = new Zend_Mail(); + $recipients = $this->_substitute($this->getRecipient(), $request); + $recipients = explode(',', $recipients); + foreach ($recipients as $recipient) { + $recipient = trim($recipient); + $mail->addTo($recipient); + } + $mail->setFrom('automated@courseapproval.unl.edu', 'Course Approval'); + $mail->setSubject($this->_substitute($this->getSubject(), $request)); + $mail->setBodyText($this->_substitute($this->getBody(), $request)); + $mail->send(); + } + + Requests_ApprovalChainModel::advance($requests); + return $requests; + } + + public function userMadeDecisions($requests, $requestDecisions) + { + // Don't do anything here, this is an automated action. + } + + public function setExtendedData($in) + { + $this->setRecipient($in['recipient']); + $this->setSubject($in['subject']); + $this->setBody($in['body']); + } + + public function getRecipient() + { + return $this->_data['recipient']; + } + + public function setRecipient($recipient) + { + $this->_data['recipient'] = $recipient; + } + + public function getSubject() + { + return $this->_data['subject']; + } + + public function setSubject($subject) + { + $this->_data['subject'] = $subject; + } + + public function getBody() + { + return $this->_data['body']; + } + + public function setBody($body) + { + $this->_data['body'] = $body; + } + + protected function _substitute($text, $request) + { + return $text; + } +} diff --git a/application/modules/requests/views/scripts/approval-chain-manager/edit-action/email.phtml b/application/modules/requests/views/scripts/approval-chain-manager/edit-action/email.phtml new file mode 100644 index 00000000..bda812fb --- /dev/null +++ b/application/modules/requests/views/scripts/approval-chain-manager/edit-action/email.phtml @@ -0,0 +1,15 @@ +<?php +if ($this->approvalAction->getId()) { + $prefix = 'edit[' . $this->approvalAction->getId() . ']['; + $postifx = ']'; +} +?> + +Recipient:<br /> +<?php echo $this->formText($prefix . 'recipient' . $postifx, $this->approvalAction->getRecipient()); ?><br /> + +Subject:<br /> +<?php echo $this->formText($prefix . 'subject' . $postifx, $this->approvalAction->getSubject()); ?><br /> + +Body:<br /> +<?php echo $this->formTextarea($prefix . 'body' . $postifx, $this->approvalAction->getBody()); ?><br /> -- GitLab