diff --git a/application/modules/requests/controllers/RequestTypeAdminController.php b/application/modules/requests/controllers/RequestTypeAdminController.php new file mode 100644 index 0000000000000000000000000000000000000000..af351f17403a934b0f8861482fec1e7418a5d467 --- /dev/null +++ b/application/modules/requests/controllers/RequestTypeAdminController.php @@ -0,0 +1,39 @@ +<?php + +class Requests_RequestTypeAdminController extends App_Controller_Action { + + public function indexAction() + { + $requestTypes = Requests_RequestTypeModel::findAll(); + $this->view->requestTypes = $requestTypes; + } + + public function editAction() + { + $in = $this->_getAllParams(); + $requestType = Requests_RequestTypeModel::find($in['id']); + if (!$requestType) { + $requestType = Requests_RequestTypeModel::fetchNew(); + } + + $this->view->requestType = $requestType; + } + + public function editPostAction() + { + $in = $this->_getAllParams(); + + $requestType = Requests_RequestTypeModel::find($in['id']); + if (!$requestType) { + $requestType = Requests_RequestTypeModel::fetchNew(); + } + + $requestType->setName($in['name']); + $requestType->setDescription($in['description']); + $requestType->setModule($in['module']); + + Requests_RequestTypeModel::save($requestType); + + $this->_redirect('/requests/request-type-admin/edit/id/' . $requestType->getId()); + } +} diff --git a/application/modules/requests/models/RequestTypeModel.php b/application/modules/requests/models/RequestTypeModel.php new file mode 100644 index 0000000000000000000000000000000000000000..e2ee3f3eed2d5d3135f439a706fc33d5077f0790 --- /dev/null +++ b/application/modules/requests/models/RequestTypeModel.php @@ -0,0 +1,204 @@ +<?php + +class Requests_RequestTypeModel extends Unl_Model +{ + static public function find($id) + { + if (Unl_Util::isArray($id) && count($id) == 0) { + return new Unl_Model_Collection(__CLASS__); + } + if ($id === null) { + return null; + } + + $db = Zend_Registry::get('db'); + + $select = new Zend_Db_Select($db); + $select->from(array('t' => 'creqRequestTypes')); + if (Unl_Util::isArray($id)) { + if (count($id) == 0) { + return new Unl_Model_Collection(__CLASS__); + } + $select->where('t.requestTypeId IN(?)', $id); + } else { + $select->where('t.requestTypeId = ?', $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)); + $object->_setClean(); + $objectId = $object->getId(); + $objects[$objectId] = $object; + } + + if (Unl_Util::isArray($id)) { + return $objects; + } else { + return $objects->pop(); + } + } + + static public function findAll() + { + $db = Zend_Registry::get('db'); + + $select = new Zend_Db_Select($db); + $select->from(array('t' => 'creqRequestTypes'), 'requestTypeId'); + $records = $select->query()->fetchAll(); + + $ids = array(); + foreach ($records as $record) { + $ids[] = $record['requestTypeId']; + } + + return self::find($ids); + } + + /** + * Gets a freshly created model object + * + * @return Requests_TypesModel + */ + static function fetchNew() + { + $data = array( + 'requestTypeId' => null, + 'name' => null, + 'description' => null, + 'approvalChain' => 1, + 'module' => null + ); + return new self($data); + } + + 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->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); + } + + foreach ($models as $model) + { + $model->_setClean(); + } + } + + static protected function _update(Unl_Model_Collection $models) + { + $db = Zend_Registry::get('db'); + + $sql = 'CREATE TEMPORARY TABLE creqRequestTypesUpdate ' + . 'SELECT * FROM creqRequestTypes LIMIT 0'; + $db->query($sql); + + $sql = 'INSERT INTO creqRequestTypesUpdate VALUES '; + $sqlParts = array(); + foreach ($models as $model) { + $sqlParts[] = $db->quoteInto('(?, ', $model->_data['requestTypeId']) + . $db->quoteInto('?, ' , $model->_data['name']) + . $db->quoteInto('?, ' , $model->_data['description']) + . $db->quoteInto('?, ' , $model->_data['approvalChain']) + . $db->quoteInto('?)' , $model->_data['module']); + } + $sql .= implode(', ', $sqlParts); + $db->query($sql); + + $sql = 'UPDATE creqRequestTypes AS a, ' + . ' creqRequestTypesUpdate AS b ' + . 'SET a.name = b.name, ' + . ' a.description = b.description, ' + . ' a.approvalChain = b.approvalChain, ' + . ' a.module = b.module ' + . 'WHERE a.requestTypeId = b.requestTypeId '; + $db->query($sql); + } + + static protected function _insert(Unl_Model_Collection $models) + { + $db = Zend_Registry::get('db'); + + $sql = 'INSERT INTO creqRequestTypes (name, description, approvalChain, module) VALUES '; + $sqlParts = array(); + foreach ($models as $model) { + $sqlParts[] = $db->quoteInto('(?, ', $model->_data['name']) + . $db->quoteInto('?, ' , $model->_data['description']) + . $db->quoteInto('?, ' , $model->_data['approvalChain']) + . $db->quoteInto('?)' , $model->_data['module']); + } + $sql .= implode(', ', $sqlParts); + $db->query($sql); + + $lastId = $db->lastInsertId(); + foreach ($models as $model) { + $model->_data['requestTypeId'] = $lastId; + $lastId++; + } + } + + public function getId() + { + return $this->_data['requestTypeId']; + } + + public function getName() + { + return $this->_data['name']; + } + public function setName($name) + { + $this->_data['name'] = $name; + } + + public function getDescription() + { + return $this->_data['description']; + } + public function setDescription($description) + { + $this->_data['description'] = $description; + } + + public function getApprovalChainId() + { + return $this->_data['approvalChain']; + } + public function setApprovalChainId($approvalChainId) + { + $this->_data['approvalChain'] = $approvalChainId; + } + + public function getModule() + { + return $this->_data['module']; + } + public function setModule($module) + { + $this->_data['module'] = $module; + } + +} diff --git a/application/modules/requests/views/scripts/request-type-admin/edit.phtml b/application/modules/requests/views/scripts/request-type-admin/edit.phtml new file mode 100644 index 0000000000000000000000000000000000000000..c65c69bd6c5e502106db0c39908d7618a4e39af3 --- /dev/null +++ b/application/modules/requests/views/scripts/request-type-admin/edit.phtml @@ -0,0 +1,40 @@ +<?php $this->layout()->breadcrumbs = array('Request Type Admin'); ?> +<?php $this->layout()->hideMenu = true; ?> + + +<?php if ($this->requestType->getId()) { ?> +<h1>Editing Type: <?php echo $this->requestType->getName(); ?></h1> +<?php } else { ?> +<h1>Creating New Request Type</h1> +<?php } ?> + +<form method="post" action="<?php echo $this->baseUrl(); ?>/requests/request-type-admin/edit.post"> + + <?php echo $this->formHidden('id', $this->requestType->getId()); ?> + + <label> + Name:<br /> + <?php echo $this->formText('name', $this->requestType->getName()); ?><br /> + </label> + + <label> + Description:<br /> + <?php echo $this->formText('description', $this->requestType->getDescription()); ?><br /> + </label> + + <label> + Description:<br /> + <?php echo $this->formSelect('module', $this->requestType->getModule(), null, array('courses' => 'Courses')); ?><br /> + </label> + + + <?php + if ($this->requestType->getId()) { + echo $this->formSubmit('submit', 'Update'); + } else { + echo $this->formSubmit('submit', 'Create'); + } + ?> + +</form> + \ No newline at end of file diff --git a/application/modules/requests/views/scripts/request-type-admin/index.phtml b/application/modules/requests/views/scripts/request-type-admin/index.phtml new file mode 100644 index 0000000000000000000000000000000000000000..a4c7861520f24e4f9eb75d9f481def0641892ebd --- /dev/null +++ b/application/modules/requests/views/scripts/request-type-admin/index.phtml @@ -0,0 +1,18 @@ +<?php $this->layout()->breadcrumbs = array('Request Type Admin'); ?> +<?php $this->layout()->hideMenu = true; ?> + +<ul class="editMenu"> + <?php $this->requestTypes->orderBy('getName'); ?> + <li> + <a href="<?php echo $this->baseUrl(); ?>/requests/request-type-admin/edit"> + --New Request Type-- + </a> + </li> + <?php foreach ($this->requestTypes as $requestType) { ?> + <li> + <a href="<?php echo $this->baseUrl(); ?>/requests/request-type-admin/edit/id/<?php echo $requestType->getId() ?>"> + <?php echo $requestType->getName(); ?> + </a> + </li> + <?php } ?> +</ul> \ No newline at end of file