Skip to content
Snippets Groups Projects
Commit e6d799c9 authored by Tim Steiner's avatar Tim Steiner
Browse files

New approval actions Email and EmailCourseInfo. The second allows dynmic...

New approval actions Email and EmailCourseInfo.  The second allows dynmic emails to be generated with course information in them.
parent dcdd1feb
No related branches found
No related tags found
No related merge requests found
<?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;
}
}
<?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 />
<?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;
}
}
<?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 />
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment