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

Handle ::find(NULL) on ApprovalBody and ApprovalRole models.

parent 03aebc75
No related branches found
No related tags found
No related merge requests found
......@@ -2,10 +2,13 @@
class Requests_ApprovalBodyModel extends Unl_Model
{
static public function find($id)
{
if (Unl_Util::isArray($id) && count($id) == 0) {
return new Unl_Model_Collection(__CLASS__);
} else if ($id === NULL) {
return null;
}
$db = Zend_Registry::get('db');
......@@ -43,7 +46,7 @@ class Requests_ApprovalBodyModel extends Unl_Model
$records = $db->query($select)->fetchAll();
$approvalBodyIds = array();
foreach ($records as $record) {
$approvalBodyIds[] = $record['approvalBodyId'];
$approvalBodyIds[] = $record['approvalBodyId'];
}
return self::find($approvalBodyIds);
......@@ -115,7 +118,7 @@ class Requests_ApprovalBodyModel extends Unl_Model
/**
* Gets a freshly created model object
*
* @return Requests_CommentsModel
* @return Requests_ApprovalBodyModel
*/
static function fetchNew()
{
......@@ -126,31 +129,31 @@ class Requests_ApprovalBodyModel extends Unl_Model
'adminGroup' => null
);
$new = new self($data);
$new = new self($data);
$new->_setClean();
return $new;
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->getId()) {
if ($model->_cleanData != $model->_data) {
$modelsToUpdate[] = $model;
}
} else {
$modelsToInsert[] = $model;
}
}
$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);
......@@ -168,7 +171,7 @@ class Requests_ApprovalBodyModel extends Unl_Model
static protected function _update(Unl_Model_Collection $models)
{
$db = Zend_Registry::get('db');
$db = Zend_Registry::get('db');
$sql = 'CREATE TEMPORARY TABLE creqApprovalBodiesUpdate '
. 'SELECT * FROM creqApprovalBodies LIMIT 0';
......@@ -176,18 +179,18 @@ class Requests_ApprovalBodyModel extends Unl_Model
$sql = 'INSERT INTO creqApprovalBodiesUpdate VALUES ';
$sqlParts = array();
foreach ($models as $model) {
$sqlParts[] = $db->quoteInto('(?, ', $model->_data['approvalBodyId'])
foreach ($models as $model) {
$sqlParts[] = $db->quoteInto('(?, ', $model->_data['approvalBodyId'])
. $db->quoteInto('?, ' , $model->_data['name'])
. $db->quoteInto('?, ' , $model->_data['description'])
. $db->quoteInto('?)' , $model->_data['adminGroup']);
}
$sql .= implode(', ', $sqlParts);
$db->query($sql);
}
$sql .= implode(', ', $sqlParts);
$db->query($sql);
$sql = 'UPDATE creqApprovalBodies AS a, '
. ' creqApprovalBodiesUpdate AS b '
. 'SET a.name = b.name, '
$sql = 'UPDATE creqApprovalBodies AS a, '
. ' creqApprovalBodiesUpdate AS b '
. 'SET a.name = b.name, '
. ' a.description = b.description, '
. ' a.adminGroup = b.adminGroup '
. 'WHERE a.approvalBodyId = b.approvalBodyId ';
......@@ -198,7 +201,7 @@ class Requests_ApprovalBodyModel extends Unl_Model
{
$db = Zend_Registry::get('db');
$sql = 'INSERT INTO creqApprovalBodies (name, description, adminGroup) VALUES ';
$sql = 'INSERT INTO creqApprovalBodies (name, description, adminGroup) VALUES ';
$sqlParts = array();
foreach ($models as $model) {
$sqlParts[] = $db->quoteInto('(?, ', $model->_data['name'])
......@@ -217,7 +220,7 @@ class Requests_ApprovalBodyModel extends Unl_Model
public function getId()
{
return $this->_data['approvalBodyId'];
return $this->_data['approvalBodyId'];
}
public function getName()
......
......@@ -2,9 +2,15 @@
class Requests_ApprovalRoleModel extends Unl_Model
{
static public function find($id)
{
if (Unl_Util::isArray($id) && count($id) == 0) {
return new Unl_Model_Collection(__CLASS__);
} else if ($id === NULL) {
return null;
}
$db = Zend_Registry::get('db');
$select = new Zend_Db_Select($db);
......@@ -43,88 +49,88 @@ class Requests_ApprovalRoleModel extends Unl_Model
$records = $select->query()->fetchAll();
$roleIds = array();
foreach ($records as $record) {
$roleIds[] = $record['approvalBodyRoleId'];
$roleIds[] = $record['approvalBodyRoleId'];
}
return self::find($roleIds);
}
static public function findByUser($user)
{
$db = Zend_Registry::get('db');
$select = new Zend_Db_Select($db);
static public function findByUser($user)
{
$db = Zend_Registry::get('db');
$select = new Zend_Db_Select($db);
// get userId as a string or array from the user object(s)
// get userId as a string or array from the user object(s)
$objects = array();
if (Unl_Util::isArray($user)) {
$userId = array();
foreach ($user as $aUser) {
$userId[] = $aUser->getId();
$objects[$aUser->getId()] = new Unl_Model_Collection(__CLASS__);
}
} else if ($user instanceof Auth_UserModel) {
$userId = $user->getId();
if (Unl_Util::isArray($user)) {
$userId = array();
foreach ($user as $aUser) {
$userId[] = $aUser->getId();
$objects[$aUser->getId()] = new Unl_Model_Collection(__CLASS__);
}
} else if ($user instanceof Auth_UserModel) {
$userId = $user->getId();
$objects[$user->getId()] = new Unl_Model_Collection(__CLASS__);
} else {
throw new Exception('$user is not a Unl_Model_Collection or a Auth_UserModel');
}
// find the group(s) that the user(s) is a "primary" member of
$select = new Zend_Db_Select($db);
$select->from(array('u' => 'creqUsers'), array('userId'));
$select->join(array('r' => 'creqApprovalBodyRoles'), 'u.primaryGroup = r.group');
if (Unl_Util::isArray($userId)) {
$select->where('u.userId IN (?)', $userId);
} else {
$select->where('u.userId = ?', $userId);
}
$records = $select->query()->fetchAll();
foreach ($records as $record) {
$recordUserId = $record['userId'];
unset($record['userId']);
$object = Unl_Model_Registry::getInstance()->getOrAdd(new self($record));
$objectId = $object->getId();
$objects[$recordUserId][$objectId] = $object;
}
// find any groups that the user(s) is a "non-primary" member of
$select = new Zend_Db_Select($db);
$select->from(array('u' => 'creqUsers'), array('userId'));
$select->join(array('m' => 'creqGroupImpliedMemberships'), 'u.primaryGroup = m.childGroup', array());
$select->join(array('r' => 'creqApprovalBodyRoles'), 'm.parentGroup = r.group');
if (Unl_Util::isArray($userId)) {
$select->where('u.userId IN (?)', $userId);
} else {
$select->where('u.userId = ?', $userId);
}
$records = $select->query()->fetchAll();
foreach ($records as $record) {
$recordUserId = $record['userId'];
unset($record['userId']);
if (!$objects[$recordUserId]) {
$objects[$recordUserId] = new Unl_Model_Collection('Requests_ApprovalRoleModel');
}
$object = Unl_Model_Registry::getInstance()->getOrAdd(new self($record));
$objectId = $object->getId();
$objects[$recordUserId][$objectId] = $object;
}
// if we were passed an array of users, return an array for each user
if (Unl_Util::isArray($userId)) {
return $objects;
// otherwise, return an array of groups for the single user we were passed
} else {
return $objects[$userId];
}
}
public function findByParentBody($parentBody)
{
} else {
throw new Exception('$user is not a Unl_Model_Collection or a Auth_UserModel');
}
// find the group(s) that the user(s) is a "primary" member of
$select = new Zend_Db_Select($db);
$select->from(array('u' => 'creqUsers'), array('userId'));
$select->join(array('r' => 'creqApprovalBodyRoles'), 'u.primaryGroup = r.group');
if (Unl_Util::isArray($userId)) {
$select->where('u.userId IN (?)', $userId);
} else {
$select->where('u.userId = ?', $userId);
}
$records = $select->query()->fetchAll();
foreach ($records as $record) {
$recordUserId = $record['userId'];
unset($record['userId']);
$object = Unl_Model_Registry::getInstance()->getOrAdd(new self($record));
$objectId = $object->getId();
$objects[$recordUserId][$objectId] = $object;
}
// find any groups that the user(s) is a "non-primary" member of
$select = new Zend_Db_Select($db);
$select->from(array('u' => 'creqUsers'), array('userId'));
$select->join(array('m' => 'creqGroupImpliedMemberships'), 'u.primaryGroup = m.childGroup', array());
$select->join(array('r' => 'creqApprovalBodyRoles'), 'm.parentGroup = r.group');
if (Unl_Util::isArray($userId)) {
$select->where('u.userId IN (?)', $userId);
} else {
$select->where('u.userId = ?', $userId);
}
$records = $select->query()->fetchAll();
foreach ($records as $record) {
$recordUserId = $record['userId'];
unset($record['userId']);
if (!$objects[$recordUserId]) {
$objects[$recordUserId] = new Unl_Model_Collection('Requests_ApprovalRoleModel');
}
$object = Unl_Model_Registry::getInstance()->getOrAdd(new self($record));
$objectId = $object->getId();
$objects[$recordUserId][$objectId] = $object;
}
// if we were passed an array of users, return an array for each user
if (Unl_Util::isArray($userId)) {
return $objects;
// otherwise, return an array of groups for the single user we were passed
} else {
return $objects[$userId];
}
}
public function findByParentBody($parentBody)
{
$db = Zend_Registry::get('db');
if (Unl_Util::isArray($parentBody)) {
......@@ -155,13 +161,13 @@ class Requests_ApprovalRoleModel extends Unl_Model
$objects = array();
foreach ((array) $parentBody->getId() as $parentBodyId) {
$objects[$parentBodyId] = new Unl_Model_Collection(__CLASS__);
$objects[$parentBodyId] = new Unl_Model_Collection(__CLASS__);
}
foreach ($approvalBodyRoles as $approvalBodyId => $approvalRoleIds) {
foreach ($approvalRoleIds as $approvalRoleId) {
foreach ($approvalRoleIds as $approvalRoleId) {
$objects[$approvalBodyId][$approvalRoleId] = Unl_Model_Registry::getInstance()->get(__CLASS__, $approvalRoleId);
}
}
}
if (Unl_Util::isArray($parentBody)) {
......@@ -169,12 +175,12 @@ class Requests_ApprovalRoleModel extends Unl_Model
} else {
return array_pop($objects);
}
}
}
/**
* Gets a freshly created model object
*
* @return Requests_CommentsModel
* @return Requests_ApprovalRoleModel
*/
static function fetchNew()
{
......@@ -278,49 +284,49 @@ class Requests_ApprovalRoleModel extends Unl_Model
}
}
public function getId()
{
return $this->_data['approvalBodyRoleId'];
}
public function getName()
{
return $this->_data['name'];
}
public function setName($name)
{
$this->_data['name'] = $name;
}
public function getNotice()
{
return $this->_data['notice'];
}
public function setNotice($notice)
{
$this->_data['notice'] = $notice;
}
public function getApprovalBody()
{
return $this->_data['approvalBody'];
}
public function setApprovalBody(Requests_ApprovalBodyModel $approvalBody)
{
$this->_data['approvalBody'] = $approvalBody->getId();
}
public function getGroup()
{
return $this->_data['group'];
}
public function setGroup(Auth_GroupModel $group)
{
$this->_data['group'] = $group->getId();
}
public function getId()
{
return $this->_data['approvalBodyRoleId'];
}
public function getName()
{
return $this->_data['name'];
}
public function setName($name)
{
$this->_data['name'] = $name;
}
public function getNotice()
{
return $this->_data['notice'];
}
public function setNotice($notice)
{
$this->_data['notice'] = $notice;
}
public function getApprovalBody()
{
return $this->_data['approvalBody'];
}
public function setApprovalBody(Requests_ApprovalBodyModel $approvalBody)
{
$this->_data['approvalBody'] = $approvalBody->getId();
}
public function getGroup()
{
return $this->_data['group'];
}
public function setGroup(Auth_GroupModel $group)
{
$this->_data['group'] = $group->getId();
}
}
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