[ Index ]
 

Code source de vtiger CRM 5.0.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/data/ -> SugarBean.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/SugarBean.php,v 1.70 2005/03/16 10:25:16 shaw Exp $
  17   * Description:  Defines the base class for all data entities used throughout the 
  18   * application.  The base class including its methods and variables is designed to 
  19   * be overloaded with module-specific methods and variables particular to the 
  20   * module's base entity class. 
  21   ********************************************************************************/
  22  
  23  include_once ('config.php');
  24  require_once ('include/logging.php');
  25  require_once ('data/Tracker.php');
  26  require_once ('include/utils/utils.php');
  27  require_once ('include/utils/UserInfoUtil.php');
  28  require_once ('include/database/PearDatabase.php');
  29  
  30  class SugarBean
  31  {
  32      /**
  33       * This method implements a generic insert and update logic for any SugarBean
  34       * This method only works for subclasses that implement the same variable names.
  35       * This method uses the presence of an id vtiger_field that is not null to signify and update.
  36       * The id vtiger_field should not be set otherwise.
  37       * todo - Add support for vtiger_field type validation and encoding of parameters.
  38   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
  39   * All Rights Reserved.
  40   * Contributor(s): ______________________________________..
  41       */
  42      
  43  
  44  	function save($module_name= '') 
  45      {
  46           global $adb; 
  47            global $current_user;
  48          $isUpdate = true;
  49  
  50          if(!isset($this->id) || $this->id == "")
  51          {
  52              $isUpdate = false;
  53          }
  54  
  55          if ( $this->new_with_id == true )
  56          {
  57              $isUpdate = false;
  58          }
  59  
  60          //$this->date_modified = $this->db->formatDate(date('YmdHis'));
  61          $this->date_modified = date('YmdHis');
  62          if (isset($current_user)) $this->modified_user_id = $current_user->id;
  63          
  64          if($isUpdate)
  65          {
  66                  $query = "Update ".$this->table_name." set ";
  67          }
  68          else
  69          {
  70                  //$this->date_entered = $this->db->formatDate(date('YmdHis'));
  71              $this->date_entered = date('YmdHis');
  72  
  73              if($this->new_schema && 
  74                  $this->new_with_id == false)
  75              {
  76                            $this->id = $this->db->getUniqueID("vtiger_users");
  77              }
  78                          
  79              $query = "INSERT into ".$this->table_name;
  80          }
  81          // todo - add date modified to the list.
  82  
  83          // write out the SQL statement.
  84          //$query .= $this->table_name." set ";
  85  
  86          $firstPass = 0;
  87          $insKeys = '(';
  88          $insValues = '(';
  89          $updKeyValues='';
  90          foreach($this->column_fields as $field)
  91          {
  92              // Do not write out the id vtiger_field on the update statement.
  93              // We are not allowed to change ids.
  94              if($isUpdate && ('id' == $field))
  95                  continue;
  96  
  97              // Only assign variables that have been set.
  98              if(isset($this->$field))
  99              {                
 100                  // Try comparing this element with the head element.
 101                  if(0 == $firstPass)
 102                  {
 103                      $firstPass = 1;
 104                  }
 105                  else
 106                  {
 107                      if($isUpdate)
 108                      {
 109                          $updKeyValues = $updKeyValues.", ";
 110                      }
 111                      else
 112                      {
 113                          $insKeys = $insKeys.", ";
 114                          $insValues = $insValues.", ";
 115                      }
 116                  }
 117                  /*else
 118                      $query = $query.", ";
 119      
 120                  $query = $query.$field."='".$adb->quote(from_html($this->$field,$isUpdate))."'";
 121                  */
 122                  if($isUpdate)
 123                  {
 124                      $updKeyValues = $updKeyValues.$field."=".$this->db->formatString($this->table_name,$field,from_html($this->$field,$isUpdate));
 125                  }
 126                  else
 127                  {
 128                      $insKeys = $insKeys.$field;
 129                      $insValues = $insValues.$this->db->formatString($this->table_name,$field,from_html($this->$field,$isUpdate));
 130                  }
 131              }
 132          }
 133  
 134          if($isUpdate)
 135          {
 136              $query = $query.$updKeyValues." WHERE ID = '$this->id'";
 137              $this->log->info("Update $this->object_name: ".$query);
 138          }
 139          else
 140          {
 141              $query = $query.$insKeys.") VALUES ".$insValues.")";
 142                  $this->log->info("Insert: ".$query);
 143          }
 144  
 145          $this->db->query($query, true);
 146  
 147          // If this is not an update then store the id for later.
 148          if(!$isUpdate && !$this->new_schema && !$this->new_with_id)
 149          {
 150              $this->db->println("Illegal Access - SugarBean");
 151              //this is mysql specific
 152                  $this->id = $this->db->getOne("SELECT LAST_INSERT_ID()" );
 153          }
 154              
 155          return $this->id;
 156      }
 157  
 158      
 159      /**
 160       * This function retrieves a record of the appropriate type from the DB.
 161       * It fills in all of the vtiger_fields from the DB into the object it was called on.
 162       * param $id - If ID is specified, it overrides the current value of $this->id.  If not specified the current value of $this->id will be used.
 163       * returns this - The object that it was called apon or null if exactly 1 record was not found.
 164   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 165   * All Rights Reserved.
 166   * Contributor(s): ______________________________________..
 167       function retrieve($id = -1, $encodeThis=true) {
 168          if ($id == -1) {
 169              $id = $this->id;
 170          }
 171          if($id == '') {
 172              return null;
 173          }
 174  // GS porting vtiger_crmentity
 175  $query = "SELECT * FROM $this->table_name WHERE $this->module_id = '$id'";
 176  //        $query = "SELECT * FROM $this->table_name WHERE ID = '$id'";
 177          $this->log->debug("Retrieve $this->object_name: ".$query);
 178  
 179          $result =& $this->db->requireSingleResult($query, true, "Retrieving record by id $this->table_name:$id found ");
 180  
 181          if(empty($result))
 182          {
 183              return null;
 184          }
 185                  
 186          $row = $this->db->fetchByAssoc($result, -1, $encodeThis);
 187  
 188          foreach($this->column_fields as $field)
 189          {
 190              if(isset($row[$field]))
 191              {
 192                  $this->$field = $row[$field];
 193              }
 194          }
 195          return $this;
 196      }
 197       */
 198  
 199  	function get_list($order_by = "", $where = "", $row_offset = 0, $limit=-1, $max=-1) {
 200          $this->log->debug("get_list:  order_by = '$order_by' and where = '$where' and limit = '$limit'");
 201          
 202          $query = $this->create_list_query($order_by, $where);
 203          
 204          return $this->process_list_query($query, $row_offset, $limit, $max);
 205      }
 206  
 207      /**
 208       * This function returns a full (ie non-paged) list of the current object type.  
 209       * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
 210       * All Rights Reserved..
 211       * Contributor(s): ______________________________________..
 212       */
 213  	function get_full_list($order_by = "", $where = "") {
 214          $this->log->debug("get_full_list:  order_by = '$order_by' and where = '$where'");
 215          $query = "SELECT * FROM $this->table_name ";
 216          
 217          if($where != "")
 218              $query .= "where ($where) AND deleted=0";
 219          else
 220              $query .= "where deleted=0";
 221  
 222          if(!empty($order_by))
 223              $query .= " ORDER BY $order_by";
 224  
 225          $result =& $this->db->query($query, false);
 226  
 227          if($this->db->getRowCount($result) > 0){
 228          
 229              // We have some data.
 230              while ($row = $this->db->fetchByAssoc($result)) {
 231                  foreach($this->list_fields as $field)
 232                  {
 233                      if (isset($row[$field])) {
 234                          $this->$field = $row[$field];
 235                          
 236                          $this->log->debug("process_full_list: $this->object_name({$row['id']}): ".$field." = ".$this->$field);
 237                      }
 238                      else {
 239                                                       $this->$field = '';   
 240                      }
 241                  }
 242  
 243  
 244                  $list[] = clone($this);         //added clone tosupport PHP5
 245              }
 246          }
 247  
 248          if (isset($list)) return $list;
 249          else return null;
 250  
 251      }
 252  
 253  	function create_list_query($order_by, $where)
 254      {
 255          $query = "SELECT * FROM $this->table_name ";
 256          
 257          if($where != "")
 258              $query .= "where ($where) AND deleted=0";
 259          else
 260              $query .= "where deleted=0";
 261  
 262          if(!empty($order_by))
 263              $query .= " ORDER BY $order_by";
 264  
 265          return $query;
 266      }
 267      
 268  
 269  	function process_list_query($query, $row_offset, $limit= -1, $max_per_page = -1)
 270      {
 271          global $list_max_entries_per_page;
 272          $this->log->debug("process_list_query: ".$query);
 273          if(!empty($limit) && $limit != -1){
 274              $result =& $this->db->limitQuery($query, $row_offset + 0, $limit,true,"Error retrieving $this->object_name list: ");
 275          }else{
 276              $result =& $this->db->query($query,true,"Error retrieving $this->object_name list: ");
 277          }
 278  
 279          $list = Array();
 280          if($max_per_page == -1){
 281              $max_per_page     = $list_max_entries_per_page;
 282          }
 283          $rows_found =  $this->db->getRowCount($result);
 284  
 285          $this->log->debug("Found $rows_found ".$this->object_name."s");
 286                  
 287          $previous_offset = $row_offset - $max_per_page;
 288          $next_offset = $row_offset + $max_per_page;
 289  
 290          if($rows_found != 0)
 291          {
 292  
 293              // We have some data.
 294  
 295              for($index = $row_offset , $row = $this->db->fetchByAssoc($result, $index); $row && ($index < $row_offset + $max_per_page || $max_per_page == -99) ;$index++, $row = $this->db->fetchByAssoc($result, $index)){
 296                  foreach($this->list_fields as $field)
 297                  {
 298                      if (isset($row[$field])) {
 299                          $this->$field = $row[$field];
 300                          
 301                          
 302                          $this->log->debug("$this->object_name({$row['id']}): ".$field." = ".$this->$field);
 303                      }
 304                      else 
 305                      {
 306                          $this->$field = "";
 307                      }
 308                  }
 309  
 310  
 311                  $list[] = clone($this);   //added clone to support PHP5
 312              }
 313          }
 314  
 315          $response = Array();
 316          $response['list'] = $list;
 317          $response['row_count'] = $rows_found;
 318          $response['next_offset'] = $next_offset;
 319          $response['previous_offset'] = $previous_offset;
 320  
 321          return $response;
 322      }
 323  
 324      
 325      /**
 326       * Track the viewing of a detail record.  This leverages get_summary_text() which is object specific
 327       * params $user_id - The user that is viewing the record.
 328       * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
 329       * All Rights Reserved..
 330       * Contributor(s): ______________________________________..
 331       */
 332  	function track_view($user_id, $current_module,$id='')
 333      {
 334          $this->log->debug("About to call vtiger_tracker (user_id, module_name, item_id)($user_id, $current_module, $this->id)");
 335  
 336          $tracker = new Tracker();
 337          $tracker->track_view($user_id, $current_module, $id, '');
 338      }
 339  
 340  
 341      /** This function should be overridden in each module.  It marks an item as deleted.
 342      * If it is not overridden, then marking this type of item is not allowed
 343       * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
 344       * All Rights Reserved..
 345       * Contributor(s): ______________________________________..
 346      */
 347  	function mark_deleted($id)
 348      {
 349          $query = "UPDATE $this->table_name set deleted=1 where id='$id'";
 350          $this->db->query($query, true,"Error marking record deleted: ");
 351  
 352          $this->mark_relationships_deleted($id);
 353  
 354          // Take the item off of the recently viewed lists.
 355          $tracker = new Tracker();
 356          $tracker->delete_item_history($id);
 357  
 358      }
 359  
 360  
 361      /* This is to allow subclasses to fill in row specific columns of a list view form 
 362      function list_view_parse_additional_sections(&$list_form)
 363      {
 364      }
 365      */
 366      /* This function assigns all of the values into the template for the list view 
 367      function get_list_view_array(){
 368          $return_array = Array();
 369          
 370          foreach($this->list_fields as $field)
 371          {
 372              $return_array[strtoupper($field)] = $this->$field;
 373          }
 374          
 375          return $return_array;    
 376      }
 377      function get_list_view_data()
 378      {
 379          
 380          return $this->get_list_view_array();
 381      }
 382  
 383      function get_where(&$fields_array)
 384      { 
 385          $where_clause = "WHERE "; 
 386          $first = 1; 
 387          foreach ($fields_array as $name=>$value) 
 388          { 
 389              if ($first) 
 390              { 
 391                  $first = 0;
 392              } 
 393              else 
 394              { 
 395                  $where_clause .= " AND ";
 396              } 
 397  
 398              $where_clause .= "$name = ".$adb->quote($value)."";
 399          } 
 400  
 401          $where_clause .= " AND deleted=0";
 402          return $where_clause;
 403      }
 404  
 405  
 406      function retrieve_by_string_fields($fields_array, $encode=true) 
 407      { 
 408          $where_clause = $this->get_where($fields_array);
 409          
 410          $query = "SELECT * FROM $this->table_name $where_clause";
 411          $this->log->debug("Retrieve $this->object_name: ".$query);
 412          $result =& $this->db->requireSingleResult($query, true, "Retrieving record $where_clause:");
 413          if( empty($result)) 
 414          { 
 415               return null; 
 416          } 
 417  
 418           $row = $this->db->fetchByAssoc($result,-1, $encode);
 419  
 420          foreach($this->column_fields as $field) 
 421          { 
 422              if(isset($row[$field])) 
 423              { 
 424                  $this->$field = $row[$field];
 425              }
 426          } 
 427          return $this;
 428      }
 429  
 430      // this method is called during an import before inserting a bean
 431      // define an associative array called $special_fields
 432      // the keys are user defined, and don't directly map to the bean's vtiger_fields
 433      // the value is the method name within that bean that will do extra
 434      // processing for that vtiger_field. example: 'full_name'=>'get_names_from_full_name'
 435  
 436      function process_special_fields() 
 437      { 
 438          foreach ($this->special_functions as $func_name) 
 439          { 
 440              if ( method_exists($this,$func_name) ) 
 441              { 
 442                  $this->$func_name(); 
 443              } 
 444          } 
 445      }
 446  
 447      */
 448  }
 449  
 450  
 451  ?>


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7