[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/include/Webservices/ -> History.php (source)

   1  <?php
   2  
   3  /*+********************************************************************************
   4   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   5   * ("License"); You may not use this file except in compliance with the License
   6   * The Original Code is:  vtiger CRM Open Source
   7   * The Initial Developer of the Original Code is vtiger.
   8   * Portions created by vtiger are Copyright (C) vtiger.
   9   * All Rights Reserved.
  10   *********************************************************************************/
  11  
  12  function vtws_history($element, $user) {
  13      $MAXLIMIT = 100;
  14  
  15      $adb = PearDatabase::getInstance();
  16  
  17      // Mandatory input validation
  18      if (empty($element['module']) && empty($element['record'])) {
  19          throw new WebServiceException(WebServiceErrorCode::$MANDFIELDSMISSING, "Missing mandatory input values.");
  20      }
  21  
  22      if (!CRMEntity::getInstance('ModTracker') || !vtlib_isModuleActive('ModTracker')) {
  23          throw new WebServiceException("TRACKING_MODULE_NOT_ACTIVE", "Tracking module not active.");
  24      }
  25  
  26      $idComponents = NULL;
  27  
  28      $moduleName = $element['module'];
  29      $record = $element['record'];
  30      $mode = empty($element['mode'])? 'Private' : $element['mode']; // Private or All
  31      $page = empty($element['page'])? 0 : intval($element['page']); // Page to start
  32  
  33      $acrossAllModule = false;
  34      if ($moduleName == 'Home') $acrossAllModule = true;
  35  
  36      // Pre-condition check
  37      if (empty($moduleName)) {
  38          $moduleName = Mobile_WS_Utils::detectModulenameFromRecordId($record);
  39          $idComponents = vtws_getIdComponents($record); // We have it - as the input is validated.
  40      }
  41  
  42      if (!$acrossAllModule && !ModTracker::isTrackingEnabledForModule($moduleName)) {
  43          throw new WebServiceException("Module_NOT_TRACKED", "Module not tracked for changes.");
  44      }
  45  
  46      // Per-condition has been met, perform the operation
  47      $sql = '';
  48      $params = array();
  49  
  50      // REFER: modules/ModTracker/ModTracker.php
  51  
  52      // Two split phases for data extraction - so we can apply limit of retrieveal at record level.
  53      $sql = 'SELECT vtiger_modtracker_basic.* FROM vtiger_modtracker_basic
  54          INNER JOIN vtiger_crmentity ON vtiger_modtracker_basic.crmid = vtiger_crmentity.crmid
  55          AND vtiger_crmentity.deleted = 0';
  56  
  57      if ($mode == 'Private') {
  58          $sql .= ' WHERE vtiger_modtracker_basic.whodid = ?';
  59          $params[] = $user->getId();
  60      } else if ($mode == 'All') {
  61          if ($acrossAllModule) {
  62              // TODO collate only active (or enabled) modules for tracking.
  63          } else if($moduleName) {
  64              $sql .= ' WHERE vtiger_modtracker_basic.module = ?';
  65              $params[] = $moduleName;
  66          } else {
  67              $sql .= ' WHERE vtiger_modtracker_basic.crmid = ?';
  68              $params[] = $idComponents[1];
  69          }
  70      }
  71  
  72      // Get most recently tracked changes with limit
  73      $start = $page*$MAXLIMIT; if ($start > 0) $start = $start + 1; // Adjust the start range
  74      $sql .= sprintf(' ORDER BY vtiger_modtracker_basic.id DESC LIMIT %s,%s', $start, $MAXLIMIT);
  75  
  76      $result = $adb->pquery($sql, $params);
  77  
  78      $recordValuesMap = array();
  79      $orderedIds = array();
  80  
  81      while ($row = $adb->fetch_array($result)) {
  82          $orderedIds[] = $row['id'];
  83  
  84          $whodid = vtws_history_entityIdHelper('Users', $row['whodid']);
  85          $crmid = vtws_history_entityIdHelper($acrossAllModule? '' : $moduleName, $row['crmid']);
  86          $status = $row['status'];
  87          $statuslabel = '';
  88          switch ($status) {
  89              case ModTracker::$UPDATED: $statuslabel = 'updated'; break;
  90              case ModTracker::$DELETED: $statuslabel = 'deleted'; break;
  91              case ModTracker::$CREATED: $statuslabel = 'created'; break;
  92              case ModTracker::$RESTORED: $statuslabel = 'restored'; break;
  93              case ModTracker::$LINK: $statuslabel = 'link'; break;
  94              case ModTracker::$UNLINK: $statuslabel = 'unlink'; break;
  95          }
  96          $item['modifieduser'] = $whodid;
  97          $item['id'] = $crmid;
  98          $item['modifiedtime'] = $row['changedon'];
  99          $item['status'] = $status;
 100          $item['statuslabel'] = $statuslabel;
 101          $item['values'] = array();
 102  
 103          $recordValuesMap[$row['id']] = $item;
 104      }
 105  
 106      $historyItems = array();
 107  
 108      // Minor optimizatin to avoid 2nd query run when there is nothing to expect.
 109      if (!empty($orderedIds)) {
 110          $sql = 'SELECT vtiger_modtracker_detail.* FROM vtiger_modtracker_detail';
 111          $sql .= ' WHERE vtiger_modtracker_detail.id IN (' . generateQuestionMarks($orderedIds) . ')';
 112  
 113          // LIMIT here is not required as $ids extracted is with limit at record level earlier.
 114          $params = $orderedIds;
 115  
 116          $result = $adb->pquery($sql, $params);
 117          while ($row = $adb->fetch_array($result)) {
 118              $item = $recordValuesMap[$row['id']];
 119  
 120              // NOTE: For reference field values transform them to webservice id.
 121              $item['values'][$row['fieldname']] = array(
 122                  'previous' => $row['prevalue'],
 123                  'current'  => $row['postvalue']
 124              );
 125              $recordValuesMap[$row['id']] = $item;
 126          }
 127  
 128          // Group the values per basic-transaction
 129          foreach ($orderedIds as $id) {
 130              $historyItems[] = $recordValuesMap[$id];
 131          }
 132      }
 133  
 134      return $historyItems;
 135  }
 136  
 137  // vtws_getWebserviceEntityId - seem to be missing the optimization
 138  // which could pose performance challenge while gathering the changes made
 139  // this helper function targets to cache and optimize the transformed values.
 140  function vtws_history_entityIdHelper($moduleName, $id) {
 141      static $wsEntityIdCache = NULL;
 142      if ($wsEntityIdCache === NULL) {
 143          $wsEntityIdCache = array('users' => array(), 'records' => array());
 144      }
 145  
 146      if (!isset($wsEntityIdCache[$moduleName][$id])) {
 147          // Determine moduleName based on $id
 148          if (empty($moduleName)) {
 149              $moduleName = getSalesEntityType($id);
 150          }
 151          if($moduleName == 'Calendar') {
 152              $moduleName = vtws_getCalendarEntityType($id);
 153          }
 154  
 155          $wsEntityIdCache[$moduleName][$id] = vtws_getWebserviceEntityId($moduleName, $id);
 156      }
 157      return $wsEntityIdCache[$moduleName][$id];
 158  }


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