2014-05-27 16:25:57 -04:00
< ? php
2018-03-06 16:41:21 -04:00
2014-05-27 16:25:57 -04:00
namespace ProcessMaker\BusinessModel ;
2018-03-06 16:41:21 -04:00
use Configurations ;
use Content ;
use Criteria ;
use DateTime ;
use Exception ;
use G ;
use ProcessMaker\Util\Common ;
use ResultSet ;
use Roles as ModelRoles ;
use RolesPeer ;
use UsersRolesPeer ;
require_once PATH_RBAC . 'model' . PATH_SEP . 'Roles.php' ;
2014-05-27 16:25:57 -04:00
class Role
{
2018-03-06 16:41:21 -04:00
private $arrayFieldDefinition = [
'ROL_UID' => [ 'type' => 'string' , 'required' => false , 'empty' => false , 'defaultValues' => [], 'fieldNameAux' => 'roleUid' ],
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
'ROL_CODE' => [ 'type' => 'string' , 'required' => true , 'empty' => false , 'defaultValues' => [], 'fieldNameAux' => 'roleCode' ],
'ROL_NAME' => [ 'type' => 'string' , 'required' => true , 'empty' => false , 'defaultValues' => [], 'fieldNameAux' => 'roleName' ],
'ROL_STATUS' => [ 'type' => 'string' , 'required' => false , 'empty' => false , 'defaultValues' => [ 'ACTIVE' , 'INACTIVE' ], 'fieldNameAux' => 'roleStatus' ]
];
2014-05-27 16:25:57 -04:00
private $formatFieldNameInUppercase = true ;
2018-03-06 16:41:21 -04:00
private $arrayFieldNameForException = [
'filter' => 'FILTER' ,
'start' => 'START' ,
'limit' => 'LIMIT'
];
const SYSTEM_RBAC = '00000000000000000000000000000001' ;
const SYSTEM_PROCESSMAKER = '00000000000000000000000000000002' ;
2014-05-27 16:25:57 -04:00
/**
2018-03-06 16:41:21 -04:00
* Role constructor .
2014-05-27 16:25:57 -04:00
*
2018-03-06 16:41:21 -04:00
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
public function __construct ()
{
try {
foreach ( $this -> arrayFieldDefinition as $key => $value ) {
$this -> arrayFieldNameForException [ $value [ " fieldNameAux " ]] = $key ;
}
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Set the format of the fields name ( uppercase , lowercase )
*
* @ param bool $flag Value that set the format
*
2018-03-06 16:41:21 -04:00
* @ return void
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
public function setFormatFieldNameInUppercase ( $flag )
{
try {
$this -> formatFieldNameInUppercase = $flag ;
$this -> setArrayFieldNameForException ( $this -> arrayFieldNameForException );
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Set exception messages for fields
*
* @ param array $arrayData Data with the fields
*
2018-03-06 16:41:21 -04:00
* @ return void
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
2014-05-28 15:01:18 -04:00
public function setArrayFieldNameForException ( array $arrayData )
2014-05-27 16:25:57 -04:00
{
try {
foreach ( $arrayData as $key => $value ) {
$this -> arrayFieldNameForException [ $key ] = $this -> getFieldNameByFormatFieldName ( $value );
}
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Get the name of the field according to the format
*
* @ param string $fieldName Field name
*
2018-03-06 16:41:21 -04:00
* @ return string Return the field name according the format
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
public function getFieldNameByFormatFieldName ( $fieldName )
{
try {
2018-03-06 16:41:21 -04:00
return ( $this -> formatFieldNameInUppercase ) ? strtoupper ( $fieldName ) : strtolower ( $fieldName );
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Verify if exists the code of a Role
*
2018-03-06 16:41:21 -04:00
* @ param string $roleCode Code
* @ param string $roleSystemUid Unique id of System ( 00000000000000000000000000000001 : RBAC ,
* 00000000000000000000000000000002 : PROCESSMAKER )
2014-05-27 16:25:57 -04:00
* @ param string $roleUidExclude Unique id of Role to exclude
*
2018-03-06 16:41:21 -04:00
* @ return bool Return true if exists the code of a Role , false otherwise
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
public function existsCode ( $roleCode , $roleSystemUid , $roleUidExclude = " " )
{
try {
2018-03-06 16:41:21 -04:00
$criteria = new Criteria ( " rbac " );
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$criteria -> addSelectColumn ( RolesPeer :: ROL_UID );
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$criteria -> add ( RolesPeer :: ROL_SYSTEM , $roleSystemUid , Criteria :: EQUAL );
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
if ( ! empty ( $roleUidExclude )) {
$criteria -> add ( RolesPeer :: ROL_UID , $roleUidExclude , Criteria :: NOT_EQUAL );
2014-05-27 16:25:57 -04:00
}
2018-03-06 16:41:21 -04:00
$criteria -> add ( RolesPeer :: ROL_CODE , $roleCode , Criteria :: EQUAL );
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$rsCriteria = RolesPeer :: doSelectRS ( $criteria );
2014-05-27 16:25:57 -04:00
if ( $rsCriteria -> next ()) {
return true ;
} else {
return false ;
}
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Verify if exists the name of a Role
*
2018-03-06 16:41:21 -04:00
* @ param string $roleName Name
* @ param string $roleSystemUid Unique id of System ( 00000000000000000000000000000001 : RBAC ,
* 00000000000000000000000000000002 : PROCESSMAKER )
2014-05-27 16:25:57 -04:00
* @ param string $roleUidExclude Unique id of Role to exclude
*
2018-03-06 16:41:21 -04:00
* @ return bool Return true if exists the name of a Role , false otherwise
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
public function existsName ( $roleName , $roleSystemUid , $roleUidExclude = " " )
{
try {
//Set variables
2018-03-06 16:41:21 -04:00
$content = new Content ();
$role = new ModelRoles ();
2014-05-27 16:25:57 -04:00
$arrayContentByRole = $content -> getAllContentsByRole ();
//SQL
2018-03-06 16:41:21 -04:00
$criteria = new Criteria ( " rbac " );
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$criteria -> addSelectColumn ( RolesPeer :: ROL_UID );
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$criteria -> add ( RolesPeer :: ROL_SYSTEM , $roleSystemUid , Criteria :: EQUAL );
2014-05-27 16:25:57 -04:00
if ( $roleUidExclude != " " ) {
2018-03-06 16:41:21 -04:00
$criteria -> add ( RolesPeer :: ROL_UID , $roleUidExclude , Criteria :: NOT_EQUAL );
2014-05-27 16:25:57 -04:00
}
2018-03-06 16:41:21 -04:00
$rsCriteria = RolesPeer :: doSelectRS ( $criteria );
$rsCriteria -> setFetchmode ( ResultSet :: FETCHMODE_ASSOC );
2014-05-27 16:25:57 -04:00
while ( $rsCriteria -> next ()) {
$row = $rsCriteria -> getRow ();
$roleUid = $row [ " ROL_UID " ];
if ( isset ( $arrayContentByRole [ $roleUid ])) {
$roleNameAux = $arrayContentByRole [ $roleUid ];
} else {
$rowAux = $role -> load ( $roleUid );
$roleNameAux = $rowAux [ " ROL_NAME " ];
}
if ( $roleNameAux == $roleName ) {
return true ;
}
}
return false ;
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Verify if does not exist the Role in table ROLES
*
2018-03-06 16:41:21 -04:00
* @ param string $roleUid Unique id of Role
2014-05-27 16:25:57 -04:00
* @ param string $fieldNameForException Field name for the exception
*
2018-03-06 16:41:21 -04:00
* @ return void Throw exception if does not exist the Role in table ROLES
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
public function throwExceptionIfNotExistsRole ( $roleUid , $fieldNameForException )
{
try {
2018-03-06 16:41:21 -04:00
$obj = RolesPeer :: retrieveByPK ( $roleUid );
2014-05-27 16:25:57 -04:00
2014-05-28 15:01:18 -04:00
if ( is_null ( $obj )) {
2018-03-06 16:41:21 -04:00
throw new Exception ( G :: LoadTranslation ( " ID_ROLE_DOES_NOT_EXIST " , [ $fieldNameForException , $roleUid ]));
2014-05-27 16:25:57 -04:00
}
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Verify if exists the code of a Role
*
2018-03-06 16:41:21 -04:00
* @ param string $roleCode Code
* @ param string $roleSystemUid Unique id of System ( 00000000000000000000000000000001 : RBAC ,
* 00000000000000000000000000000002 : PROCESSMAKER )
2014-05-27 16:25:57 -04:00
* @ param string $fieldNameForException Field name for the exception
2018-03-06 16:41:21 -04:00
* @ param string $roleUidExclude Unique id of Role to exclude
2014-05-27 16:25:57 -04:00
*
2018-03-06 16:41:21 -04:00
* @ return void Throw exception if exists the code of a Role
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
public function throwExceptionIfExistsCode ( $roleCode , $roleSystemUid , $fieldNameForException , $roleUidExclude = " " )
{
try {
if ( $this -> existsCode ( $roleCode , $roleSystemUid , $roleUidExclude )) {
2018-03-06 16:41:21 -04:00
throw new Exception ( G :: LoadTranslation ( " ID_ROLE_CODE_ALREADY_EXISTS " , [ $fieldNameForException , $roleCode ]));
2014-05-27 16:25:57 -04:00
}
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Verify if exists the name of a Role
*
2018-03-06 16:41:21 -04:00
* @ param string $roleName Name
* @ param string $roleSystemUid Unique id of System ( 00000000000000000000000000000001 : RBAC ,
* 00000000000000000000000000000002 : PROCESSMAKER )
2014-05-27 16:25:57 -04:00
* @ param string $fieldNameForException Field name for the exception
2018-03-06 16:41:21 -04:00
* @ param string $roleUidExclude Unique id of Role to exclude
2014-05-27 16:25:57 -04:00
*
2018-03-06 16:41:21 -04:00
* @ return void Throw exception if exists the name of a Role
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
public function throwExceptionIfExistsName ( $roleName , $roleSystemUid , $fieldNameForException , $roleUidExclude = " " )
{
try {
if ( $this -> existsName ( $roleName , $roleSystemUid , $roleUidExclude )) {
2018-03-06 16:41:21 -04:00
throw new Exception ( G :: LoadTranslation ( " ID_ROLE_NAME_ALREADY_EXISTS " , [ $fieldNameForException , $roleName ]));
2014-05-27 16:25:57 -04:00
}
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Validate the data if they are invalid ( INSERT and UPDATE )
*
2018-03-06 16:41:21 -04:00
* @ param string $roleUid Unique id of Role
* @ param array $arrayData Data
2014-05-27 16:25:57 -04:00
*
2018-03-06 16:41:21 -04:00
* @ return void Throw exception if data has an invalid value
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
2014-05-28 15:01:18 -04:00
public function throwExceptionIfDataIsInvalid ( $roleUid , array $arrayData )
2014-05-27 16:25:57 -04:00
{
try {
//Set variables
2018-03-06 16:41:21 -04:00
$arrayRoleData = ( $roleUid == " " ) ? [] : $this -> getRole ( $roleUid , true );
$flagInsert = ( $roleUid == " " ) ? true : false ;
2014-05-27 16:25:57 -04:00
$arrayDataMain = array_merge ( $arrayRoleData , $arrayData );
//Verify data - Field definition
2018-03-06 16:41:21 -04:00
$process = new Process ();
2014-05-27 16:25:57 -04:00
$process -> throwExceptionIfDataNotMetFieldDefinition ( $arrayData , $this -> arrayFieldDefinition , $this -> arrayFieldNameForException , $flagInsert );
//Verify data
if ( isset ( $arrayData [ " ROL_CODE " ]) && ! preg_match ( " /^ \ w+ $ / " , $arrayData [ " ROL_CODE " ])) {
2018-03-06 16:41:21 -04:00
throw new Exception ( G :: LoadTranslation ( " ID_ROLE_FIELD_CANNOT_CONTAIN_SPECIAL_CHARACTERS " , [ $this -> arrayFieldNameForException [ " roleCode " ]]));
2014-05-27 16:25:57 -04:00
}
if ( isset ( $arrayData [ " ROL_CODE " ])) {
$this -> throwExceptionIfExistsCode ( $arrayData [ " ROL_CODE " ], $arrayDataMain [ " ROL_SYSTEM " ], $this -> arrayFieldNameForException [ " roleCode " ], $roleUid );
}
if ( isset ( $arrayData [ " ROL_NAME " ])) {
$this -> throwExceptionIfExistsName ( $arrayData [ " ROL_NAME " ], $arrayDataMain [ " ROL_SYSTEM " ], $this -> arrayFieldNameForException [ " roleName " ], $roleUid );
}
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Create Role
*
* @ param array $arrayData Data
*
2018-03-06 16:41:21 -04:00
* @ return array Return data of the new Role created
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
2014-05-28 15:01:18 -04:00
public function create ( array $arrayData )
2014-05-27 16:25:57 -04:00
{
try {
//Verify data
2018-03-06 16:41:21 -04:00
$validator = new Validator ();
2014-05-27 16:25:57 -04:00
$validator -> throwExceptionIfDataIsEmpty ( $arrayData , " \$ arrayData " );
//Set data
$arrayData = array_change_key_case ( $arrayData , CASE_UPPER );
2018-03-06 16:41:21 -04:00
unset ( $arrayData [ 'ROL_UID' ]);
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$arrayData [ 'ROL_SYSTEM' ] = self :: SYSTEM_PROCESSMAKER ;
2014-05-27 16:25:57 -04:00
//Verify data
2018-03-06 16:41:21 -04:00
$this -> throwExceptionIfDataIsInvalid ( '' , $arrayData );
2014-05-27 16:25:57 -04:00
//Create
2018-03-06 16:41:21 -04:00
$role = new ModelRoles ();
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$roleUid = Common :: generateUID ();
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$arrayData [ 'ROL_UID' ] = $roleUid ;
$arrayData [ 'ROL_STATUS' ] = isset ( $arrayData [ 'ROL_STATUS' ]) ? $arrayData [ 'ROL_STATUS' ] === 'ACTIVE' ? 1 : 0 : 1 ;
$arrayData [ 'ROL_CREATE_DATE' ] = date ( 'Y-M-d H:i:s' );
$arrayData [ 'ROL_UPDATE_DATE' ] = date ( 'Y-M-d H:i:s' );
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$role -> createRole ( $arrayData );
2014-05-27 16:25:57 -04:00
//Return
return $this -> getRole ( $roleUid );
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Update Role
*
2018-03-06 16:41:21 -04:00
* @ param string $roleUid Unique id of Role
* @ param array $arrayData Data
2014-05-27 16:25:57 -04:00
*
2018-03-06 16:41:21 -04:00
* @ return array Return data of the Role updated
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
2014-05-28 15:01:18 -04:00
public function update ( $roleUid , array $arrayData )
2014-05-27 16:25:57 -04:00
{
try {
//Verify data
2018-03-06 16:41:21 -04:00
$validator = new Validator ();
2014-05-27 16:25:57 -04:00
$validator -> throwExceptionIfDataIsEmpty ( $arrayData , " \$ arrayData " );
//Set data
$arrayData = array_change_key_case ( $arrayData , CASE_UPPER );
2014-06-12 15:57:15 -04:00
$arrayDataBackup = $arrayData ;
2014-05-27 16:25:57 -04:00
$arrayRoleData = $this -> getRole ( $roleUid );
//Verify data
2018-03-06 16:41:21 -04:00
$this -> throwExceptionIfNotExistsRole ( $roleUid , $this -> arrayFieldNameForException [ 'roleUid' ]);
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
if ( $roleUid === self :: SYSTEM_PROCESSMAKER ) {
throw new Exception ( G :: LoadTranslation ( 'ID_ROLES_MSG' ));
2014-05-29 13:10:11 -04:00
}
2014-05-27 16:25:57 -04:00
$this -> throwExceptionIfDataIsInvalid ( $roleUid , $arrayData );
//Update
2018-03-06 16:41:21 -04:00
$role = new ModelRoles ();
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$arrayData [ 'ROL_UID' ] = $roleUid ;
$arrayData [ 'ROL_UPDATE_DATE' ] = date ( 'Y-M-d H:i:s' );
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
if ( ! isset ( $arrayData [ 'ROL_NAME' ])) {
$arrayData [ 'ROL_NAME' ] = $arrayRoleData [ $this -> getFieldNameByFormatFieldName ( 'ROL_NAME' )];
2014-05-27 16:25:57 -04:00
}
2018-03-06 16:41:21 -04:00
if ( isset ( $arrayData [ 'ROL_STATUS' ])) {
$arrayData [ 'ROL_STATUS' ] = $arrayData [ 'ROL_STATUS' ] === 'ACTIVE' ? 1 : 0 ;
2014-05-27 16:25:57 -04:00
}
2018-03-06 16:41:21 -04:00
$role -> updateRole ( $arrayData );
2014-05-27 16:25:57 -04:00
$arrayData = $arrayDataBackup ;
//Return
if ( ! $this -> formatFieldNameInUppercase ) {
$arrayData = array_change_key_case ( $arrayData , CASE_LOWER );
}
return $arrayData ;
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Delete Role
*
* @ param string $roleUid Unique id of Role
*
2018-03-06 16:41:21 -04:00
* @ return void
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
public function delete ( $roleUid )
{
try {
2018-03-06 16:41:21 -04:00
$role = new ModelRoles ();
2014-05-27 16:25:57 -04:00
//Verify data
$this -> throwExceptionIfNotExistsRole ( $roleUid , $this -> arrayFieldNameForException [ " roleUid " ]);
if ( $role -> numUsersWithRole ( $roleUid ) > 0 ) {
2018-03-06 16:41:21 -04:00
throw new Exception ( G :: LoadTranslation ( " ID_ROLES_CAN_NOT_DELETE " ));
2014-05-27 16:25:57 -04:00
}
//Delete
$result = $role -> removeRole ( $roleUid );
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Get criteria for Role
*
2018-03-06 16:41:21 -04:00
* @ return object
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
public function getRoleCriteria ()
{
try {
2018-03-06 16:41:21 -04:00
$criteria = new Criteria ( " rbac " );
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$criteria -> addSelectColumn ( RolesPeer :: ROL_UID );
$criteria -> addSelectColumn ( RolesPeer :: ROL_PARENT );
$criteria -> addSelectColumn ( RolesPeer :: ROL_CODE );
$criteria -> addSelectColumn ( RolesPeer :: ROL_STATUS );
$criteria -> addSelectColumn ( RolesPeer :: ROL_SYSTEM );
$criteria -> addSelectColumn ( RolesPeer :: ROL_CREATE_DATE );
$criteria -> addSelectColumn ( RolesPeer :: ROL_UPDATE_DATE );
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$criteria -> add ( RolesPeer :: ROL_SYSTEM , self :: SYSTEM_PROCESSMAKER , Criteria :: EQUAL ); //PROCESSMAKER
2014-05-27 16:25:57 -04:00
return $criteria ;
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Get data of a Role from a record
*
* @ param array $record Record
*
2018-03-06 16:41:21 -04:00
* @ return array Return an array with data Role
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
2014-05-28 15:01:18 -04:00
public function getRoleDataFromRecord ( array $record )
2014-05-27 16:25:57 -04:00
{
try {
2018-03-06 16:41:21 -04:00
$conf = new Configurations ();
2014-05-27 16:25:57 -04:00
$confEnvSetting = $conf -> getFormats ();
2018-03-06 16:41:21 -04:00
$dateTime = new DateTime ( $record [ 'ROL_CREATE_DATE' ]);
$roleCreateDate = $dateTime -> format ( $confEnvSetting [ 'dateFormat' ]);
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$roleUpdateDate = '' ;
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
if ( ! empty ( $record [ 'ROL_UPDATE_DATE' ])) {
$dateTime = new DateTime ( $record [ 'ROL_UPDATE_DATE' ]);
$roleUpdateDate = $dateTime -> format ( $confEnvSetting [ 'dateFormat' ]);
2014-05-27 16:25:57 -04:00
}
2018-03-06 16:41:21 -04:00
return [
$this -> getFieldNameByFormatFieldName ( 'ROL_UID' ) => $record [ 'ROL_UID' ],
$this -> getFieldNameByFormatFieldName ( 'ROL_CODE' ) => $record [ 'ROL_CODE' ],
$this -> getFieldNameByFormatFieldName ( 'ROL_NAME' ) => $record [ 'ROL_NAME' ],
$this -> getFieldNameByFormatFieldName ( 'ROL_STATUS' ) => $record [ 'ROL_STATUS' ] . '' === '1' ? 'ACTIVE' : 'INACTIVE' ,
$this -> getFieldNameByFormatFieldName ( 'ROL_SYSTEM' ) => $record [ 'ROL_SYSTEM' ],
$this -> getFieldNameByFormatFieldName ( 'ROL_CREATE_DATE' ) => $roleCreateDate ,
$this -> getFieldNameByFormatFieldName ( 'ROL_UPDATE_DATE' ) => $roleUpdateDate ,
$this -> getFieldNameByFormatFieldName ( 'ROL_TOTAL_USERS' ) => ( int ) $record [ 'ROL_TOTAL_USERS' ]
];
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Get all Roles
*
2018-03-06 16:41:21 -04:00
* @ 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
2014-05-27 16:25:57 -04:00
*
2018-03-06 16:41:21 -04:00
* @ return array Return an array with all Roles
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
2014-05-28 15:01:18 -04:00
public function getRoles ( array $arrayFilterData = null , $sortField = null , $sortDir = null , $start = null , $limit = null )
2014-05-27 16:25:57 -04:00
{
try {
2018-03-06 16:41:21 -04:00
$arrayRole = [];
2014-05-27 16:25:57 -04:00
//Verify data
2018-03-06 16:41:21 -04:00
$process = new Process ();
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$process -> throwExceptionIfDataNotMetPagerVarDefinition ([ " start " => $start , " limit " => $limit ], $this -> arrayFieldNameForException );
2014-05-27 16:25:57 -04:00
//Get data
if ( ! is_null ( $limit ) && $limit . " " == " 0 " ) {
return $arrayRole ;
}
//Set variables
2018-03-06 16:41:21 -04:00
$content = new Content ();
$role = new ModelRoles ();
2014-05-27 16:25:57 -04:00
$arrayContentByRole = $content -> getAllContentsByRole ();
//SQL
$criteria = $this -> getRoleCriteria ();
2018-03-06 16:41:21 -04:00
$criteria -> addAsColumn ( " ROL_TOTAL_USERS " , " (SELECT COUNT( " . UsersRolesPeer :: ROL_UID . " ) FROM " . UsersRolesPeer :: TABLE_NAME . " WHERE " . UsersRolesPeer :: ROL_UID . " = " . RolesPeer :: ROL_UID . " ) " );
2014-05-27 16:25:57 -04:00
if ( ! is_null ( $arrayFilterData ) && is_array ( $arrayFilterData ) && isset ( $arrayFilterData [ " filter " ]) && trim ( $arrayFilterData [ " filter " ]) != " " ) {
2018-03-06 16:41:21 -04:00
$criteria -> add ( RolesPeer :: ROL_CODE , " % " . $arrayFilterData [ " filter " ] . " % " , Criteria :: LIKE );
2014-05-27 16:25:57 -04:00
}
//SQL
if ( ! is_null ( $sortField ) && trim ( $sortField ) != " " ) {
$sortField = strtoupper ( $sortField );
2018-03-06 16:41:21 -04:00
if ( in_array ( $sortField , [ " ROL_UID " , " ROL_PARENT " , " ROL_STATUS " , " ROL_SYSTEM " , " ROL_CREATE_DATE " , " ROL_UPDATE_DATE " ])) {
$sortField = RolesPeer :: TABLE_NAME . " . " . $sortField ;
2014-05-27 16:25:57 -04:00
} else {
2018-03-06 16:41:21 -04:00
$sortField = RolesPeer :: ROL_CODE ;
2014-05-27 16:25:57 -04:00
}
} else {
2018-03-06 16:41:21 -04:00
$sortField = RolesPeer :: ROL_CODE ;
2014-05-27 16:25:57 -04:00
}
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 ));
}
2018-03-06 16:41:21 -04:00
$rsCriteria = RolesPeer :: doSelectRS ( $criteria );
$rsCriteria -> setFetchmode ( ResultSet :: FETCHMODE_ASSOC );
2014-05-27 16:25:57 -04:00
while ( $rsCriteria -> next ()) {
$row = $rsCriteria -> getRow ();
$roleUid = $row [ " ROL_UID " ];
if ( isset ( $arrayContentByRole [ $roleUid ])) {
$roleName = $arrayContentByRole [ $roleUid ];
} else {
$rowAux = $role -> load ( $roleUid );
$roleName = $rowAux [ " ROL_NAME " ];
}
$row [ " ROL_NAME " ] = $roleName ;
$arrayRole [] = $this -> getRoleDataFromRecord ( $row );
}
//Return
return $arrayRole ;
2018-03-06 16:41:21 -04:00
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
/**
* Get data of a Role
*
2018-03-06 16:41:21 -04:00
* @ param string $roleUid Unique id of Role
* @ param bool $flagGetRecord Value that set the getting
2014-05-27 16:25:57 -04:00
*
2018-03-06 16:41:21 -04:00
* @ return array Return an array with data of a Role
* @ throws Exception
2014-05-27 16:25:57 -04:00
*/
public function getRole ( $roleUid , $flagGetRecord = false )
{
try {
//Verify data
$this -> throwExceptionIfNotExistsRole ( $roleUid , $this -> arrayFieldNameForException [ " roleUid " ]);
//Set variables
if ( ! $flagGetRecord ) {
2018-03-06 16:41:21 -04:00
$content = new Content ();
$role = new ModelRoles ();
2014-05-27 16:25:57 -04:00
$arrayContentByRole = $content -> getAllContentsByRole ();
}
//Get data
//SQL
$criteria = $this -> getRoleCriteria ();
if ( ! $flagGetRecord ) {
2018-03-06 16:41:21 -04:00
$criteria -> addAsColumn ( " ROL_TOTAL_USERS " , " (SELECT COUNT( " . UsersRolesPeer :: ROL_UID . " ) FROM " . UsersRolesPeer :: TABLE_NAME . " WHERE " . UsersRolesPeer :: ROL_UID . " = " . RolesPeer :: ROL_UID . " ) " );
2014-05-27 16:25:57 -04:00
}
2018-03-06 16:41:21 -04:00
$criteria -> add ( RolesPeer :: ROL_UID , $roleUid , Criteria :: EQUAL );
2014-05-27 16:25:57 -04:00
2018-03-06 16:41:21 -04:00
$rsCriteria = RolesPeer :: doSelectRS ( $criteria );
$rsCriteria -> setFetchmode ( ResultSet :: FETCHMODE_ASSOC );
2014-05-27 16:25:57 -04:00
$rsCriteria -> next ();
$row = $rsCriteria -> getRow ();
if ( ! $flagGetRecord ) {
if ( isset ( $arrayContentByRole [ $roleUid ])) {
$roleName = $arrayContentByRole [ $roleUid ];
} else {
$rowAux = $role -> load ( $roleUid );
$roleName = $rowAux [ " ROL_NAME " ];
}
$row [ " ROL_NAME " ] = $roleName ;
}
//Return
2018-03-06 16:41:21 -04:00
return ( ! $flagGetRecord ) ? $this -> getRoleDataFromRecord ( $row ) : $row ;
} catch ( Exception $e ) {
2014-05-27 16:25:57 -04:00
throw $e ;
}
}
}