[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Vtiger/ -> CRMEntity.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  class Vtiger_CRMEntity extends CRMEntity {
  12      var $db, $log; // Used in class functions of CRMEntity
  13      var $column_fields = Array();
  14  
  15      /** Indicator if this is a custom module or standard module */
  16      var $IsCustomModule = true;
  17  
  18      // Placeholder for sort fields - All the fields will be initialized for Sorting through initSortFields
  19      var $sortby_fields = Array();
  20  
  21      // Required Information for enabling Import feature
  22      var $required_fields = Array ('assigned_user_id'=>1);
  23  
  24      // Callback function list during Importing
  25      var $special_functions = Array('set_import_assigned_user');
  26  
  27  	function __construct() {
  28          global $log;
  29          $this->column_fields = getColumnFields(get_class($this));
  30          $this->db = new PearDatabase();
  31          $this->log = $log;
  32      }
  33  
  34  	function save_module($module) {
  35          // Custom Save for Module
  36      }
  37  
  38      /**
  39       * Return query to use based on given modulename, fieldname
  40       * Useful to handle specific case handling for Popup
  41       */
  42  	function getQueryByModuleField($module, $fieldname, $srcrecord) {
  43          // $srcrecord could be empty
  44      }
  45  
  46      /**
  47       * Get list view query.
  48       */
  49  	function getListQuery($module, $where='') {
  50          $query = "SELECT vtiger_crmentity.*, $this->table_name.*";
  51  
  52          // Select Custom Field Table Columns if present
  53          if(!empty($this->customFieldTable)) $query .= ", " . $this->customFieldTable[0] . ".* ";
  54  
  55          $query .= " FROM $this->table_name";
  56  
  57          $query .= "    INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = $this->table_name.$this->table_index";
  58  
  59          // Consider custom table join as well.
  60          if(!empty($this->customFieldTable)) {
  61              $query .= " INNER JOIN ".$this->customFieldTable[0]." ON ".$this->customFieldTable[0].'.'.$this->customFieldTable[1] .
  62                        " = $this->table_name.$this->table_index";
  63          }
  64          $query .= " LEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid";
  65          $query .= " LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid";
  66  
  67          $linkedModulesQuery = $this->db->pquery("SELECT distinct fieldname, columnname, relmodule FROM vtiger_field" .
  68                  " INNER JOIN vtiger_fieldmodulerel ON vtiger_fieldmodulerel.fieldid = vtiger_field.fieldid" .
  69                  " WHERE uitype='10' AND vtiger_fieldmodulerel.module=?", array($module));
  70          $linkedFieldsCount = $this->db->num_rows($linkedModulesQuery);
  71  
  72          for($i=0; $i<$linkedFieldsCount; $i++) {
  73              $related_module = $this->db->query_result($linkedModulesQuery, $i, 'relmodule');
  74              $fieldname = $this->db->query_result($linkedModulesQuery, $i, 'fieldname');
  75              $columnname = $this->db->query_result($linkedModulesQuery, $i, 'columnname');
  76  
  77              $other = CRMEntity::getInstance($related_module);
  78              vtlib_setup_modulevars($related_module, $other);
  79  
  80              $query .= " LEFT JOIN $other->table_name ON $other->table_name.$other->table_index =".
  81                      "$this->table_name.$columnname";
  82          }
  83  
  84          global $current_user;
  85          $query .= $this->getNonAdminAccessControlQuery($module,$current_user);
  86          $query .= "WHERE vtiger_crmentity.deleted = 0 ".$where;
  87          return $query;
  88      }
  89  
  90      /**
  91       * Apply security restriction (sharing privilege) query part for List view.
  92       */
  93  	function getListViewSecurityParameter($module) {
  94          global $current_user;
  95          require('user_privileges/user_privileges_'.$current_user->id.'.php');
  96          require('user_privileges/sharing_privileges_'.$current_user->id.'.php');
  97  
  98          $sec_query = '';
  99          $tabid = getTabid($module);
 100  
 101          if($is_admin==false && $profileGlobalPermission[1] == 1 && $profileGlobalPermission[2] == 1
 102              && $defaultOrgSharingPermission[$tabid] == 3) {
 103  
 104                  $sec_query .= " AND (vtiger_crmentity.smownerid in($current_user->id) OR vtiger_crmentity.smownerid IN
 105                      (
 106                          SELECT vtiger_user2role.userid FROM vtiger_user2role
 107                          INNER JOIN vtiger_users ON vtiger_users.id=vtiger_user2role.userid
 108                          INNER JOIN vtiger_role ON vtiger_role.roleid=vtiger_user2role.roleid
 109                          WHERE vtiger_role.parentrole LIKE '".$current_user_parent_role_seq."::%'
 110                      )
 111                      OR vtiger_crmentity.smownerid IN
 112                      (
 113                          SELECT shareduserid FROM vtiger_tmp_read_user_sharing_per
 114                          WHERE userid=".$current_user->id." AND tabid=".$tabid."
 115                      )
 116                      OR
 117                          (";
 118  
 119                      // Build the query based on the group association of current user.
 120                      if(sizeof($current_user_groups) > 0) {
 121                          $sec_query .= " vtiger_groups.groupid IN (". implode(",", $current_user_groups) .") OR ";
 122                      }
 123                      $sec_query .= " vtiger_groups.groupid IN
 124                          (
 125                              SELECT vtiger_tmp_read_group_sharing_per.sharedgroupid
 126                              FROM vtiger_tmp_read_group_sharing_per
 127                              WHERE userid=".$current_user->id." and tabid=".$tabid."
 128                          )";
 129                  $sec_query .= ")
 130                  )";
 131          }
 132          return $sec_query;
 133      }
 134  
 135      /**
 136       * Create query to export the records.
 137       */
 138  	function create_export_query($where)
 139      {
 140          global $current_user,$currentModule;
 141  
 142          include ("include/utils/ExportUtils.php");
 143  
 144          //To get the Permitted fields query and the permitted fields list
 145          $sql = getPermittedFieldsQuery('ServiceContracts', "detail_view");
 146  
 147          $fields_list = getFieldsListFromQuery($sql);
 148  
 149          $query = "SELECT $fields_list, vtiger_users.user_name AS user_name
 150                      FROM vtiger_crmentity INNER JOIN $this->table_name ON vtiger_crmentity.crmid=$this->table_name.$this->table_index";
 151  
 152          if(!empty($this->customFieldTable)) {
 153              $query .= " INNER JOIN ".$this->customFieldTable[0]." ON ".$this->customFieldTable[0].'.'.$this->customFieldTable[1] .
 154                        " = $this->table_name.$this->table_index";
 155          }
 156  
 157          $query .= " LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid";
 158          $query .= " LEFT JOIN vtiger_users ON vtiger_crmentity.smownerid = vtiger_users.id and ".
 159          "vtiger_users.status='Active'";
 160  
 161          $linkedModulesQuery = $this->db->pquery("SELECT distinct fieldname, columnname, relmodule FROM vtiger_field" .
 162                  " INNER JOIN vtiger_fieldmodulerel ON vtiger_fieldmodulerel.fieldid = vtiger_field.fieldid" .
 163                  " WHERE uitype='10' AND vtiger_fieldmodulerel.module=?", array($thismodule));
 164          $linkedFieldsCount = $this->db->num_rows($linkedModulesQuery);
 165  
 166          for($i=0; $i<$linkedFieldsCount; $i++) {
 167              $related_module = $this->db->query_result($linkedModulesQuery, $i, 'relmodule');
 168              $fieldname = $this->db->query_result($linkedModulesQuery, $i, 'fieldname');
 169              $columnname = $this->db->query_result($linkedModulesQuery, $i, 'columnname');
 170  
 171              $other = CRMEntity::getInstance($related_module);
 172              vtlib_setup_modulevars($related_module, $other);
 173  
 174              $query .= " LEFT JOIN $other->table_name ON $other->table_name.$other->table_index = ".
 175                      "$this->table_name.$columnname";
 176          }
 177  
 178          $query .= $this->getNonAdminAccessControlQuery($thismodule,$current_user);
 179          $where_auto = " vtiger_crmentity.deleted=0";
 180  
 181          if($where != '') $query .= " WHERE ($where) AND $where_auto";
 182          else $query .= " WHERE $where_auto";
 183  
 184          return $query;
 185      }
 186  
 187      /**
 188       * Function which will give the basic query to find duplicates
 189       */
 190  	function getDuplicatesQuery($module,$table_cols,$field_values,$ui_type_arr,$select_cols='') {
 191          $select_clause = "SELECT ". $this->table_name .".".$this->table_index ." AS recordid, vtiger_users_last_import.deleted,".$table_cols;
 192  
 193          // Select Custom Field Table Columns if present
 194          if(isset($this->customFieldTable)) $query .= ", " . $this->customFieldTable[0] . ".* ";
 195  
 196          $from_clause = " FROM $this->table_name";
 197  
 198          $from_clause .= "    INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = $this->table_name.$this->table_index";
 199  
 200          // Consider custom table join as well.
 201          if(isset($this->customFieldTable)) {
 202              $from_clause .= " INNER JOIN ".$this->customFieldTable[0]." ON ".$this->customFieldTable[0].'.'.$this->customFieldTable[1] .
 203                        " = $this->table_name.$this->table_index";
 204          }
 205          $from_clause .= " LEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid
 206                  LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid";
 207  
 208          $where_clause = "    WHERE vtiger_crmentity.deleted = 0";
 209          $where_clause .= $this->getListViewSecurityParameter($module);
 210  
 211          if (isset($select_cols) && trim($select_cols) != '') {
 212              $sub_query = "SELECT $select_cols FROM  $this->table_name AS t " .
 213                  " INNER JOIN vtiger_crmentity AS crm ON crm.crmid = t.".$this->table_index;
 214              // Consider custom table join as well.
 215              if(isset($this->customFieldTable)) {
 216                  $sub_query .= " INNER JOIN ".$this->customFieldTable[0]." tcf ON tcf.".$this->customFieldTable[1]." = t.$this->table_index";
 217              }
 218              $sub_query .= " WHERE crm.deleted=0 GROUP BY $select_cols HAVING COUNT(*)>1";
 219          } else {
 220              $sub_query = "SELECT $table_cols $from_clause $where_clause GROUP BY $table_cols HAVING COUNT(*)>1";
 221          }
 222  
 223          $query = $select_clause . $from_clause .
 224                      " LEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=" . $this->table_name .".".$this->table_index .
 225                      " INNER JOIN (" . $sub_query . ") AS temp ON ".get_on_clause($field_values,$ui_type_arr,$module) .
 226                      $where_clause .
 227                      " ORDER BY $table_cols,". $this->table_name .".".$this->table_index ." ASC";
 228  
 229          return $query;
 230      }
 231  }


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