[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Settings/Leads/models/ -> Mapping.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 Settings_Leads_Mapping_Model extends Settings_Vtiger_Module_Model {
  12  
  13      var $name = 'Leads';
  14  
  15      /**

  16       * Function to get detail view url of this model

  17       * @return <String> url

  18       */
  19  	public function getDetailViewUrl() {
  20          return 'index.php?parent='. $this->getParentName() .'&module='. $this->getName() .'&view=MappingDetail';
  21      }
  22  
  23      /**

  24       * Function to get edit view url of this model

  25       * @return <String> url

  26       */
  27  	public function getEditViewUrl() {
  28          return 'index.php?parent='. $this->getParentName() .'&module='. $this->getName() .'&view=MappingEdit';
  29      }
  30  
  31      /**

  32       * Function to get delete url of this mapping model

  33       * @return <String> url

  34       */
  35  	public function getMappingDeleteUrl() {
  36          return 'index.php?parent='. $this->getParentName() .'&module='. $this->getName() .'&action=MappingDelete';
  37      }
  38  
  39      /**

  40       * Function to get headers for detail view

  41       * @return <Array> headers list

  42       */
  43  	public function getHeaders() {
  44          return array('Leads' => 'Leads', 'Type' => 'Type', 'Accounts' => 'Accounts', 'Contacts' => 'Contacts', 'Potentails' => 'Potentials');
  45      }
  46  
  47      /**

  48       * Function to get list of detail view link models

  49       * @return <Array> list of detail view link models <Vtiger_Link_Model>

  50       */
  51  	public function getDetailViewLinks() {
  52          return array(Vtiger_Link_Model::getInstanceFromValues(array(
  53                  'linktype' => 'DETAILVIEW',
  54                  'linklabel' => 'LBL_EDIT',
  55                  'linkurl' => 'javascript:Settings_LeadMapping_Js.triggerEdit("'. $this->getEditViewUrl() .'")',
  56                  'linkicon' => ''
  57                  )));
  58      }
  59  
  60      /**

  61       * Function to get list of mapping link models

  62       * @return <Array> list of mapping link models <Vtiger_Link_Model>

  63       */
  64  	public function getMappingLinks() {
  65          return array(Vtiger_Link_Model::getInstanceFromValues(array(
  66                  'linktype' => 'DETAILVIEW',
  67                  'linklabel' => 'LBL_DELETE',
  68                  'linkurl' => 'javascript:Settings_LeadMapping_Js.triggerDelete(event,"'. $this->getMappingDeleteUrl() .'")',
  69                  'linkicon' => ''
  70                  )));
  71      }
  72  
  73      /**

  74       * Function to get mapping details

  75       * @return <Array> list of mapping details

  76       */
  77  	public function getMapping($editable = false) {
  78          if (!$this->mapping) {
  79              $db = PearDatabase::getInstance();
  80              $query = 'SELECT * FROM vtiger_convertleadmapping';
  81              if ($editable) {
  82                  $query .= ' WHERE editable = 1';
  83              }
  84  
  85              $result = $db->pquery($query, array());
  86              $numOfRows = $db->num_rows($result);
  87              $mapping = array();
  88              for ($i=0; $i<$numOfRows; $i++) {
  89                  $rowData = $db->query_result_rowdata($result, $i);
  90                  $mapping[$rowData['cfmid']] = $rowData;
  91              }
  92  
  93              $finalMapping = $fieldIdsList = array();
  94              foreach ($mapping as $mappingDetails) {
  95                  array_push($fieldIdsList, $mappingDetails['leadfid'], $mappingDetails['accountfid'], $mappingDetails['contactfid'], $mappingDetails['potentialfid']);
  96              }
  97              $fieldLabelsList = array();
  98              if(!empty($fieldIdsList)){
  99                  $fieldLabelsList = $this->getFieldsInfo(array_unique($fieldIdsList));
 100              }
 101              foreach ($mapping as $mappingId => $mappingDetails) {
 102                  $finalMapping[$mappingId] = array(
 103                          'editable'    => $mappingDetails['editable'],
 104                          'Leads'        => $fieldLabelsList[$mappingDetails['leadfid']],
 105                          'Accounts'    => $fieldLabelsList[$mappingDetails['accountfid']],
 106                          'Contacts'    => $fieldLabelsList[$mappingDetails['contactfid']],
 107                          'Potentials'=> $fieldLabelsList[$mappingDetails['potentialfid']]
 108                  );
 109              }
 110  
 111              $this->mapping = $finalMapping;
 112          }
 113          return $this->mapping;
 114      }
 115  
 116      /**

 117       * Function to get fields info

 118       * @param <Array> list of field ids

 119       * @return <Array> list of field info

 120       */
 121  	public function getFieldsInfo($fieldIdsList) {
 122          $leadModel = Vtiger_Module_Model::getInstance($this->getName());
 123          $leadId = $leadModel->getId();
 124  
 125          $db = PearDatabase::getInstance();
 126          $result = $db->pquery('SELECT fieldid, fieldlabel, uitype, typeofdata, fieldname, tablename, tabid FROM vtiger_field WHERE fieldid IN ('. generateQuestionMarks($fieldIdsList). ')', $fieldIdsList);
 127          $numOfRows = $db->num_rows($result);
 128  
 129          $fieldLabelsList = array();
 130          for ($i=0; $i<$numOfRows; $i++) {
 131              $rowData = $db->query_result_rowdata($result, $i);
 132  
 133              $fieldInfo = array('id' => $rowData['fieldid'], 'label' => $rowData['fieldlabel']);
 134              if ($rowData['tabid'] === $leadId) {
 135                  $fieldModel = Settings_Leads_Field_Model::getCleanInstance();
 136                  $fieldModel->set('uitype', $rowData['uitype']);
 137                  $fieldModel->set('typeofdata', $rowData['typeofdata']);
 138                  $fieldModel->set('name', $rowData['fieldname']);
 139                  $fieldModel->set('table', $rowData['tablename']);
 140  
 141                  $fieldInfo['fieldDataType'] = $fieldModel->getFieldDataType();
 142              }
 143  
 144              $fieldLabelsList[$rowData['fieldid']] = $fieldInfo;
 145          }
 146          return $fieldLabelsList;
 147      }
 148  
 149      /**

 150       * Function to save the mapping info

 151       * @param <Array> $mapping info

 152       */
 153  	public function save($mapping) {
 154          $db = PearDatabase::getInstance();
 155          $deleteMappingsList = $updateMappingsList = $createMappingsList = array();
 156          foreach ($mapping as $mappingDetails) {
 157              $mappingId = $mappingDetails['mappingId'];
 158              if ($mappingDetails['lead']) {
 159                  if ($mappingId) {
 160                      if ((array_key_exists('deletable', $mappingDetails)) || (!$mappingDetails['account'] && !$mappingDetails['contact'] && !$mappingDetails['potential'])) {
 161                          $deleteMappingsList[] = $mappingId;
 162                      } else {
 163                          if ($mappingDetails['account'] || $mappingDetails['contact'] || $mappingDetails['potential']) {
 164                              $updateMappingsList[] = $mappingDetails;
 165                          }
 166                      }
 167                  } else {
 168                      if ($mappingDetails['account'] || $mappingDetails['contact'] || $mappingDetails['potential']) {
 169                          $createMappingsList[] = $mappingDetails;
 170                      }
 171                  }
 172              }
 173          }
 174  
 175          if ($deleteMappingsList) {
 176              $db->pquery('DELETE FROM vtiger_convertleadmapping WHERE editable = 1 AND cfmid IN ('. generateQuestionMarks($deleteMappingsList) .')', $deleteMappingsList);
 177          }
 178  
 179          if ($createMappingsList) {
 180              $insertQuery = 'INSERT INTO vtiger_convertleadmapping(leadfid, accountfid, contactfid, potentialfid) VALUES ';
 181  
 182              $count = count($createMappingsList);
 183              for ($i=0; $i<$count; $i++) {
 184                  $mappingDetails = $createMappingsList[$i];
 185                  $insertQuery .= '('. $mappingDetails['lead'] .', '. $mappingDetails['account'] .', '. $mappingDetails['contact'] .', '. $mappingDetails['potential'] .')';
 186                  if ($i !== $count-1) {
 187                      $insertQuery .= ', ';
 188                  }
 189              }
 190              $db->pquery($insertQuery, array());
 191          }
 192  
 193          if ($updateMappingsList) {
 194              $leadQuery        = ' SET leadfid = CASE ';
 195              $accountQuery    = ' accountfid = CASE ';
 196              $contactQuery    = ' contactfid = CASE ';
 197              $potentialQuery    = ' potentialfid = CASE ';
 198  
 199              foreach ($updateMappingsList as $mappingDetails) {
 200                  $mappingId         = $mappingDetails['mappingId'];
 201                  $leadQuery        .= " WHEN cfmid = $mappingId THEN ". $mappingDetails['lead'];
 202                  $accountQuery    .= " WHEN cfmid = $mappingId THEN ". $mappingDetails['account'];
 203                  $contactQuery    .= " WHEN cfmid = $mappingId THEN ". $mappingDetails['contact'];
 204                  $potentialQuery    .= " WHEN cfmid = $mappingId THEN ". $mappingDetails['potential'];
 205              }
 206              $leadQuery        .= ' ELSE leadfid END ';
 207              $accountQuery    .= ' ELSE accountfid END ';
 208              $contactQuery    .= ' ELSE contactfid END ';
 209              $potentialQuery .= ' ELSE potentialfid END ';
 210  
 211              $db->pquery("UPDATE vtiger_convertleadmapping $leadQuery, $accountQuery, $contactQuery, $potentialQuery WHERE editable = ?", array(1));
 212          }
 213      }
 214  
 215      /**

 216       * Function to get restricted field ids list

 217       * @return <Array> list of field ids

 218       */
 219  	public static function getRestrictedFieldIdsList() {
 220          $db = PearDatabase::getInstance();
 221          $result = $db->pquery('SELECT * FROM vtiger_convertleadmapping WHERE editable = ?', array(0));
 222          $numOfRows = $db->num_rows($result);
 223  
 224          $restrictedIdsList = array();
 225          for ($i=0; $i<$numOfRows; $i++) {
 226              $rowData = $db->query_result_rowdata($result, $i);
 227              if ($rowData['accountfid']) {
 228                  $restrictedIdsList[] = $rowData['accountfid'];
 229              }
 230              if ($rowData['contactfid']) {
 231                  $restrictedIdsList[] = $rowData['contactfid'];
 232              }
 233              if ($rowData['potentialfid']) {
 234                  $restrictedIdsList[] = $rowData['potentialfid'];
 235              }
 236          }
 237          return $restrictedIdsList;
 238      }
 239  
 240      /**

 241       * Function to get mapping supported modules list

 242       * @return <Array>

 243       */
 244  	public static function getSupportedModulesList() {
 245          return array('Accounts', 'Contacts', 'Potentials');
 246      }
 247  
 248      /**

 249       * Function to get instance

 250       * @param <Boolean> true/false

 251       * @return <Settings_Leads_Mapping_Model>

 252       */
 253  	public static function getInstance($editable = false) {
 254          $instance = new self();
 255          $instance->getMapping($editable);
 256          return $instance;
 257      }
 258  
 259      /**

 260       * Function to get instance

 261       * @return <Settings_Leads_Mapping_Model>

 262       */
 263  	public static function getCleanInstance() {
 264          return new self();
 265      }
 266  
 267      /**

 268       * Function to delate the mapping

 269       * @param <Array> $mappingIdsList

 270       */
 271  	public static function deleteMapping($mappingIdsList) {
 272          $db = PearDatabase::getInstance();
 273          $db->pquery('DELETE FROM vtiger_convertleadmapping WHERE cfmid IN ('. generateQuestionMarks($mappingIdsList). ')', $mappingIdsList);
 274      }
 275  }


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