[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/data/ -> Tracker.php (source)

   1  <?php
   2  /*********************************************************************************
   3   * The contents of this file are subject to the SugarCRM Public License Version 1.1.2
   4   * ("License"); You may not use this file except in compliance with the
   5   * License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
   6   * Software distributed under the License is distributed on an  "AS IS"  basis,
   7   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
   8   * the specific language governing rights and limitations under the License.
   9   * The Original Code is:  SugarCRM Open Source
  10   * The Initial Developer of the Original Code is SugarCRM, Inc.
  11   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.;
  12   * All Rights Reserved.
  13   * Contributor(s): ______________________________________.
  14   ********************************************************************************/
  15  /*********************************************************************************
  16   * $Header: /advent/projects/wesat/vtiger_crm/sugarcrm/data/Tracker.php,v 1.15 2005/04/28 05:44:22 samk Exp $
  17   * Description:  Updates entries for the Last Viewed functionality tracking the
  18   * last viewed records on a per user basis.
  19   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
  20   * All Rights Reserved.
  21   * Contributor(s): ______________________________________..
  22   ********************************************************************************/
  23  
  24  include_once ('config.php');
  25  require_once ('include/logging.php');
  26  require_once ('include/database/PearDatabase.php');
  27  
  28  /** This class is used to track the recently viewed items on a per user basis.
  29   * It is intended to be called by each module when rendering the detail form.
  30   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
  31   * All Rights Reserved.
  32   * Contributor(s): ______________________________________..
  33  */
  34  class Tracker {
  35      var $log;
  36      var $db;
  37      var $table_name = "vtiger_tracker";
  38  
  39      // Tracker vtiger_table
  40      var $column_fields = Array(
  41          "id",
  42          "user_id",
  43          "module_name",
  44          "item_id",
  45          "item_summary"
  46      );
  47  
  48      function Tracker()
  49      {
  50          $this->log = LoggerManager::getLogger('Tracker');
  51      // $this->db = PearDatabase::getInstance();
  52      global $adb;
  53          $this->db = $adb;
  54      }
  55  
  56      /**
  57       * Add this new item to the vtiger_tracker vtiger_table.  If there are too many items (global config for now)
  58       * then remove the oldest item.  If there is more than one extra item, log an error.
  59       * If the new item is the same as the most recent item then do not change the list
  60   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
  61   * All Rights Reserved.
  62   * Contributor(s): ______________________________________..
  63       */
  64      function track_view($user_id, $current_module, $item_id, $item_summary)
  65      {
  66        global $adb;
  67        $this->delete_history($user_id, $item_id);
  68        global $log;
  69  $log->info("in  track view method ".$current_module);
  70          
  71  //No genius required. Just add an if case and change the query so that it puts the tracker entry whenever you touch on the DetailView of the required entity
  72           //get the first name and last name from the respective modules
  73       if($current_module != '')
  74       {
  75           $query = "select fieldname,tablename,entityidfield from vtiger_entityname where modulename = ?";
  76           $result = $adb->pquery($query, array($current_module));
  77           $fieldsname = $adb->query_result($result,0,'fieldname');
  78           $tablename = $adb->query_result($result,0,'tablename'); 
  79           $entityidfield = $adb->query_result($result,0,'entityidfield'); 
  80           if(!(strpos($fieldsname,',') === false))
  81           {
  82               // concatenate multiple fields with an whitespace between them
  83               $fieldlists = explode(',',$fieldsname);
  84               $fl = array();
  85               foreach($fieldlists as $w => $c)
  86               {
  87                   if (count($fl))
  88                       $fl[] = "' '";
  89                   $fl[] = $c;
  90               }
  91               $fieldsname = $adb->sql_concat($fl);
  92           }    
  93           $query1 = "select $fieldsname as entityname from $tablename where $entityidfield = ?"; 
  94           $result = $adb->pquery($query1, array($item_id));
  95           $item_summary = $adb->query_result($result,0,'entityname');
  96           if(strlen($item_summary) > 30)
  97           {
  98              $item_summary=substr($item_summary,0,30).'...';
  99           }
 100       }
 101       
 102       #if condition added to skip vtiger_faq in last viewed history
 103            $query = "INSERT into $this->table_name (user_id, module_name, item_id, item_summary) values (?,?,?,?)";
 104            $qparams = array($user_id, $current_module, $item_id, $item_summary);
 105            
 106            $this->log->info("Track Item View: ".$query);
 107            
 108            $this->db->pquery($query, $qparams, true);
 109            
 110            
 111            $this->prune_history($user_id);
 112      }
 113  
 114      /**
 115       * param $user_id - The id of the user to retrive the history for
 116       * param $module_name - Filter the history to only return records from the specified module.  If not specified all records are returned
 117       * return - return the array of result set rows from the query.  All of the vtiger_table vtiger_fields are included
 118   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 119   * All Rights Reserved.
 120   * Contributor(s): ______________________________________..
 121       */
 122      function get_recently_viewed($user_id, $module_name = "")
 123      {
 124          if (empty($user_id)) {
 125              return;
 126          }
 127  
 128  //        $query = "SELECT * from $this->table_name WHERE user_id='$user_id' ORDER BY id DESC";
 129      $query = "SELECT * from $this->table_name inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_tracker.item_id WHERE user_id=? and vtiger_crmentity.deleted=0 ORDER BY id DESC";
 130          $this->log->debug("About to retrieve list: $query");
 131          $result = $this->db->pquery($query, array($user_id), true);
 132          $list = Array();
 133          while($row = $this->db->fetchByAssoc($result, -1, false))
 134          {
 135          //echo "while loppp";
 136          //echo '<BR>';
 137  
 138  
 139              // If the module was not specified or the module matches the module of the row, add the row to the list
 140              if($module_name == "" || $row[module_name] == $module_name)
 141              {
 142          //Adding Security check
 143          require_once ('include/utils/utils.php');
 144          require_once ('include/utils/UserInfoUtil.php');
 145          $entity_id = $row['item_id'];
 146          $module = $row['module_name'];
 147          //echo "module is ".$module."  id is      ".$entity_id;
 148          //echo '<BR>';
 149          if($module == "Users")
 150          {
 151              global $current_user;
 152              if(is_admin($current_user))
 153              {
 154                  $per = 'yes';
 155              }    
 156          }
 157          else
 158          {
 159              
 160              $per = isPermitted($module,'DetailView',$entity_id);
 161              
 162          }
 163          if($per == 'yes')
 164          {
 165                      $list[] = $row;
 166          }
 167              }
 168          }
 169          return $list;
 170      }
 171  
 172  
 173  
 174      /**
 175       * INTERNAL -- This method cleans out any entry for a record for a user.
 176       * It is used to remove old occurances of previously viewed items.
 177   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 178   * All Rights Reserved.
 179   * Contributor(s): ______________________________________..
 180       */
 181      function delete_history( $user_id, $item_id)
 182      {
 183          $query = "DELETE from $this->table_name WHERE user_id=? and item_id=?";
 184             $this->db->pquery($query, array($user_id, $item_id), true);
 185      }
 186  
 187      /**
 188       * INTERNAL -- This method cleans out any entry for a record.
 189   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 190   * All Rights Reserved.
 191   * Contributor(s): ______________________________________..
 192       */
 193      function delete_item_history($item_id)
 194      {
 195          $query = "DELETE from $this->table_name WHERE item_id=?";
 196         $this->db->pquery($query, array($item_id), true);
 197  
 198      }
 199  
 200      /**
 201       * INTERNAL -- This function will clean out old history records for this user if necessary.
 202   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 203   * All Rights Reserved.
 204   * Contributor(s): ______________________________________..
 205       */
 206      function prune_history($user_id)
 207      {
 208          global $history_max_viewed;
 209  
 210          // Check to see if the number of items in the list is now greater than the config max.
 211          $query = "SELECT count(*) from $this->table_name WHERE user_id='$user_id'";
 212  
 213          $this->log->debug("About to verify history size: $query");
 214  
 215          $count = $this->db->getOne($query);
 216  
 217  
 218          $this->log->debug("history size: (current, max)($count, $history_max_viewed)");
 219          while($count > $history_max_viewed)
 220          {
 221              // delete the last one.  This assumes that entries are added one at a time.
 222              // we should never add a bunch of entries
 223              $query = "SELECT * from $this->table_name WHERE user_id='$user_id' ORDER BY id ASC";
 224              $this->log->debug("About to try and find oldest item: $query");
 225              $result =  $this->db->limitQuery($query,0,1);
 226  
 227              $oldest_item = $this->db->fetchByAssoc($result, -1, false);
 228              $query = "DELETE from $this->table_name WHERE id=?";
 229              $this->log->debug("About to delete oldest item: ");
 230  
 231              $result = $this->db->pquery($query, array($oldest_item['id']), true);
 232  
 233  
 234              $count--;
 235          }
 236      }
 237  
 238  }
 239  ?>


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