Spaces:
Sleeping
Sleeping
File size: 22,412 Bytes
07c3cdd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 | <?php
namespace ProcessMaker\BusinessModel;
use BasePeer;
use Configurations;
use Criteria;
use Department as DepartmentModel;
use DepartmentPeer;
use Exception;
use ProcessMaker\BusinessModel\Validator;
use Propel;
use RBAC;
use ResultSet;
use Users;
use UsersPeer;
use G;
class Department
{
/**
* Verify if exists the title of a Department
*
* @param string $departmentTitle Title
* @param string $departmentUidExclude Unique id of Department to exclude
* @return bool Return true if exists the title of a Department, false otherwise
*/
public function existsTitle($departmentTitle, $departmentUidExclude = "")
{
try {
$criteria = new Criteria("workflow");
$criteria->addSelectColumn(DepartmentPeer::DEP_UID);
$criteria->addSelectColumn(DepartmentPeer::DEP_TITLE);
if ($departmentUidExclude != "") {
$criteria->add(DepartmentPeer::DEP_UID, $departmentUidExclude, Criteria::NOT_EQUAL);
}
$criteria->add(DepartmentPeer::DEP_TITLE, $departmentTitle, Criteria::EQUAL);
$rsCriteria = DepartmentPeer::doSelectRS($criteria);
return ($rsCriteria->next())? true : false;
} catch (Exception $e) {
throw $e;
}
}
/**
* Verify if the User is not in a Department
*
* @param string $departmentUid
* @param string $userUid
* @return void Throw exception user not exists
*/
private function throwExceptionUserNotExistsInDepartment($departmentUid, $userUid)
{
try {
$user = UsersPeer::retrieveByPK($userUid);
if (is_null($user) || $user->getDepUid() != $departmentUid) {
throw new Exception(G::LoadTranslation('ID_USER_NOT_EXIST_DEPARTMENT', [$userUid]));
}
} catch (Exception $e) {
throw $e;
}
}
/**
* Verify if exists the title of a Department
*
* @param string $departmentTitle Title
* @param string $fieldNameForException Field name for the exception
* @param string $departmentUidExclude Unique id of Department to exclude
* @return void Throw exception if exists the title of a Department
*/
public function throwExceptionIfExistsTitle($departmentTitle, $fieldNameForException, $departmentUidExclude = "")
{
try {
if ($this->existsTitle($departmentTitle, $departmentUidExclude)) {
throw new Exception(G::LoadTranslation("ID_DEPARTMENT_TITLE_ALREADY_EXISTS", array($fieldNameForException, $departmentTitle)));
}
} catch (Exception $e) {
throw $e;
}
}
/**
* Get Department record
*
* @param string $departmentUid Unique id of Department
* @param array $arrayVariableNameForException Variable name for exception
* @param bool $throwException Flag to throw the exception if the main parameters are invalid or do not exist
* (TRUE: throw the exception; FALSE: returns FALSE)
* @return array Returns an array with Department record, ThrowTheException/FALSE otherwise
*/
public function getDepartmentRecordByPk(
$departmentUid,
array $arrayVariableNameForException,
$throwException = true
) {
try {
$obj = DepartmentPeer::retrieveByPK($departmentUid);
if (is_null($obj)) {
if ($throwException) {
throw new Exception(G::LoadTranslation(
'ID_DEPARTMENT_NOT_EXIST', [$arrayVariableNameForException['$departmentUid'], $departmentUid]
));
} else {
return false;
}
}
//Return
return $obj->toArray(BasePeer::TYPE_FIELDNAME);
} catch (Exception $e) {
throw $e;
}
}
/**
* Get list for Departments
*
* @access public
* @return array
*/
public function getDepartments()
{
$oDepartment = new DepartmentModel();
$aDepts = $oDepartment->getDepartments('');
foreach ($aDepts as &$depData) {
$depData['DEP_CHILDREN'] = $this->getChildren($depData);
$depData = array_change_key_case($depData, CASE_LOWER);
}
return $aDepts;
}
/**
* Assign User to Department
*
* @param string $departmentUid Unique id of Department
* @param array $arrayData Data
* return array Return data of the User assigned to Department
*/
public function assignUser($departmentUid, array $arrayData)
{
try {
//Verify data
$process = new Process();
$validator = new Validator();
$validator->throwExceptionIfDataIsNotArray($arrayData, "\$arrayData");
$validator->throwExceptionIfDataIsEmpty($arrayData, "\$arrayData");
//Set data
$arrayData = array_change_key_case($arrayData, CASE_UPPER);
unset($arrayData["DEP_UID"]);
//Set variables
$arrayUserFieldDefinition = array(
"DEP_UID" => array("type" => "string", "required" => false, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "departmentUid"),
"USR_UID" => array("type" => "string", "required" => true, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "userUid")
);
$arrayUserFieldNameForException = array(
"departmentUid" => strtolower("DEP_UID"),
"userUid" => strtolower("USR_UID")
);
//Verify data
$departmentUid = Validator::depUid($departmentUid);
$process->throwExceptionIfDataNotMetFieldDefinition($arrayData, $arrayUserFieldDefinition, $arrayUserFieldNameForException, true);
$process->throwExceptionIfNotExistsUser($arrayData["USR_UID"], $arrayUserFieldNameForException["userUid"]);
//Assign User
$department = new DepartmentModel();
$department->load($departmentUid);
$department->addUserToDepartment($departmentUid, $arrayData["USR_UID"], ($department->getDepManager() == "")? true : false, false);
$department->updateDepartmentManager($departmentUid);
//Return
$arrayData = array_merge(array("DEP_UID" => $departmentUid), $arrayData);
$arrayData = array_change_key_case($arrayData, CASE_LOWER);
return $arrayData;
} catch (Exception $e) {
throw $e;
}
}
/**
* Post Unassign User
*
* @access public
* @return void
*/
public function unassignUser($dep_uid, $usr_uid)
{
$dep_uid = Validator::depUid($dep_uid);
$usr_uid = Validator::usrUid($usr_uid);
$this->throwExceptionUserNotExistsInDepartment($dep_uid, $usr_uid);
$dep = new DepartmentModel();
$dep->load( $dep_uid );
$manager = $dep->getDepManager();
$dep->removeUserFromDepartment( $dep_uid, $usr_uid );
if ($usr_uid == $manager) {
$editDepto['DEP_UID'] = $dep_uid;
$editDepto['DEP_MANAGER'] = '';
$dep->update( $editDepto );
$dep->updateDepartmentManager($dep_uid);
}
}
/**
* Get custom record
*
* @param array $record Record
* @return array Return an array with custom record
*/
private function __getUserCustomRecordFromRecord(array $record)
{
try {
$recordc = [
'usr_uid' => $record['USR_UID'],
'usr_username' => $record['USR_USERNAME'],
'usr_firstname' => $record['USR_FIRSTNAME'],
'usr_lastname' => $record['USR_LASTNAME'],
'usr_status' => $record['USR_STATUS']
];
if (isset($record['USR_SUPERVISOR'])) {
$recordc['usr_supervisor'] = $record['USR_SUPERVISOR'];
}
return $recordc;
} catch (Exception $e) {
throw $e;
}
}
/**
* Get all Users of a Department (Assigned/Available)
*
* @param string $departmentUid Unique id of Department
* @param string $option Option (ASSIGNED, AVAILABLE)
* @param array $arrayFilterData Data of the filters
* @param string $sortField Field name to sort
* @param string $sortDir Direction of sorting (ASC, DESC)
* @param int $start Start
* @param int $limit Limit
* @param bool $flagRecord Flag that set the "getting" of record
* @param bool $throwException Flag to throw the exception (This only if the parameters are invalid)
* (TRUE: throw the exception; FALSE: returns FALSE)
* @return array Return an array with all Users of a Department, ThrowTheException/FALSE otherwise
*/
public function getUsers(
$departmentUid,
$option,
array $arrayFilterData = null,
$sortField = null,
$sortDir = null,
$start = null,
$limit = null,
$flagRecord = true,
$throwException = true
) {
try {
$arrayUser = array();
$numRecTotal = 0;
//Verify data and Set variables
$flagFilter = !is_null($arrayFilterData) && is_array($arrayFilterData) && isset($arrayFilterData['filter']);
$result = Validator::validatePagerDataByPagerDefinition(
['$start' => $start, '$limit' => $limit],
['$start' => '$start', '$limit' => '$limit']
);
if ($result !== true) {
if ($throwException) {
throw new Exception($result);
} else {
return false;
}
}
$arrayDepartmentData = $this->getDepartmentRecordByPk(
$departmentUid, ['$departmentUid' => '$departmentUid'], $throwException
);
if ($arrayDepartmentData === false) {
return false;
}
//Set variables
$filterName = 'filter';
if ($flagFilter) {
$arrayAux = [
'' => 'filter',
'LEFT' => 'lfilter',
'RIGHT' => 'rfilter'
];
$filterName = $arrayAux[
(isset($arrayFilterData['filterOption']))? $arrayFilterData['filterOption'] : ''
];
}
//Get data
if (!is_null($limit) && (string)($limit) == '0') {
return [
'total' => $numRecTotal,
'start' => (int)((!is_null($start))? $start : 0),
'limit' => (int)((!is_null($limit))? $limit : 0),
$filterName => ($flagFilter)? $arrayFilterData['filter'] : '',
'data' => $arrayUser
];
}
//Query
$criteria = new Criteria('workflow');
$criteria->addSelectColumn(UsersPeer::USR_UID);
$criteria->addSelectColumn(UsersPeer::USR_USERNAME);
$criteria->addSelectColumn(UsersPeer::USR_FIRSTNAME);
$criteria->addSelectColumn(UsersPeer::USR_LASTNAME);
$criteria->addSelectColumn(UsersPeer::USR_STATUS);
$criteria->add(UsersPeer::USR_STATUS, 'CLOSED', Criteria::NOT_EQUAL);
switch ($option) {
case 'ASSIGNED':
$criteria->add(UsersPeer::DEP_UID, $departmentUid, Criteria::EQUAL);
break;
case 'AVAILABLE':
$criteria->add(UsersPeer::DEP_UID, '', Criteria::EQUAL);
$criteria->add(UsersPeer::USR_UID, RBAC::GUEST_USER_UID, Criteria::NOT_EQUAL);
break;
}
if ($flagFilter && trim($arrayFilterData['filter']) != '') {
$arraySearch = [
'' => '%' . $arrayFilterData['filter'] . '%',
'LEFT' => $arrayFilterData['filter'] . '%',
'RIGHT' => '%' . $arrayFilterData['filter']
];
$search = $arraySearch[
(isset($arrayFilterData['filterOption']))? $arrayFilterData['filterOption'] : ''
];
$criteria->add(
$criteria->getNewCriterion(UsersPeer::USR_USERNAME, $search, Criteria::LIKE)->addOr(
$criteria->getNewCriterion(UsersPeer::USR_FIRSTNAME, $search, Criteria::LIKE)->addOr(
$criteria->getNewCriterion(UsersPeer::USR_LASTNAME, $search, Criteria::LIKE)))
);
}
//Number records total
$numRecTotal = UsersPeer::doCount($criteria);
//Query
$conf = new Configurations();
$sortFieldDefault = UsersPeer::TABLE_NAME . '.' . $conf->userNameFormatGetFirstFieldByUsersTable();
if (!is_null($sortField) && trim($sortField) != '') {
$sortField = strtoupper($sortField);
if (in_array(UsersPeer::TABLE_NAME . '.' . $sortField, $criteria->getSelectColumns())) {
$sortField = UsersPeer::TABLE_NAME . '.' . $sortField;
} else {
$sortField = $sortFieldDefault;
}
} else {
$sortField = $sortFieldDefault;
}
if (!is_null($sortDir) && trim($sortDir) != '' && strtoupper($sortDir) == 'DESC') {
$criteria->addDescendingOrderByColumn($sortField);
} else {
$criteria->addAscendingOrderByColumn($sortField);
}
if (!is_null($start)) {
$criteria->setOffset((int)($start));
}
if (!is_null($limit)) {
$criteria->setLimit((int)($limit));
}
$rsCriteria = UsersPeer::doSelectRS($criteria);
$rsCriteria->setFetchmode(ResultSet::FETCHMODE_ASSOC);
while ($rsCriteria->next()) {
$record = $rsCriteria->getRow();
switch ($option) {
case 'ASSIGNED':
$record['USR_SUPERVISOR'] = $record['USR_UID'] == $arrayDepartmentData['DEP_MANAGER'];
break;
case 'AVAILABLE':
break;
}
$arrayUser[] = ($flagRecord)? $record : $this->__getUserCustomRecordFromRecord($record);
}
//Return
return [
'total' => $numRecTotal,
'start' => (int)((!is_null($start))? $start : 0),
'limit' => (int)((!is_null($limit))? $limit : 0),
$filterName => ($flagFilter)? $arrayFilterData['filter'] : '',
'data' => $arrayUser
];
} catch (Exception $e) {
throw $e;
}
}
/**
* Put Set Manager User
*
* @access public
* @return void
*/
public function setManagerUser($dep_uid, $usr_uid)
{
$dep_uid = Validator::depUid($dep_uid);
$usr_uid = Validator::usrUid($usr_uid);
$oCriteria = new Criteria( 'workflow' );
$oCriteria->addSelectColumn( DepartmentPeer::DEP_UID );
$oCriteria->add( DepartmentPeer::DEP_MANAGER, $usr_uid, Criteria::EQUAL );
$oDataset = DepartmentPeer::doSelectRS( $oCriteria );
$oDataset->setFetchmode( ResultSet::FETCHMODE_ASSOC );
if ($oDataset->next()) {
throw (new Exception(G::LoadTranslation("ID_DEPARTMENT_MANAGER_EXIST", array('usr_uid',$usr_uid))));
}
$editDepartment['DEP_UID'] = $dep_uid;
$editDepartment['DEP_MANAGER'] = $usr_uid;
$oDept = new DepartmentModel();
$oDept->update( $editDepartment );
$oDept->updateDepartmentManager( $dep_uid );
$oDept = new DepartmentModel();
$oDept->Load($dep_uid);
$oDept->addUserToDepartment($dep_uid, $usr_uid, ($oDept->getDepManager() == "")? true : false, false);
$oDept->updateDepartmentManager($dep_uid);
}
/**
* Get list for Departments
*
* @var string $dep_uid. Uid for Department
* @access public
* @return array
*/
public function getDepartment($dep_uid)
{
$dep_uid = Validator::depUid($dep_uid);
$criteria = new Criteria( 'workflow' );
$criteria->add( DepartmentPeer::DEP_UID, $dep_uid, Criteria::EQUAL );
$con = Propel::getConnection( DepartmentPeer::DATABASE_NAME );
$objects = DepartmentPeer::doSelect( $criteria, $con );
$oUsers = new Users();
$node = array ();
foreach ($objects as $oDepartment) {
$node['DEP_UID'] = $oDepartment->getDepUid();
$node['DEP_PARENT'] = $oDepartment->getDepParent();
$node['DEP_TITLE'] = $oDepartment->getDepTitle();
$node['DEP_STATUS'] = $oDepartment->getDepStatus();
$node['DEP_MANAGER'] = $oDepartment->getDepManager();
$node['DEP_LDAP_DN'] = $oDepartment->getDepLdapDn();
$node['DEP_LAST'] = 0;
$manager = $oDepartment->getDepManager();
if ($manager != '') {
$UserUID = $oUsers->load($manager);
$node['DEP_MANAGER_USERNAME'] = isset( $UserUID['USR_USERNAME'] ) ? $UserUID['USR_USERNAME'] : '';
$node['DEP_MANAGER_FIRSTNAME'] = isset( $UserUID['USR_FIRSTNAME'] ) ? $UserUID['USR_FIRSTNAME'] : '';
$node['DEP_MANAGER_LASTNAME'] = isset( $UserUID['USR_LASTNAME'] ) ? $UserUID['USR_LASTNAME'] : '';
} else {
$node['DEP_MANAGER_USERNAME'] = '';
$node['DEP_MANAGER_FIRSTNAME'] = '';
$node['DEP_MANAGER_LASTNAME'] = '';
}
$criteria = new Criteria();
$criteria->add(UsersPeer::DEP_UID, $dep_uid, Criteria::EQUAL );
$node['DEP_MEMBERS'] = UsersPeer::doCount($criteria);
$criteriaCount = new Criteria( 'workflow' );
$criteriaCount->clearSelectColumns();
$criteriaCount->addSelectColumn( 'COUNT(*)' );
$criteriaCount->add( DepartmentPeer::DEP_PARENT, $oDepartment->getDepUid(), Criteria::EQUAL );
$rs = DepartmentPeer::doSelectRS( $criteriaCount );
$rs->next();
$row = $rs->getRow();
$node['HAS_CHILDREN'] = $row[0];
}
$node = array_change_key_case($node, CASE_LOWER);
return $node;
}
/**
* Save Department
*
* @var string $dep_data. Data for Process
* @var string $create. Flag for create or update
* @access public
* @return array
*/
public function saveDepartment($dep_data, $create = true)
{
Validator::isArray($dep_data, '$dep_data');
Validator::isNotEmpty($dep_data, '$dep_data');
Validator::isBoolean($create, '$create');
$dep_data = array_change_key_case($dep_data, CASE_UPPER);
if ($create) {
unset($dep_data["DEP_UID"]);
}
$oDepartment = new DepartmentModel();
if (isset($dep_data['DEP_UID']) && $dep_data['DEP_UID'] != '') {
Validator::depUid($dep_data['DEP_UID']);
}
if (isset($dep_data['DEP_PARENT']) && $dep_data['DEP_PARENT'] != '') {
Validator::depUid($dep_data['DEP_PARENT'], 'dep_parent');
}
if (isset($dep_data['DEP_MANAGER']) && $dep_data['DEP_MANAGER'] != '') {
Validator::usrUid($dep_data['DEP_MANAGER'], 'dep_manager');
}
if (isset($dep_data['DEP_STATUS'])) {
Validator::depStatus($dep_data['DEP_STATUS']);
}
if (!$create) {
if (isset($dep_data["DEP_TITLE"])) {
$this->throwExceptionIfExistsTitle($dep_data["DEP_TITLE"], strtolower("DEP_TITLE"), $dep_data["DEP_UID"]);
$dep_data["DEPO_TITLE"] = $dep_data["DEP_TITLE"];
}
$oDepartment->update($dep_data);
$oDepartment->updateDepartmentManager($dep_data['DEP_UID']);
} else {
if (isset($dep_data['DEP_TITLE'])) {
$this->throwExceptionIfExistsTitle($dep_data["DEP_TITLE"], strtolower("DEP_TITLE"));
} else {
throw (new Exception(G::LoadTranslation("ID_FIELD_REQUIRED", array('dep_title'))));
}
$dep_uid = $oDepartment->create($dep_data);
$response = $this->getDepartment($dep_uid);
return $response;
}
}
/**
* Delete department
* @var string $dep_uid. Uid for department
*
* @access public
* @return array
*/
public function deleteDepartment($dep_uid)
{
$dep_uid = Validator::depUid($dep_uid);
$oDepartment = new DepartmentModel();
$countUsers = $oDepartment->cantUsersInDepartment($dep_uid);
if ($countUsers != 0) {
throw (new Exception(G::LoadTranslation("ID_CANT_DELETE_DEPARTMENT_HAS_USERS")));
}
$dep_data = $this->getDepartment($dep_uid);
if ($dep_data['has_children'] != 0) {
throw (new Exception(G::LoadTranslation("ID_CANT_DELETE_DEPARTMENT_HAS_CHILDREN")));
}
$oDepartment->remove($dep_uid);
}
/**
* Look for Children for department
*
* @var array $dataDep. Data for child department
* @access public
* @return array
*/
protected function getChildren ($dataDep)
{
$children = array();
if ((int)$dataDep['HAS_CHILDREN'] > 0) {
$oDepartment = new DepartmentModel();
$aDepts = $oDepartment->getDepartments($dataDep['DEP_UID']);
foreach ($aDepts as &$depData) {
$depData['DEP_CHILDREN'] = $this->getChildren($depData);
$depData = array_change_key_case($depData, CASE_LOWER);
$children[] = $depData;
}
}
return $children;
}
}
|