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

Added admin interface for request types at /requests/request-type-admin

parent 68c4ed8a
No related branches found
No related tags found
No related merge requests found
<?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());
}
}
<?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;
}
}
<?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
<?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
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