[ Index ]
 

Code source de vtiger CRM 5.0.2

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

title

Body

[fermer]

/data/ -> CRMEntity.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/vtigercrm/data/CRMEntity.php,v 1.16 2005/04/29 04:21:31 mickie 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  
  29  class CRMEntity
  30  {
  31    /**
  32     * This method implements a generic insert and update logic for any SugarBean
  33     * This method only works for subclasses that implement the same variable names.
  34     * This method uses the presence of an id vtiger_field that is not null to signify and update.
  35     * The id vtiger_field should not be set otherwise.
  36     * todo - Add support for vtiger_field type validation and encoding of parameters.
  37     * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
  38     * All Rights Reserved.
  39     * Contributor(s): ______________________________________..
  40     */
  41  
  42      
  43    function saveentity($module)
  44    {
  45      global $current_user, $adb;//$adb added by raju for mass mailing
  46      $insertion_mode = $this->mode;
  47  
  48      $this->db->println("TRANS saveentity starts $module");
  49      $this->db->startTransaction();
  50      
  51  
  52      foreach($this->tab_name as $table_name)
  53      {
  54              
  55          if($table_name == "vtiger_crmentity")
  56          {
  57              $this->insertIntoCrmEntity($module);
  58          }
  59          else
  60          {
  61              $this->insertIntoEntityTable($table_name, $module);            
  62          }
  63      }
  64  
  65      //Calling the Module specific save code
  66      $this->save_module($module);
  67      
  68      $this->db->completeTransaction();
  69          $this->db->println("TRANS saveentity ends");
  70    }
  71  
  72  
  73      
  74  	function insertIntoAttachment1($id,$module,$filedata,$filename,$filesize,$filetype,$user_id)
  75      {
  76          $date_var = date('YmdHis');
  77          global $current_user;
  78          global $adb;
  79          //global $root_directory;
  80          global $log;
  81  
  82          $ownerid = $user_id;
  83          
  84          if($filesize != 0)
  85          {
  86              $data = base64_encode(fread(fopen($filedata, "r"), $filesize));
  87          }
  88          
  89          $current_id = $adb->getUniqueID("vtiger_crmentity");
  90  
  91          if($module=='Emails') 
  92          { 
  93              $log->info("module is ".$module);
  94              $idname='emailid';      $tablename='emails';    $descname='description';
  95          }
  96          else     
  97          { 
  98              $idname='notesid';      $tablename='notes';     $descname='notecontent';
  99          }
 100  
 101          $sql="update ".$tablename." set filename='".$filename."' where ".$idname."=".$id;
 102          $adb->query($sql);
 103  
 104          $sql1 = "insert into vtiger_crmentity (crmid,smcreatorid,smownerid,setype,description,createdtime,modifiedtime) values(".$current_id.",".$current_user->id.",".$ownerid.",'".$module." Attachment','"."',".$adb->formatString("vtiger_crmentity","createdtime",$date_var).",".$adb->formatString("vtiger_crmentity","modifiedtime",$date_var).")";
 105          $adb->query($sql1);
 106  
 107          $sql2="insert into vtiger_attachments(attachmentsid, name, description, type) values(".$current_id.",'".$filename."','"."','".$filetype."')";
 108          $result=$adb->query($sql2);
 109  
 110          //TODO -- instead of put contents in db now we should store the file in harddisk
 111  
 112          $sql3='insert into vtiger_seattachmentsrel values('.$id.','.$current_id.')';
 113          $adb->query($sql3);
 114      }
 115      
 116  
 117  
 118      /**
 119       *      This function is used to upload the attachment in the server and save that attachment information in db.
 120       *      @param int $id  - entity id to which the file to be uploaded
 121       *      @param string $module  - the current module name
 122       *      @param array $file_details  - array which contains the file information(name, type, size, tmp_name and error)
 123       *      return void
 124      */
 125  	function uploadAndSaveFile($id,$module,$file_details)
 126      {
 127          global $log;
 128          $log->debug("Entering into uploadAndSaveFile($id,$module,$file_details) method.");
 129  
 130          global $adb, $current_user;
 131          global $upload_badext;
 132  
 133          $date_var = date('YmdHis');
 134  
 135          //to get the owner id
 136          $ownerid = $this->column_fields['assigned_user_id'];
 137          if(!isset($ownerid) || $ownerid=='')
 138              $ownerid = $current_user->id;
 139  
 140      
 141          // Arbitrary File Upload Vulnerability fix - Philip
 142          $binFile = $file_details['name'];
 143          $ext_pos = strrpos($binFile, ".");
 144  
 145          $ext = substr($binFile, $ext_pos + 1);
 146  
 147          if (in_array($ext, $upload_badext))
 148          {
 149              $binFile .= ".txt";
 150          }
 151          // Vulnerability fix ends
 152  
 153          $current_id = $adb->getUniqueID("vtiger_crmentity");
 154  
 155          $filename = basename($binFile);
 156          $filetype= $file_details['type'];
 157          $filesize = $file_details['size'];
 158          $filetmp_name = $file_details['tmp_name'];
 159  
 160          //get the file path inwhich folder we want to upload the file
 161          $upload_file_path = decideFilePath();
 162  
 163          //upload the file in server
 164          $upload_status = move_uploaded_file($filetmp_name,$upload_file_path.$current_id."_".$binFile);
 165  
 166          $save_file = 'true';
 167          //only images are allowed for these modules
 168          if($module == 'Contacts' || $module == 'Products')
 169          {
 170              $save_file = validateImageFile(&$file_details);
 171          }
 172  
 173          if($save_file == 'true' && $upload_status == 'true')
 174          {
 175              //This is only to update the attached filename in the vtiger_notes vtiger_table for the Notes module
 176              if($module=='Notes')
 177              {
 178                  $sql="update vtiger_notes set filename='".$filename."' where notesid = ".$id;
 179                  $adb->query($sql);
 180              }
 181  
 182              $sql1 = "insert into vtiger_crmentity (crmid,smcreatorid,smownerid,setype,description,createdtime,modifiedtime) values(".$current_id.",".$current_user->id.",".$ownerid.",'".$module." Attachment','".$this->column_fields['description']."',".$adb->formatString("vtiger_crmentity","createdtime",$date_var).",".$adb->formatString("vtiger_crmentity","modifiedtime",$date_var).")";
 183              $adb->query($sql1);
 184  
 185              $sql2="insert into vtiger_attachments(attachmentsid, name, description, type, path) values(".$current_id.",'".$filename."','".$this->column_fields['description']."','".$filetype."','".$upload_file_path."')";
 186              $result=$adb->query($sql2);
 187  
 188              if($_REQUEST['mode'] == 'edit')
 189              {
 190                  if($id != '' && $_REQUEST['fileid'] != '')
 191                  {
 192                      $delquery = 'delete from vtiger_seattachmentsrel where crmid = '.$id.' and attachmentsid = '.$_REQUEST['fileid'];
 193                      $adb->query($delquery);
 194                  }
 195              }
 196              if($module == 'Notes')
 197              {
 198                  $query = "delete from vtiger_seattachmentsrel where crmid = ".$id;
 199                  $adb->query($query);
 200              }
 201              $sql3='insert into vtiger_seattachmentsrel values('.$id.','.$current_id.')';
 202              $adb->query($sql3);
 203  
 204              return true;
 205          }
 206          else
 207          {
 208              $log->debug("Skip the save attachment process.");
 209              return false;
 210          }
 211      }
 212  
 213      /** Function to insert values in the vtiger_crmentity for the specified module
 214          * @param $module -- module:: Type varchar
 215        */    
 216  
 217    function insertIntoCrmEntity($module)
 218    {
 219      global $adb;
 220      global $current_user;
 221      global $log;
 222                  
 223      $date_var = date('YmdHis');
 224      if($_REQUEST['assigntype'] == 'T')
 225      {
 226          $ownerid= 0;
 227      }
 228      else
 229      {
 230          $ownerid = $this->column_fields['assigned_user_id'];
 231      }
 232                  
 233      if($module == 'Products' || $module == 'Notes' || $module =='Faq' || $module == 'Vendors' || $module == 'PriceBooks')
 234      {
 235          $log->info("module is =".$module);
 236          $ownerid = $current_user->id;
 237      }
 238      if($module == 'Events')
 239      {
 240          $module = 'Calendar';
 241      }
 242      if($this->mode == 'edit')
 243      {
 244          $description_val = from_html($adb->formatString("vtiger_crmentity","description",$this->column_fields['description']),($insertion_mode == 'edit')?true:false);
 245  
 246          require('user_privileges/user_privileges_'.$current_user->id.'.php');
 247          $tabid = getTabid($module);
 248          if($is_admin == true || $profileGlobalPermission[1] == 0 || $profileGlobalPermission[2] ==0)
 249          {
 250              $sql = "update vtiger_crmentity set smownerid=".$ownerid.",modifiedby=".$current_user->id.",description=".$description_val.", modifiedtime=".$adb->formatString("vtiger_crmentity","modifiedtime",$date_var)." where crmid=".$this->id;
 251          }
 252          else
 253          {
 254              $profileList = getCurrentUserProfileList();
 255              $perm_qry = "SELECT columnname FROM vtiger_field INNER JOIN vtiger_profile2field ON vtiger_profile2field.fieldid = vtiger_field.fieldid INNER JOIN vtiger_def_org_field ON vtiger_def_org_field.fieldid = vtiger_field.fieldid WHERE vtiger_field.tabid = ".$tabid." AND vtiger_profile2field.visible = 0 AND vtiger_profile2field.profileid IN ".$profileList." AND vtiger_def_org_field.visible = 0 and vtiger_field.tablename='vtiger_crmentity' and vtiger_field.displaytype in (1,3);";
 256              $perm_result = $adb->query($perm_qry);
 257              $perm_rows = $adb->num_rows($perm_result);
 258              for($i=0; $i<$perm_rows; $i++)
 259              {
 260                  $columname[]=$adb->query_result($perm_result,$i,"columnname");
 261              }
 262              if(is_array($columname) && in_array("description",$columname))
 263              {
 264                  $sql = "update vtiger_crmentity set smownerid=".$ownerid.",modifiedby=".$current_user->id.",description=".$description_val.", modifiedtime=".$adb->formatString("vtiger_crmentity","modifiedtime",$date_var)." where crmid=".$this->id;
 265              }
 266              else
 267              {
 268                  $sql = "update vtiger_crmentity set smownerid=".$ownerid.",modifiedby=".$current_user->id.", modifiedtime=".$adb->formatString("vtiger_crmentity","modifiedtime",$date_var)." where crmid=".$this->id;
 269              }
 270          }
 271          $adb->query($sql);
 272          $sql1 ="delete from vtiger_ownernotify where crmid=".$this->id;
 273          $adb->query($sql1);
 274          if($ownerid != $current_user->id)
 275          {
 276              $sql1 = "insert into vtiger_ownernotify values(".$this->id.",".$ownerid.",null)";
 277              $adb->query($sql1);
 278          }
 279      }
 280      else
 281      {
 282          //if this is the create mode and the group allocation is chosen, then do the following
 283          $current_id = $adb->getUniqueID("vtiger_crmentity");
 284          $_REQUEST['currentid']=$current_id;
 285  
 286          $description_val = from_html($adb->formatString("vtiger_crmentity","description",$this->column_fields['description']),($insertion_mode == 'edit')?true:false);
 287          $sql = "insert into vtiger_crmentity (crmid,smcreatorid,smownerid,setype,description,createdtime,modifiedtime) values('".$current_id."','".$current_user->id."','".$ownerid."','".$module."',".$description_val.",".$adb->formatDate($date_var).",".$adb->formatDate($date_var).")";
 288          $adb->query($sql);
 289          $this->id = $current_id;
 290      }
 291  
 292      }
 293  
 294  
 295      /** Function to insert values in the specifed table for the specified module
 296          * @param $table_name -- table name:: Type varchar
 297          * @param $module -- module:: Type varchar
 298        */
 299    function insertIntoEntityTable($table_name, $module)
 300    {
 301        global $log;
 302          global $current_user;      
 303         $log->info("function insertIntoEntityTable ".$module.' vtiger_table name ' .$table_name);
 304        global $adb;
 305        $insertion_mode = $this->mode;
 306  
 307        //Checkin whether an entry is already is present in the vtiger_table to update
 308        if($insertion_mode == 'edit')
 309        {
 310            $check_query = "select * from ".$table_name." where ".$this->tab_name_index[$table_name]."=".$this->id;
 311            $check_result=$adb->query($check_query);
 312  
 313            $num_rows = $adb->num_rows($check_result);
 314  
 315            if($num_rows <= 0)
 316            {
 317                $insertion_mode = '';
 318            }     
 319        }
 320  
 321        if($insertion_mode == 'edit')
 322        {
 323            $update = '';
 324            $tabid= getTabid($module);    
 325            require('user_privileges/user_privileges_'.$current_user->id.'.php');
 326            if($is_admin == true || $profileGlobalPermission[1] == 0 || $profileGlobalPermission[2] ==0)
 327            {
 328  
 329                $sql = "select * from vtiger_field where tabid=".$tabid." and tablename='".$table_name."' and displaytype in (1,3)"; 
 330            }
 331            else
 332            {
 333                $profileList = getCurrentUserProfileList();
 334                $sql = "SELECT *
 335                FROM vtiger_field
 336                INNER JOIN vtiger_profile2field
 337                ON vtiger_profile2field.fieldid = vtiger_field.fieldid
 338                INNER JOIN vtiger_def_org_field
 339                ON vtiger_def_org_field.fieldid = vtiger_field.fieldid
 340                WHERE vtiger_field.tabid = ".$tabid."
 341                AND vtiger_profile2field.visible = 0 
 342                AND vtiger_profile2field.profileid IN ".$profileList."
 343                AND vtiger_def_org_field.visible = 0 and vtiger_field.tablename='".$table_name."' and vtiger_field.displaytype in (1,3)";
 344            }       
 345  
 346        }
 347        else
 348        {
 349            $column = $this->tab_name_index[$table_name];
 350            if($column == 'id' && $table_name == 'vtiger_users')
 351            {
 352               $currentuser_id = $adb->getUniqueID("vtiger_users");
 353              $this->id = $currentuser_id;
 354            }
 355            $value = $this->id;
 356              $tabid= getTabid($module);    
 357            $sql = "select * from vtiger_field where tabid=".$tabid." and tablename='".$table_name."' and displaytype in (1,3,4)"; 
 358        }
 359  
 360        $result = $adb->query($sql);
 361        $noofrows = $adb->num_rows($result);
 362        for($i=0; $i<$noofrows; $i++)
 363        {
 364            $fieldname=$adb->query_result($result,$i,"fieldname");
 365            $columname=$adb->query_result($result,$i,"columnname");
 366            $uitype=$adb->query_result($result,$i,"uitype");
 367            if(isset($this->column_fields[$fieldname]))
 368            {
 369                if($uitype == 56)
 370                {
 371                    if($this->column_fields[$fieldname] == 'on' || $this->column_fields[$fieldname] == 1)
 372                    {
 373                        $fldvalue = 1;
 374                    }
 375                    else
 376                    {
 377                        $fldvalue = 0;
 378                    }
 379  
 380                }
 381                elseif($uitype == 33)
 382                {
 383                    if(is_array($this->column_fields[$fieldname]))
 384                    {
 385                      $field_list = implode(' |##| ',$this->column_fields[$fieldname]);
 386                    }else
 387                    {
 388                      $field_list = $this->column_fields[$fieldname];
 389                    }
 390                    $fldvalue = $field_list;
 391                }
 392                elseif($uitype == 5 || $uitype == 6 || $uitype ==23)
 393                {
 394                    if($_REQUEST['action'] == 'Import')
 395                    {
 396                        $fldvalue = $this->column_fields[$fieldname];
 397                    }
 398                    else
 399                    {
 400                        $fldvalue = getDBInsertDateValue($this->column_fields[$fieldname]);
 401                    }
 402                }
 403                elseif($uitype == 7)
 404                {
 405                    //strip out the spaces and commas in numbers if given ie., in amounts there may be ,
 406                    $fldvalue = str_replace(",","",$this->column_fields[$fieldname]);//trim($this->column_fields[$fieldname],",");
 407  
 408                }
 409                else
 410                {
 411                    $fldvalue = $this->column_fields[$fieldname]; 
 412                    $fldvalue = stripslashes($fldvalue);
 413                }
 414                $fldvalue = from_html($adb->formatString($table_name,$columname,$fldvalue),($insertion_mode == 'edit')?true:false);
 415  
 416  
 417  
 418            }
 419            else
 420            {
 421                $fldvalue = '';
 422            }
 423            if($fldvalue=='') $fldvalue ="NULL";
 424            if($insertion_mode == 'edit')
 425            {
 426                if($table_name == 'vtiger_notes' && $columname == 'filename' && $_FILES['filename']['name'] == '')
 427                {
 428                    $fldvalue = $this->getOldFileName($this->id);
 429                }
 430                if($table_name != 'vtiger_ticketcomments')
 431                {
 432                    if($i == 0)
 433                    {
 434                        $update = $columname."=".$fldvalue."";
 435                    }
 436                    else
 437                    {
 438                        $update .= ', '.$columname."=".$fldvalue."";
 439                    }
 440                }
 441            }
 442            else
 443            {
 444                $column .= ", ".$columname;
 445                $value .= ", ".$fldvalue."";
 446            }
 447  
 448        }
 449  
 450  
 451  
 452  
 453  
 454        if($insertion_mode == 'edit')
 455        {
 456            if($_REQUEST['module'] == 'Potentials')
 457            {
 458                $dbquery = 'select sales_stage from vtiger_potential where potentialid = '.$this->id;
 459                $sales_stage = $adb->query_result($adb->query($dbquery),0,'sales_stage');
 460                if($sales_stage != $_REQUEST['sales_stage'] && $_REQUEST['sales_stage'] != '')
 461                {
 462                    $date_var = date('YmdHis');
 463                    $closingdate = getDBInsertDateValue($this->column_fields['closingdate']);
 464                    $sql = "insert into vtiger_potstagehistory values('',".$this->id.",'".$this->column_fields['amount']."','".$sales_stage."','".$this->column_fields['probability']."',0,".$adb->formatString("vtiger_potstagehistory","closedate",$closingdate).",".$adb->formatString("vtiger_potstagehistory","lastmodified",$date_var).")";
 465                    $adb->query($sql);
 466                }
 467            }
 468            elseif($_REQUEST['module'] == 'PurchaseOrder' || $_REQUEST['module'] == 'SalesOrder' || $_REQUEST['module'] == 'Quotes' || $_REQUEST['module'] == 'Invoice')
 469            {
 470                //added to update the history for PO, SO, Quotes and Invoice
 471                $history_field_array = Array(
 472                                "PurchaseOrder"=>"postatus",
 473                              "SalesOrder"=>"sostatus",
 474                              "Quotes"=>"quotestage",
 475                              "Invoice"=>"invoicestatus"
 476                                );
 477  
 478                $inventory_module = $_REQUEST['module'];
 479  
 480                if($_REQUEST['ajxaction'] == 'DETAILVIEW')//if we use ajax edit
 481                {
 482                    if($inventory_module == "PurchaseOrder")
 483                        $relatedname = getVendorName($this->column_fields['vendor_id']);
 484                    else
 485                        $relatedname = getAccountName($this->column_fields['account_id']);
 486  
 487                    $total = $this->column_fields['hdnGrandTotal'];
 488                }
 489                else//using edit button and save
 490                {
 491                    if($inventory_module == "PurchaseOrder")
 492                        $relatedname = $_REQUEST["vendor_name"];
 493                    else
 494                        $relatedname = $_REQUEST["account_name"];
 495  
 496                  $total = $_REQUEST['total'];
 497                }
 498  
 499                $oldvalue = getSingleFieldValue($this->table_name,$history_field_array[$inventory_module],$this->module_id,$this->id);
 500                if($oldvalue != $this->column_fields["$history_field_array[$inventory_module]"])
 501                {
 502                    addInventoryHistory($inventory_module, $this->id,$relatedname,$total,$this->column_fields["$history_field_array[$inventory_module]"]);
 503                }
 504            }
 505  
 506            //Check done by Don. If update is empty the the query fails
 507            if(trim($update) != '')
 508                {
 509                $sql1 = "update ".$table_name." set ".$update." where ".$this->tab_name_index[$table_name]."=".$this->id;
 510  
 511                $adb->query($sql1); 
 512            }
 513            //to disable the update of groupentity relation in ajax edit for the fields except assigned_user_id field
 514            if($_REQUEST['ajxaction'] != 'DETAILVIEW' || ($_REQUEST['ajxaction'] == 'DETAILVIEW' && $_REQUEST['fldName'] == 'assigned_user_id'))
 515            {      
 516                if($_REQUEST['assigntype'] == 'T')
 517                {
 518                    $groupname = $_REQUEST['assigned_group_name'];
 519                    //echo 'about to update lead group relation';
 520                    if($module == 'Leads' && $table_name == 'vtiger_leaddetails')
 521                    {
 522                        updateLeadGroupRelation($this->id,$groupname);
 523                    }
 524                    elseif($module == 'Accounts' && $table_name == 'vtiger_account')
 525                    {
 526                        updateAccountGroupRelation($this->id,$groupname);
 527                    }
 528                    elseif($module == 'Contacts' && $table_name == 'vtiger_contactdetails')
 529                    {
 530                        updateContactGroupRelation($this->id,$groupname);
 531                    }
 532                    elseif($module == 'Potentials' && $table_name == 'vtiger_potential')
 533                    {
 534                        updatePotentialGroupRelation($this->id,$groupname);
 535                    }
 536                    elseif($module == 'Quotes' && $table_name == 'vtiger_quotes')
 537                    {
 538                        updateQuoteGroupRelation($this->id,$groupname);
 539                    }
 540                    elseif($module == 'SalesOrder' && $table_name == 'vtiger_salesorder')
 541                    {
 542                        updateSoGroupRelation($this->id,$groupname);
 543                    }
 544                    elseif($module == 'Invoice' && $table_name == 'vtiger_invoice')
 545                    {
 546                        updateInvoiceGroupRelation($this->id,$groupname);
 547                    }
 548                    elseif($module == 'PurchaseOrder' && $table_name == 'vtiger_purchaseorder')
 549                    {
 550                        updatePoGroupRelation($this->id,$groupname);
 551                    }
 552                    elseif($module == 'HelpDesk' && $table_name == 'vtiger_troubletickets')
 553                    {
 554                        updateTicketGroupRelation($this->id,$groupname);
 555                    }
 556                    elseif($module == 'Campaigns' && $table_name == 'vtiger_campaign')
 557                    {
 558                        updateCampaignGroupRelation($this->id,$groupname);
 559                    }
 560                    elseif($module =='Calendar' || $module =='Events' || $module == 'Emails')
 561                    {
 562                        if($table_name == 'vtiger_activity')
 563                        {
 564                            updateActivityGroupRelation($this->id,$groupname);
 565                        }
 566                    }
 567  
 568  
 569                }
 570                else
 571                {
 572                    //echo 'about to update lead group relation again!';
 573                    if($module == 'Leads' && $table_name == 'vtiger_leaddetails')
 574                    {
 575                        updateLeadGroupRelation($this->id,'');
 576                    }
 577                    elseif($module == 'Accounts' && $table_name == 'vtiger_account')
 578                    {
 579                        updateAccountGroupRelation($this->id,'');
 580                    }
 581                    elseif($module == 'Contacts' && $table_name == 'vtiger_contactdetails')
 582                    {
 583                        updateContactGroupRelation($this->id,'');
 584                    }
 585                    elseif($module == 'Potentials' && $table_name == 'vtiger_potential')
 586                    {
 587                        updatePotentialGroupRelation($this->id,'');
 588                    }
 589                    elseif($module == 'Quotes' && $table_name == 'vtiger_quotes')
 590                    {
 591                        updateQuoteGroupRelation($this->id,'');
 592                    }
 593                    elseif($module == 'SalesOrder' && $table_name == 'vtiger_salesorder')
 594                    {
 595                        updateSoGroupRelation($this->id,'');
 596                    }
 597                    elseif($module == 'Invoice' && $table_name == 'vtiger_invoice')
 598                    {
 599                        updateInvoiceGroupRelation($this->id,'');
 600                    }
 601                    elseif($module == 'PurchaseOrder' && $table_name == 'vtiger_purchaseorder')
 602                    {
 603                        updatePoGroupRelation($this->id,'');
 604                    }
 605                    elseif($module == 'HelpDesk' && $table_name == 'vtiger_troubletickets')
 606                    {
 607                        updateTicketGroupRelation($this->id,'');
 608                    }
 609                    elseif($module == 'Campaigns' && $table_name == 'vtiger_campaign')
 610                    {
 611                        updateCampaignGroupRelation($this->id,$groupname);
 612                    }
 613                    elseif($module =='Calendar' || $module =='Events' || $module == 'Emails')
 614                    {
 615                        if($table_name == 'vtiger_activity')
 616                        {
 617                            updateActivityGroupRelation($this->id,$groupname);
 618                        }
 619                    }
 620  
 621  
 622                }
 623            }
 624  
 625        }
 626        else
 627        {    
 628            $sql1 = "insert into ".$table_name." (".$column.") values(".$value.")";
 629            $adb->query($sql1); 
 630            $groupname = $_REQUEST['assigned_group_name'];
 631            if($_REQUEST['assigntype'] == 'T' && $table_name == 'vtiger_leaddetails')
 632            {
 633                insert2LeadGroupRelation($this->id,$groupname);
 634            }
 635            elseif($_REQUEST['assigntype'] == 'T' && $table_name == 'vtiger_account')
 636            {
 637                insert2AccountGroupRelation($this->id,$groupname);
 638            }
 639            elseif($_REQUEST['assigntype'] == 'T' && $table_name == 'vtiger_contactdetails')
 640            {
 641                insert2ContactGroupRelation($this->id,$groupname);
 642            }
 643            elseif($_REQUEST['assigntype'] == 'T' && $table_name == 'vtiger_potential')
 644            {
 645                insert2PotentialGroupRelation($this->id,$groupname);
 646            }
 647            elseif($_REQUEST['assigntype'] == 'T' && $table_name == 'vtiger_quotes')
 648            {
 649                insert2QuoteGroupRelation($this->id,$groupname);
 650            }
 651            elseif($_REQUEST['assigntype'] == 'T' && $table_name == 'vtiger_salesorder')
 652            {
 653                insert2SoGroupRelation($this->id,$groupname);
 654            }
 655            elseif($_REQUEST['assigntype'] == 'T' && $table_name == 'vtiger_invoice')
 656            {
 657                insert2InvoiceGroupRelation($this->id,$groupname);
 658            }
 659            elseif($_REQUEST['assigntype'] == 'T' && $table_name == 'vtiger_purchaseorder')
 660            {
 661                insert2PoGroupRelation($this->id,$groupname);
 662            }
 663            elseif($_REQUEST['assigntype'] == 'T' && $table_name == 'vtiger_activity') 
 664            {
 665                insert2ActivityGroupRelation($this->id,$groupname);
 666            }
 667            elseif($_REQUEST['assigntype'] == 'T' && $table_name == 'vtiger_troubletickets') 
 668            {
 669                insert2TicketGroupRelation($this->id,$groupname);
 670            }
 671            elseif($_REQUEST['assigntype'] == 'T' && $table_name == 'vtiger_campaign')
 672            {
 673                insert2CampaignGroupRelation($this->id,$groupname);
 674            }
 675  
 676        }
 677  
 678    }
 679      /** Function to delete a record in the specifed table 
 680          * @param $table_name -- table name:: Type varchar
 681        * The function will delete a record .The id is obtained from the class variable $this->id and the columnname got from $this->tab_name_index[$table_name]
 682        */
 683  function deleteRelation($table_name)
 684  {
 685           global $adb;
 686           $check_query = "select * from ".$table_name." where ".$this->tab_name_index[$table_name]."=".$this->id;
 687           $check_result=$adb->query($check_query);
 688           $num_rows = $adb->num_rows($check_result);
 689  
 690           if($num_rows == 1)
 691           {
 692                  $del_query = "DELETE from ".$table_name." where ".$this->tab_name_index[$table_name]."=".$this->id;
 693                  $adb->query($del_query);
 694           }
 695  
 696  }
 697      /** Function to attachment filename of the given entity 
 698          * @param $notesid -- crmid:: Type Integer
 699        * The function will get the attachmentsid for the given entityid from vtiger_seattachmentsrel table and get the attachmentsname from vtiger_attachments table 
 700        * returns the 'filename'
 701        */
 702  function getOldFileName($notesid)
 703  {
 704         global $log;
 705  $log->info("in getOldFileName  ".$notesid);
 706      global $adb;
 707      $query1 = "select * from vtiger_seattachmentsrel where crmid=".$notesid;
 708      $result = $adb->query($query1);
 709      $noofrows = $adb->num_rows($result);
 710      if($noofrows != 0)
 711          $attachmentid = $adb->query_result($result,0,'attachmentsid');
 712      if($attachmentid != '')
 713      {
 714          $query2 = "select * from vtiger_attachments where attachmentsid=".$attachmentid;
 715          $filename = $adb->query_result($adb->query($query2),0,'name');
 716      }
 717      return "'".$filename."'";
 718  }
 719      
 720      
 721      
 722  
 723  
 724  
 725  
 726  
 727  // Code included by Jaguar - Ends 
 728  
 729      /** Function to retrive the information of the given recordid ,module 
 730          * @param $record -- Id:: Type Integer
 731          * @param $module -- module:: Type varchar
 732        * This function retrives the information from the database and sets the value in the class columnfields array
 733        */
 734    function retrieve_entity_info($record, $module)
 735    {
 736      global $adb,$log,$app_strings;
 737      $result = Array();
 738      foreach($this->tab_name_index as $table_name=>$index)
 739      {
 740          $result[$table_name] = $adb->query("select * from ".$table_name." where ".$index."=".$record);
 741          if($adb->query_result($result["vtiger_crmentity"],0,"deleted") == 1)
 742          die("<br><br><center>".$app_strings['LBL_RECORD_DELETE']." <a href='javascript:window.history.back()'>".$app_strings['LBL_GO_BACK'].".</a></center>");
 743      }
 744      $tabid = getTabid($module);
 745      $sql1 =  "select * from vtiger_field where tabid=".$tabid;
 746      $result1 = $adb->query($sql1);
 747      $noofrows = $adb->num_rows($result1);
 748      for($i=0; $i<$noofrows; $i++)
 749      {
 750        $fieldcolname = $adb->query_result($result1,$i,"columnname");
 751        $tablename = $adb->query_result($result1,$i,"tablename");
 752        $fieldname = $adb->query_result($result1,$i,"fieldname");
 753  
 754        $fld_value = $adb->query_result($result[$tablename],0,$fieldcolname);
 755        $this->column_fields[$fieldname] = $fld_value;
 756                  
 757      }
 758      if($module == 'Users')
 759      {
 760          for($i=0; $i<$noofrows; $i++)
 761          {
 762              $fieldcolname = $adb->query_result($result1,$i,"columnname");
 763              $tablename = $adb->query_result($result1,$i,"tablename");
 764              $fieldname = $adb->query_result($result1,$i,"fieldname");
 765              $fld_value = $adb->query_result($result[$tablename],0,$fieldcolname);
 766              $this->$fieldname = $fld_value;
 767  
 768          }
 769      }
 770          
 771      $this->column_fields["record_id"] = $record;
 772      $this->column_fields["record_module"] = $module;
 773    }
 774  
 775      /** Function to saves the values in all the tables mentioned in the class variable $tab_name for the specified module
 776          * @param $module -- module:: Type varchar
 777        */
 778  	function save($module_name) 
 779      {
 780          global $log;
 781              $log->debug("module name is ".$module_name);
 782          //GS Save entity being called with the modulename as parameter
 783          $this->saveentity($module_name);
 784      }
 785    
 786  	function process_list_query($query, $row_offset, $limit= -1, $max_per_page = -1)
 787      {
 788          global $list_max_entries_per_page;
 789          $this->log->debug("process_list_query: ".$query);
 790          if(!empty($limit) && $limit != -1){
 791              $result =& $this->db->limitQuery($query, $row_offset + 0, $limit,true,"Error retrieving $this->object_name list: ");
 792          }else{
 793              $result =& $this->db->query($query,true,"Error retrieving $this->object_name list: ");
 794          }
 795  
 796          $list = Array();
 797          if($max_per_page == -1){
 798              $max_per_page     = $list_max_entries_per_page;
 799          }
 800          $rows_found =  $this->db->getRowCount($result);
 801  
 802          $this->log->debug("Found $rows_found ".$this->object_name."s");
 803                  
 804          $previous_offset = $row_offset - $max_per_page;
 805          $next_offset = $row_offset + $max_per_page;
 806  
 807          if($rows_found != 0)
 808          {
 809  
 810              // We have some data.
 811  
 812              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)){
 813  
 814                  
 815                  foreach($this->list_fields as $entry)
 816                  {
 817  
 818                      foreach($entry as $key=>$field) // this will be cycled only once
 819                      {                        
 820                          if (isset($row[$field])) {
 821                              $this->column_fields[$this->list_fields_names[$key]] = $row[$field];
 822                          
 823                          
 824                              $this->log->debug("$this->object_name({$row['id']}): ".$field." = ".$this->$field);
 825                          }
 826                          else 
 827                          {
 828                              $this->column_fields[$this->list_fields_names[$key]] = "";
 829                          }
 830                      }
 831                  }
 832  
 833  
 834                  //$this->db->println("here is the bug");
 835                  
 836  
 837                  $list[] = clone($this);//added by Richie to support PHP5
 838              }
 839          }
 840  
 841          $response = Array();
 842          $response['list'] = $list;
 843          $response['row_count'] = $rows_found;
 844          $response['next_offset'] = $next_offset;
 845          $response['previous_offset'] = $previous_offset;
 846  
 847          return $response;
 848      }
 849  
 850  	function process_full_list_query($query)
 851      {
 852          $this->log->debug("CRMEntity:process_full_list_query");
 853          $result =& $this->db->query($query, false);
 854          $this->log->debug("CRMEntity:process_full_list_query: result is ".$result);
 855  
 856  
 857          if($this->db->getRowCount($result) > 0){
 858          
 859          //    $this->db->println("process_full mid=".$this->module_id." mname=".$this->module_name);
 860              // We have some data.
 861              while ($row = $this->db->fetchByAssoc($result)) {                
 862                  $rowid=$row[$this->module_id];
 863  
 864                  if(isset($rowid))
 865                             $this->retrieve_entity_info($rowid,$this->module_name);
 866                  else
 867                      $this->db->println("rowid not set unable to retrieve");
 868                   
 869                   
 870                  
 871          //clone function added to resolvoe PHP5 compatibility issue in Dashboards
 872          //If we do not use clone, while using PHP5, the memory address remains fixed but the
 873      //data gets overridden hence all the rows that come in bear the same value. This in turn
 874  //provides a wrong display of the Dashboard graphs. The data is erroneously shown for a specific month alone
 875  //Added by Richie
 876                  $list[] = clone($this);//added by Richie to support PHP5
 877              }
 878          }
 879  
 880          if (isset($list)) return $list;
 881          else return null;
 882      }
 883      
 884      /** This function should be overridden in each module.  It marks an item as deleted.
 885      * If it is not overridden, then marking this type of item is not allowed
 886       * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
 887       * All Rights Reserved..
 888       * Contributor(s): ______________________________________..
 889      */
 890  	function mark_deleted($id)
 891      {
 892          $query = "UPDATE vtiger_crmentity set deleted=1 where crmid='$id'";
 893          $this->db->query($query, true,"Error marking record deleted: ");
 894  
 895  
 896      }
 897  
 898  
 899  	function retrieve_by_string_fields($fields_array, $encode=true) 
 900      { 
 901          $where_clause = $this->get_where($fields_array);
 902          
 903          $query = "SELECT * FROM $this->table_name $where_clause";
 904          $this->log->debug("Retrieve $this->object_name: ".$query);
 905          $result =& $this->db->requireSingleResult($query, true, "Retrieving record $where_clause:");
 906          if( empty($result)) 
 907          { 
 908               return null; 
 909          } 
 910  
 911           $row = $this->db->fetchByAssoc($result,-1, $encode);
 912  
 913          foreach($this->column_fields as $field) 
 914          { 
 915              if(isset($row[$field])) 
 916              { 
 917                  $this->$field = $row[$field];
 918              }
 919          } 
 920          return $this;
 921      }
 922  
 923      // this method is called during an import before inserting a bean
 924      // define an associative array called $special_fields
 925      // the keys are user defined, and don't directly map to the bean's vtiger_fields
 926      // the value is the method name within that bean that will do extra
 927      // processing for that vtiger_field. example: 'full_name'=>'get_names_from_full_name'
 928  
 929  	function process_special_fields() 
 930      { 
 931          foreach ($this->special_functions as $func_name) 
 932          { 
 933              if ( method_exists($this,$func_name) ) 
 934              { 
 935                  $this->$func_name(); 
 936              } 
 937          } 
 938      }
 939  
 940      /**
 941           * Function to check if the custom vtiger_field vtiger_table exists
 942           * return true or false
 943           */
 944          function checkIfCustomTableExists($tablename)
 945          {
 946                  $query = "select * from ".$tablename;
 947                  $result = $this->db->query($query);
 948                  $testrow = $this->db->num_fields($result);
 949                  if($testrow > 1)
 950                  {
 951                          $exists=true;
 952                  }
 953                  else
 954                  {
 955                          $exists=false;
 956                  }
 957                  return $exists;
 958          }
 959  
 960      /**
 961       * function to construct the query to fetch the custom vtiger_fields
 962       * return the query to fetch the custom vtiger_fields
 963           */
 964          function constructCustomQueryAddendum($tablename,$module)
 965          {
 966                  global $adb;
 967          $tabid=getTabid($module);        
 968                  $sql1 = "select columnname,fieldlabel from vtiger_field where generatedtype=2 and tabid=".$tabid;
 969                  $result = $adb->query($sql1);
 970                  $numRows = $adb->num_rows($result);
 971                  $sql3 = "select ";
 972                  for($i=0; $i < $numRows;$i++)
 973                  {
 974                          $columnName = $adb->query_result($result,$i,"columnname");
 975                          $fieldlable = $adb->query_result($result,$i,"fieldlabel");
 976                          //construct query as below
 977                          if($i == 0)
 978                          {
 979                                  $sql3 .= $tablename.".".$columnName. " '" .$fieldlable."'";
 980                          }
 981                          else
 982                          {
 983                                  $sql3 .= ", ".$tablename.".".$columnName. " '" .$fieldlable."'";
 984                          }
 985  
 986                  }
 987                  if($numRows>0)
 988                  {
 989                          $sql3=$sql3.',';
 990                  }
 991                  return $sql3;
 992  
 993          }
 994  
 995  
 996      /**
 997       * This function returns a full (ie non-paged) list of the current object type.  
 998       * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
 999       * All Rights Reserved..
1000       * Contributor(s): ______________________________________..
1001       */
1002  	function get_full_list($order_by = "", $where = "") {
1003          $this->log->debug("get_full_list:  order_by = '$order_by' and where = '$where'");
1004          $query = $this->create_list_query($order_by, $where);
1005          return $this->process_full_list_query($query);
1006      }
1007  
1008      /**
1009       * Track the viewing of a detail record.  This leverages get_summary_text() which is object specific
1010       * params $user_id - The user that is viewing the record.
1011       * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
1012       * All Rights Reserved..
1013       * Contributor(s): ______________________________________..
1014       */
1015  	function track_view($user_id, $current_module,$id='')
1016      {
1017          $this->log->debug("About to call vtiger_tracker (user_id, module_name, item_id)($user_id, $current_module, $this->id)");
1018  
1019          $tracker = new Tracker();
1020          $tracker->track_view($user_id, $current_module, $id, '');
1021      }
1022  
1023      
1024  
1025  }
1026  ?>


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