[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Settings/Roles/models/ -> Record.php (source)

   1  <?php
   2  /*+***********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   *************************************************************************************/
  10  
  11  /**
  12   * Roles Record Model Class
  13   */
  14  class Settings_Roles_Record_Model extends Settings_Vtiger_Record_Model {
  15  
  16      /**
  17       * Function to get the Id
  18       * @return <Number> Role Id
  19       */
  20  	public function getId() {
  21          return $this->get('roleid');
  22      }
  23  
  24      /**
  25       * Function to get the Role Name
  26       * @return <String>
  27       */
  28  	public function getName() {
  29          return $this->get('rolename');
  30      }
  31  
  32      /**
  33       * Function to get the depth of the role
  34       * @return <Number>
  35       */
  36  	public function getDepth() {
  37          return $this->get('depth');
  38      }
  39  
  40      /**
  41       * Function to get Parent Role hierarchy as a string
  42       * @return <String>
  43       */
  44  	public function getParentRoleString() {
  45          return $this->get('parentrole');
  46      }
  47  
  48      /**
  49       * Function to set the immediate parent role
  50       * @return <Settings_Roles_Record_Model> instance
  51       */
  52  	public function setParent($parentRole) {
  53          $this->parent = $parentRole;
  54          return $this;
  55      }
  56  
  57      /**
  58       * Function to get the immediate parent role
  59       * @return <Settings_Roles_Record_Model> instance
  60       */
  61  	public function getParent() {
  62          if(!$this->parent) {
  63              $parentRoleString = $this->getParentRoleString();
  64              $parentComponents = explode('::', $parentRoleString);
  65              $noOfRoles = count($parentComponents);
  66              // $currentRole = $parentComponents[$noOfRoles-1];
  67              if($noOfRoles > 1) {
  68                  $this->parent = self::getInstanceById($parentComponents[$noOfRoles-2]);
  69              } else {
  70                  $this->parent = null;
  71              }
  72          }
  73          return $this->parent;
  74      }
  75  
  76      /**
  77       * Function to get the immediate children roles
  78       * @return <Array> - List of Settings_Roles_Record_Model instances
  79       */
  80  	public function getChildren() {
  81          $db = PearDatabase::getInstance();
  82          if(!$this->children) {
  83              $parentRoleString = $this->getParentRoleString();
  84              $currentRoleDepth = $this->getDepth();
  85  
  86              $sql = 'SELECT * FROM vtiger_role WHERE parentrole LIKE ? AND depth = ?';
  87              $params = array($parentRoleString.'::%', $currentRoleDepth+1);
  88              $result = $db->pquery($sql, $params);
  89              $noOfRoles = $db->num_rows($result);
  90              $roles = array();
  91              for ($i=0; $i<$noOfRoles; ++$i) {
  92                  $role = self::getInstanceFromQResult($result, $i);
  93                  $roles[$role->getId()] = $role;
  94              }
  95              $this->children = $roles;
  96          }
  97          return $this->children;
  98      }
  99      
 100  	public function getSameLevelRoles() {
 101          $db = PearDatabase::getInstance();
 102          if(!$this->children) {
 103              $parentRoles = getParentRole($this->getId());
 104              $currentRoleDepth = $this->getDepth();
 105              $parentRoleString = '';
 106              foreach ($parentRoles as $key => $role) {
 107                  if(empty($parentRoleString)) $parentRoleString = $role;
 108                  else $parentRoleString = $parentRoleString.'::'.$role;
 109              }
 110              $sql = 'SELECT * FROM vtiger_role WHERE parentrole LIKE ? AND depth = ?';
 111              $params = array($parentRoleString.'::%', $currentRoleDepth);
 112              $result = $db->pquery($sql, $params);
 113              $noOfRoles = $db->num_rows($result);
 114              $roles = array();
 115              for ($i=0; $i<$noOfRoles; ++$i) {
 116                  $role = self::getInstanceFromQResult($result, $i);
 117                  $roles[$role->getId()] = $role;
 118              }
 119              $this->children = $roles;
 120          }
 121          return $this->children;
 122      }
 123  
 124      /**
 125       * Function to get all the children roles
 126       * @return <Array> - List of Settings_Roles_Record_Model instances
 127       */
 128  	public function getAllChildren() {
 129          $db = PearDatabase::getInstance();
 130  
 131          $parentRoleString = $this->getParentRoleString();
 132  
 133          $sql = 'SELECT * FROM vtiger_role WHERE parentrole LIKE ?';
 134          $params = array($parentRoleString.'::%');
 135          $result = $db->pquery($sql, $params);
 136          $noOfRoles = $db->num_rows($result);
 137          $roles = array();
 138          for ($i=0; $i<$noOfRoles; ++$i) {
 139              $role = self::getInstanceFromQResult($result, $i);
 140              $roles[$role->getId()] = $role;
 141          }
 142          return $roles;
 143      }
 144      
 145      /**
 146       * Function returns profiles related to the current role
 147       * @return <Array> - profile ids
 148       */
 149      public function getProfileIdList(){
 150          
 151          $db = PearDatabase::getInstance();
 152          $query = 'SELECT profileid FROM vtiger_role2profile WHERE roleid=?';
 153          
 154          $result = $db->pquery($query,array($this->getId()));
 155          $num_rows = $db->num_rows($result);
 156          
 157          $profilesList = array();
 158          for($i=0; $i<$num_rows; $i++) {
 159              $profilesList[] = $db->query_result($result,$i,'profileid');
 160          }
 161          return $profilesList;
 162      }
 163      
 164      /**
 165       * Function to get the profile id if profile is directly related to role
 166       * @return id
 167       */
 168      public function getDirectlyRelatedProfileId() {
 169          //TODO : see if you need cache the result
 170          $roleId = $this->getId();
 171          if(empty($roleId)) {
 172              return false;
 173          }
 174          
 175          $db = PearDatabase::getInstance();
 176          
 177          $query = 'SELECT directly_related_to_role, vtiger_profile.profileid FROM vtiger_role2profile 
 178                    INNER JOIN vtiger_profile ON vtiger_profile.profileid = vtiger_role2profile.profileid 
 179                    WHERE vtiger_role2profile.roleid=?';
 180          $params = array($this->getId());
 181          
 182          $result = $db->pquery($query,$params);
 183          
 184          if($db->num_rows($result) == 1 && $db->query_result($result,0,'directly_related_to_role') == '1'){
 185             return $db->query_result($result, 0, 'profileid');
 186          }
 187          return false;
 188      }
 189  
 190      /**
 191       * Function to get the Edit View Url for the Role
 192       * @return <String>
 193       */
 194  	public function getEditViewUrl() {
 195          return 'index.php?module=Roles&parent=Settings&view=Edit&record='.$this->getId();
 196      }
 197  
 198  //    public function getListViewEditUrl() {
 199  //        return '?module=Roles&parent=Settings&view=Edit&record='.$this->getId();
 200  //    }
 201  
 202      /**
 203       * Function to get the Create Child Role Url for the current role
 204       * @return <String>
 205       */
 206  	public function getCreateChildUrl() {
 207          return '?module=Roles&parent=Settings&view=Edit&parent_roleid='.$this->getId();
 208      }
 209  
 210      /**
 211       * Function to get the Delete Action Url for the current role
 212       * @return <String>
 213       */
 214  	public function getDeleteActionUrl() {
 215          return '?module=Roles&parent=Settings&view=DeleteAjax&record='.$this->getId();
 216      }
 217  
 218      /**
 219       * Function to get the Popup Window Url for the current role
 220       * @return <String>
 221       */
 222  	public function getPopupWindowUrl() {
 223          return 'module=Roles&parent=Settings&view=Popup&src_record='.$this->getId();
 224      }
 225  
 226      /**
 227       * Function to get all the profiles associated with the current role
 228       * @return <Array> Settings_Profiles_Record_Model instances
 229       */
 230  	public function getProfiles() {
 231          if(!$this->profiles) {
 232              $this->profiles = Settings_Profiles_Record_Model::getAllByRole($this->getId());
 233          }
 234          return $this->profiles;
 235      }
 236  
 237      /**
 238       * Function to add a child role to the current role
 239       * @param <Settings_Roles_Record_Model> $role
 240       * @return Settings_Roles_Record_Model instance
 241       */
 242  	public function addChildRole($role) {
 243          $role->setParent($this);
 244          $role->save();
 245          return $role;
 246      }
 247  
 248      /**
 249       * Function to move the current role and all its children nodes to the new parent role
 250       * @param <Settings_Roles_Record_Model> $newParentRole
 251       */
 252  	public function moveTo($newParentRole) {
 253          $currentDepth = $this->getDepth();
 254          $currentParentRoleString = $this->getParentRoleString();
 255  
 256          $newDepth = $newParentRole->getDepth() + 1;
 257          $newParentRoleString = $newParentRole->getParentRoleString() .'::'. $this->getId();
 258  
 259          $depthDifference = $newDepth - $currentDepth;
 260          $allChildren = $this->getAllChildren();
 261  
 262          $this->set('depth', $newDepth);
 263          $this->set('parentrole', $newParentRoleString);
 264          $this->set('allowassignedrecordsto', $this->get('allowassignedrecordsto'));
 265          $this->save();
 266  
 267          foreach($allChildren as $roleId => $roleModel) {
 268              $oldChildDepth = $roleModel->getDepth();
 269              $newChildDepth = $oldChildDepth + $depthDifference;
 270  
 271              $oldChildParentRoleString = $roleModel->getParentRoleString();
 272              $newChildParentRoleString = str_replace($currentParentRoleString, $newParentRoleString, $oldChildParentRoleString);
 273  
 274              $roleModel->set('depth', $newChildDepth);
 275              $roleModel->set('parentrole', $newChildParentRoleString);
 276              $roleModel->set('allowassignedrecordsto', $roleModel->get('allowassignedrecordsto'));
 277              $roleModel->save();
 278          }
 279      }
 280  
 281      /**
 282       * Function to save the role
 283       */
 284  	public function save() {
 285          $db = PearDatabase::getInstance();
 286          $roleId = $this->getId();
 287          $mode = 'edit';
 288  
 289          if(empty($roleId)) {
 290              $mode = '';
 291              $roleIdNumber = $db->getUniqueId('vtiger_role');
 292              $roleId = 'H'.$roleIdNumber;
 293          }
 294          $parentRole = $this->getParent();
 295          if($parentRole != null) {
 296              $this->set('depth', $parentRole->getDepth()+1);
 297              $this->set('parentrole', $parentRole->getParentRoleString() .'::'. $roleId);
 298          }
 299  
 300          if($mode == 'edit') {
 301              $sql = 'UPDATE vtiger_role SET rolename=?, parentrole=?, depth=?, allowassignedrecordsto=? WHERE roleid=?';
 302              $params = array($this->getName(), $this->getParentRoleString(), $this->getDepth(), $this->get('allowassignedrecordsto'), $roleId);
 303              $db->pquery($sql, $params);
 304          } else {
 305              $sql = 'INSERT INTO vtiger_role(roleid, rolename, parentrole, depth, allowassignedrecordsto) VALUES (?,?,?,?,?)';
 306              $params = array($roleId, $this->getName(), $this->getParentRoleString(), $this->getDepth(), $this->get('allowassignedrecordsto'));
 307              $db->pquery($sql, $params);
 308              $picklist2RoleSQL = "INSERT INTO vtiger_role2picklist SELECT '".$roleId."',picklistvalueid,picklistid,sortid
 309                      FROM vtiger_role2picklist WHERE roleid = ?";
 310              $db->pquery($picklist2RoleSQL, array($parentRole->getId()));
 311          }
 312  
 313          $profileIds = $this->get('profileIds');
 314          if(empty($profileIds)) {
 315              $profiles = $this->getProfiles();
 316              if(!empty($profiles) && count($profiles) > 0) {
 317                  $profileIds = array_keys($profiles);
 318              }
 319          }
 320          if(!empty($profileIds)) {
 321              $noOfProfiles = count($profileIds);
 322              if($noOfProfiles > 0) {
 323                  $db->pquery('DELETE FROM vtiger_role2profile WHERE roleid=?', array($roleId));
 324  
 325                  $sql = 'INSERT INTO vtiger_role2profile(roleid, profileid) VALUES (?,?)';
 326                  for($i=0; $i<$noOfProfiles; ++$i) {
 327                      $params = array($roleId, $profileIds[$i]);
 328                      $db->pquery($sql, $params);
 329                  }
 330              }
 331          }
 332      }
 333  
 334      /**
 335       * Function to delete the role
 336       * @param <Settings_Roles_Record_Model> $transferToRole
 337       */
 338  	public function delete($transferToRole) {
 339          $db = PearDatabase::getInstance();
 340          $roleId = $this->getId();
 341          $transferRoleId = $transferToRole->getId();
 342  
 343          $db->pquery('UPDATE vtiger_user2role SET roleid=? WHERE roleid=?', array($transferRoleId, $roleId));
 344  
 345          $db->pquery('DELETE FROM vtiger_role2profile WHERE roleid=?', array($roleId));
 346          $db->pquery('DELETE FROM vtiger_group2role WHERE roleid=?', array($roleId));
 347          $db->pquery('DELETE FROM vtiger_group2rs WHERE roleandsubid=?', array($roleId));
 348  
 349          //delete handling for sharing rules
 350          deleteRoleRelatedSharingRules($roleId);
 351  
 352          $db->pquery('DELETE FROM vtiger_role WHERE roleid=?', array($roleId));
 353  
 354          $allChildren = $this->getAllChildren();
 355          $transferParentRoleSequence = $transferToRole->getParentRoleString();
 356          $currentParentRoleSequence = $this->getParentRoleString();
 357  
 358          foreach($allChildren as $roleId => $roleModel) {
 359              $oldChildParentRoleString = $roleModel->getParentRoleString();
 360              $newChildParentRoleString = str_replace($currentParentRoleSequence, $transferParentRoleSequence, $oldChildParentRoleString);
 361              $newChildDepth = count(explode('::', $newChildParentRoleString))-1;
 362              $roleModel->set('depth', $newChildDepth);
 363              $roleModel->set('parentrole', $newChildParentRoleString);
 364              $roleModel->save();
 365          }
 366      }
 367  
 368      /**
 369       * Function to get the list view actions for the record
 370       * @return <Array> - Associate array of Vtiger_Link_Model instances
 371       */
 372  	public function getRecordLinks() {
 373  
 374          $links = array();
 375          if($this->getParent()) {
 376              $recordLinks = array(
 377                  array(
 378                      'linktype' => 'LISTVIEWRECORD',
 379                      'linklabel' => 'LBL_EDIT_RECORD',
 380                      'linkurl' => $this->getListViewEditUrl(),
 381                      'linkicon' => 'icon-pencil'
 382                  ),
 383                  array(
 384                      'linktype' => 'LISTVIEWRECORD',
 385                      'linklabel' => 'LBL_DELETE_RECORD',
 386                      'linkurl' => $this->getDeleteActionUrl(),
 387                      'linkicon' => 'icon-trash'
 388                  )
 389              );
 390              foreach($recordLinks as $recordLink) {
 391                  $links[] = Vtiger_Link_Model::getInstanceFromValues($recordLink);
 392              }
 393          }
 394  
 395          return $links;
 396      }
 397  
 398      /**
 399       * Function to get the instance of Roles record model from query result
 400       * @param <Object> $result
 401       * @param <Number> $rowNo
 402       * @return Settings_Roles_Record_Model instance
 403       */
 404  	public static function getInstanceFromQResult($result, $rowNo) {
 405          $db = PearDatabase::getInstance();
 406          $row = $db->query_result_rowdata($result, $rowNo);
 407          $role = new self();
 408          return $role->setData($row);
 409      }
 410  
 411      /**
 412       * Function to get all the roles
 413       * @param <Boolean> $baseRole
 414       * @return <Array> list of Role models <Settings_Roles_Record_Model>
 415       */
 416  	public static function getAll($baseRole = false) {
 417          $db = PearDatabase::getInstance();
 418          $params = array();
 419  
 420          $sql = 'SELECT * FROM vtiger_role';
 421          if (!$baseRole) {
 422              $sql .= ' WHERE depth != ?';
 423              $params[] = 0;
 424          }
 425          $sql .= ' ORDER BY parentrole';
 426  
 427          $result = $db->pquery($sql, $params);
 428          $noOfRoles = $db->num_rows($result);
 429  
 430          $roles = array();
 431          for ($i=0; $i<$noOfRoles; ++$i) {
 432              $role = self::getInstanceFromQResult($result, $i);
 433              $roles[$role->getId()] = $role;
 434          }
 435          return $roles;
 436      }
 437  
 438      /**
 439       * Function to get the instance of Role model, given role id
 440       * @param <Integer> $roleId
 441       * @return Settings_Roles_Record_Model instance, if exists. Null otherwise
 442       */
 443  	public static function getInstanceById($roleId) {
 444          $db = PearDatabase::getInstance();
 445  
 446          $sql = 'SELECT * FROM vtiger_role WHERE roleid = ?';
 447          $params = array($roleId);
 448          $result = $db->pquery($sql, $params);
 449          if($db->num_rows($result) > 0) {
 450              return self::getInstanceFromQResult($result, 0);
 451          }
 452          return null;
 453      }
 454  
 455      /**
 456       * Function to get the instance of Base Role model
 457       * @return Settings_Roles_Record_Model instance, if exists. Null otherwise
 458       */
 459  	public static function getBaseRole() {
 460          $db = PearDatabase::getInstance();
 461  
 462          $sql = 'SELECT * FROM vtiger_role WHERE depth=0 LIMIT 1';
 463          $params = array();
 464          $result = $db->pquery($sql, $params);
 465          if($db->num_rows($result) > 0) {
 466              return self::getInstanceFromQResult($result, 0);
 467          }
 468          return null;
 469      }
 470      
 471      /* Function to get the instance of the role by Name
 472      * @param type $name -- name of the role
 473      * @return null/role instance
 474      */
 475     public static function getInstanceByName($name, $excludedRecordId = array()) {
 476         $db = PearDatabase::getInstance();
 477         $sql = 'SELECT * FROM vtiger_role WHERE rolename=?';
 478         $params = array($name);
 479         if(!empty($excludedRecordId)){
 480             $sql.= ' AND roleid NOT IN ('.generateQuestionMarks($excludedRecordId).')';
 481             $params = array_merge($params,$excludedRecordId);
 482         }
 483         $result = $db->pquery($sql, $params);
 484         if($db->num_rows($result) > 0) {
 485             return self::getInstanceFromQResult($result, 0);
 486         }
 487         return null;
 488     }
 489  
 490     /**
 491      * Function to get Users who are from this role
 492      * @return <Array> User record models list <Users_Record_Model>
 493      */
 494     public function getUsers() {
 495         $db = PearDatabase::getInstance();
 496         $result = $db->pquery('SELECT userid FROM vtiger_user2role WHERE roleid = ?', array($this->getId()));
 497         $numOfRows = $db->num_rows($result);
 498  
 499         $usersList = array();
 500         for($i=0; $i<$numOfRows; $i++) {
 501             $userId = $db->query_result($result, $i, 'userid');
 502             $usersList[$userId] = Users_Record_Model::getInstanceById($userId, 'Users');
 503         }
 504         return $usersList;
 505     }
 506  }


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1