[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Emails/ -> Emails.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/modules/Emails/Emails.php,v 1.41 2005/04/28 08:11:21 rank Exp $
  17   * Description:  TODO: To be written.
  18   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
  19   * All Rights Reserved.
  20   * Contributor(s): ______________________________________..
  21   ********************************************************************************/
  22  
  23  // Email is used to store customer information.
  24  class Emails extends CRMEntity {
  25  
  26      var $log;
  27      var $db;
  28      var $table_name = "vtiger_activity";
  29      var $table_index = 'activityid';
  30      // Stored vtiger_fields
  31      // added to check email save from plugin or not
  32      var $plugin_save = false;
  33      var $rel_users_table = "vtiger_salesmanactivityrel";
  34      var $rel_contacts_table = "vtiger_cntactivityrel";
  35      var $rel_serel_table = "vtiger_seactivityrel";
  36      var $tab_name = Array('vtiger_crmentity', 'vtiger_activity', 'vtiger_emaildetails');
  37      var $tab_name_index = Array('vtiger_crmentity' => 'crmid', 'vtiger_activity' => 'activityid',
  38          'vtiger_seactivityrel' => 'activityid', 'vtiger_cntactivityrel' => 'activityid', 'vtiger_email_track' => 'mailid', 'vtiger_emaildetails' => 'emailid');
  39      // This is the list of vtiger_fields that are in the lists.
  40      var $list_fields = Array(
  41          'Subject' => Array('activity' => 'subject'),
  42          'Related to' => Array('seactivityrel' => 'parent_id'),
  43          'Date Sent' => Array('activity' => 'date_start'),
  44          'Time Sent' => Array('activity' => 'time_start'),
  45          'Assigned To' => Array('crmentity', 'smownerid'),
  46          'Access Count' => Array('email_track', 'access_count')
  47      );
  48      var $list_fields_name = Array(
  49          'Subject' => 'subject',
  50          'Related to' => 'parent_id',
  51          'Date Sent' => 'date_start',
  52          'Time Sent' => 'time_start',
  53          'Assigned To' => 'assigned_user_id',
  54          'Access Count' => 'access_count'
  55      );
  56      var $list_link_field = 'subject';
  57      var $column_fields = Array();
  58      var $sortby_fields = Array('subject', 'date_start', 'saved_toid');
  59      //Added these variables which are used as default order by and sortorder in ListView
  60      var $default_order_by = 'date_start';
  61      var $default_sort_order = 'DESC';
  62      // Used when enabling/disabling the mandatory fields for the module.
  63      // Refers to vtiger_field.fieldname values.
  64      var $mandatory_fields = Array('subject', 'assigned_user_id');
  65  
  66      /** This function will set the columnfields for Email module
  67       */
  68  	function Emails() {
  69          $this->log = LoggerManager::getLogger('email');
  70          $this->log->debug("Entering Emails() method ...");
  71          $this->log = LoggerManager::getLogger('email');
  72          $this->db = PearDatabase::getInstance();
  73          $this->column_fields = getColumnFields('Emails');
  74          $this->log->debug("Exiting Email method ...");
  75      }
  76  
  77  	function save_module($module) {
  78          global $adb;
  79          //Inserting into seactivityrel
  80          //modified by Richie as raju's implementation broke the feature for addition of webmail to vtiger_crmentity.need to be more careful in future while integrating code
  81          if ($_REQUEST['module'] == "Emails" && $_REQUEST['smodule'] != 'webmails' && (!$this->plugin_save)) {
  82              if ($_REQUEST['currentid'] != '') {
  83                  $actid = $_REQUEST['currentid'];
  84              } else {
  85                  $actid = $_REQUEST['record'];
  86              }
  87              $parentid = $_REQUEST['parent_id'];
  88              if ($_REQUEST['module'] != 'Emails' && $_REQUEST['module'] != 'Webmails') {
  89                  if (!$parentid) {
  90                      $parentid = $adb->getUniqueID('vtiger_seactivityrel');
  91                  }
  92                  $mysql = 'insert into vtiger_seactivityrel values(?,?)';
  93                  $adb->pquery($mysql, array($parentid, $actid));
  94              } else {
  95                  $myids = explode("|", $parentid);  //2@71|
  96                  for ($i = 0; $i < (count($myids) - 1); $i++) {
  97                      $realid = explode("@", $myids[$i]);
  98                      $mycrmid = $realid[0];
  99                      //added to handle the relationship of emails with vtiger_users
 100                      if ($realid[1] == -1) {
 101                          $del_q = 'delete from vtiger_salesmanactivityrel where smid=? and activityid=?';
 102                          $adb->pquery($del_q, array($mycrmid, $actid));
 103                          $mysql = 'insert into vtiger_salesmanactivityrel values(?,?)';
 104                      } else {
 105                          $del_q = 'delete from vtiger_seactivityrel where crmid=? and activityid=?';
 106                          $adb->pquery($del_q, array($mycrmid, $actid));
 107                          $mysql = 'insert into vtiger_seactivityrel values(?,?)';
 108                      }
 109                      $params = array($mycrmid, $actid);
 110                      $adb->pquery($mysql, $params);
 111                  }
 112              }
 113          } else {
 114              if (isset($this->column_fields['parent_id']) && $this->column_fields['parent_id'] != '') {
 115                  $adb->pquery("DELETE FROM vtiger_seactivityrel WHERE crmid = ? AND activityid = ? ",
 116                          array($this->column_fields['parent_id'], $this->id));
 117                  //$this->insertIntoEntityTable('vtiger_seactivityrel', $module);
 118                  $sql = 'insert into vtiger_seactivityrel values(?,?)';
 119                  $params = array($this->column_fields['parent_id'], $this->id);
 120                  $adb->pquery($sql, $params);
 121              } elseif ($this->column_fields['parent_id'] == '' && $insertion_mode == "edit") {
 122                  $this->deleteRelation('vtiger_seactivityrel');
 123              }
 124          }
 125  
 126  
 127          //Insert into cntactivity rel
 128  
 129          if (isset($this->column_fields['contact_id']) && $this->column_fields['contact_id'] != '') {
 130              $this->insertIntoEntityTable('vtiger_cntactivityrel', $module);
 131          } elseif ($this->column_fields['contact_id'] == '' && $insertion_mode == "edit") {
 132              $this->deleteRelation('vtiger_cntactivityrel');
 133          }
 134  
 135          //Inserting into attachment
 136  
 137          $this->insertIntoAttachment($this->id, $module);
 138      }
 139  
 140  	function insertIntoAttachment($id, $module) {
 141          global $log, $adb;
 142          $log->debug("Entering into insertIntoAttachment($id,$module) method.");
 143  
 144          $file_saved = false;
 145  
 146          //Added to send generated Invoice PDF with mail
 147          $pdfAttached = $_REQUEST['pdf_attachment'];
 148          //created Invoice pdf is attached with the mail
 149          if (isset($_REQUEST['pdf_attachment']) && $_REQUEST['pdf_attachment'] != '') {
 150              $file_saved = pdfAttach($this, $module, $pdfAttached, $id);
 151          }
 152  
 153          //This is to added to store the existing attachment id of the contact where we should delete this when we give new image
 154          foreach ($_FILES as $fileindex => $files) {
 155              if ($files['name'] != '' && $files['size'] > 0) {
 156                  $files['original_name'] = vtlib_purify($_REQUEST[$fileindex . '_hidden']);
 157                  $file_saved = $this->uploadAndSaveFile($id, $module, $files);
 158              }
 159          }
 160          if ($module == 'Emails' && isset($_REQUEST['att_id_list']) && $_REQUEST['att_id_list'] != '') {
 161              $att_lists = explode(";", $_REQUEST['att_id_list'], -1);
 162              $id_cnt = count($att_lists);
 163              if ($id_cnt != 0) {
 164                  for ($i = 0; $i < $id_cnt; $i++) {
 165                      $sql_rel = 'insert into vtiger_seattachmentsrel values(?,?)';
 166                      $adb->pquery($sql_rel, array($id, $att_lists[$i]));
 167                  }
 168              }
 169          }
 170          if ($_REQUEST['att_module'] == 'Webmails') {
 171              require_once("modules/Webmails/Webmails.php");
 172              require_once("modules/Webmails/MailParse.php");
 173              require_once('modules/Webmails/MailBox.php');
 174              //$mailInfo = getMailServerInfo($current_user);
 175              //$temprow = $adb->fetch_array($mailInfo);
 176  
 177              $MailBox = new MailBox($_REQUEST["mailbox"]);
 178              $mbox = $MailBox->mbox;
 179              $webmail = new Webmails($mbox, $_REQUEST['mailid']);
 180              $array_tab = Array();
 181              $webmail->loadMail($array_tab);
 182              if (isset($webmail->att_details)) {
 183                  foreach ($webmail->att_details as $fileindex => $files) {
 184                      if ($files['name'] != '' && $files['size'] > 0) {
 185                          //print_r($files);
 186                          $file_saved = $this->saveForwardAttachments($id, $module, $files);
 187                      }
 188                  }
 189              }
 190          }
 191          $log->debug("Exiting from insertIntoAttachment($id,$module) method.");
 192      }
 193  
 194  	function saveForwardAttachments($id, $module, $file_details) {
 195          global $log;
 196          $log->debug("Entering into saveForwardAttachments($id,$module,$file_details) method.");
 197          global $adb, $current_user;
 198          global $upload_badext;
 199          require_once('modules/Webmails/MailBox.php');
 200          $mailbox = $_REQUEST["mailbox"];
 201          $MailBox = new MailBox($mailbox);
 202          $mail = $MailBox->mbox;
 203          $binFile = sanitizeUploadFileName($file_details['name'], $upload_badext);
 204          $filename = ltrim(basename(" " . $binFile)); //allowed filename like UTF-8 characters
 205          $filetype = $file_details['type'];
 206          $filesize = $file_details['size'];
 207          $filepart = $file_details['part'];
 208          $transfer = $file_details['transfer'];
 209          $file = imap_fetchbody($mail, $_REQUEST['mailid'], $filepart);
 210          if ($transfer == 'BASE64')
 211              $file = imap_base64($file);
 212          elseif ($transfer == 'QUOTED-PRINTABLE')
 213              $file = imap_qprint($file);
 214          $current_id = $adb->getUniqueID("vtiger_crmentity");
 215          $date_var = date('Y-m-d H:i:s');
 216          //to get the owner id
 217          $ownerid = $this->column_fields['assigned_user_id'];
 218          if (!isset($ownerid) || $ownerid == '')
 219              $ownerid = $current_user->id;
 220          $upload_file_path = decideFilePath();
 221          file_put_contents($upload_file_path . $current_id . "_" . $filename, $file);
 222  
 223          $sql1 = "insert into vtiger_crmentity (crmid,smcreatorid,smownerid,setype,description,createdtime,modifiedtime) values(?,?,?,?,?,?,?)";
 224          $params1 = array($current_id, $current_user->id, $ownerid, $module . " Attachment", $this->column_fields['description'], $adb->formatDate($date_var, true), $adb->formatDate($date_var, true));
 225          $adb->pquery($sql1, $params1);
 226  
 227          $sql2 = "insert into vtiger_attachments(attachmentsid, name, description, type, path) values(?,?,?,?,?)";
 228          $params2 = array($current_id, $filename, $this->column_fields['description'], $filetype, $upload_file_path);
 229          $result = $adb->pquery($sql2, $params2);
 230  
 231          if ($_REQUEST['mode'] == 'edit') {
 232              if ($id != '' && $_REQUEST['fileid'] != '') {
 233                  $delquery = 'delete from vtiger_seattachmentsrel where crmid = ? and attachmentsid = ?';
 234                  $adb->pquery($delquery, array($id, $_REQUEST['fileid']));
 235              }
 236          }
 237          $sql3 = 'insert into vtiger_seattachmentsrel values(?,?)';
 238          $adb->pquery($sql3, array($id, $current_id));
 239          return true;
 240          $log->debug("exiting from  saveforwardattachment function.");
 241      }
 242  
 243      /** Returns a list of the associated contacts
 244       * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
 245       * All Rights Reserved..
 246       * Contributor(s): ______________________________________..
 247       */
 248  	function get_contacts($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 249          global $log, $singlepane_view, $currentModule, $current_user;
 250          $log->debug("Entering get_contacts(" . $id . ") method ...");
 251          $this_module = $currentModule;
 252  
 253          $related_module = vtlib_getModuleNameById($rel_tab_id);
 254          require_once("modules/$related_module/$related_module.php");
 255          $other = new $related_module();
 256          vtlib_setup_modulevars($related_module, $other);
 257          $singular_modname = vtlib_toSingular($related_module);
 258  
 259          $parenttab = getParentTab();
 260  
 261          $returnset = '&return_module=' . $this_module . '&return_action=DetailView&return_id=' . $id;
 262  
 263          $button = '';
 264  
 265          if ($actions) {
 266              if (is_string($actions))
 267                  $actions = explode(',', strtoupper($actions));
 268              if (in_array('SELECT', $actions) && isPermitted($related_module, 4, '') == 'yes') {
 269                  $button .= "<input title='" . getTranslatedString('LBL_SELECT') . " " . getTranslatedString($related_module) . "' class='crmbutton small edit' type='button' onclick=\"return window.open('index.php?module=$related_module&return_module=$currentModule&action=Popup&popuptype=detailview&select=enable&form=EditView&form_submit=false&recordid=$id&parenttab=$parenttab','test','width=640,height=602,resizable=0,scrollbars=0');\" value='" . getTranslatedString('LBL_SELECT') . " " . getTranslatedString($related_module) . "'>&nbsp;";
 270              }
 271              if (in_array('BULKMAIL', $actions) && isPermitted($related_module, 1, '') == 'yes') {
 272                  $button .= "<input title='" . getTranslatedString('LBL_BULK_MAILS') . "' class='crmbutton small create'" .
 273                          " onclick='this.form.action.value=\"sendmail\";this.form.module.value=\"$this_module\"' type='submit' name='button'" .
 274                          " value='" . getTranslatedString('LBL_BULK_MAILS') . "'>";
 275              }
 276          }
 277  
 278          $query = 'select vtiger_contactdetails.accountid, vtiger_contactdetails.contactid, vtiger_contactdetails.firstname,vtiger_contactdetails.lastname, vtiger_contactdetails.department, vtiger_contactdetails.title, vtiger_contactdetails.email, vtiger_contactdetails.phone, vtiger_contactdetails.emailoptout, vtiger_crmentity.crmid, vtiger_crmentity.smownerid, vtiger_crmentity.modifiedtime from vtiger_contactdetails inner join vtiger_cntactivityrel on vtiger_cntactivityrel.contactid=vtiger_contactdetails.contactid inner join vtiger_crmentity on vtiger_crmentity.crmid = vtiger_contactdetails.contactid left join vtiger_groups on vtiger_groups.groupid=vtiger_crmentity.smownerid where vtiger_cntactivityrel.activityid=' . $adb->quote($id) . ' and vtiger_crmentity.deleted=0';
 279  
 280          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 281  
 282          if ($return_value == null)
 283              $return_value = Array();
 284          $return_value['CUSTOM_BUTTON'] = $button;
 285  
 286          $log->debug("Exiting get_contacts method ...");
 287          return $return_value;
 288      }
 289  
 290      /** Returns the column name that needs to be sorted
 291       * Portions created by vtigerCRM are Copyright (C) vtigerCRM.
 292       * All Rights Reserved..
 293       * Contributor(s): Mike Crowe
 294       */
 295  	function getSortOrder() {
 296          global $log;
 297          $log->debug("Entering getSortOrder() method ...");
 298          if (isset($_REQUEST['sorder']))
 299              $sorder = $this->db->sql_escape_string($_REQUEST['sorder']);
 300          else
 301              $sorder = (($_SESSION['EMAILS_SORT_ORDER'] != '') ? ($_SESSION['EMAILS_SORT_ORDER']) : ($this->default_sort_order));
 302  
 303          $log->debug("Exiting getSortOrder method ...");
 304          return $sorder;
 305      }
 306  
 307      /** Returns the order in which the records need to be sorted
 308       * Portions created by vtigerCRM are Copyright (C) vtigerCRM.
 309       * All Rights Reserved..
 310       * Contributor(s): Mike Crowe
 311       */
 312  	function getOrderBy() {
 313          global $log;
 314          $log->debug("Entering getOrderBy() method ...");
 315  
 316          $use_default_order_by = '';
 317          if (PerformancePrefs::getBoolean('LISTVIEW_DEFAULT_SORTING', true)) {
 318              $use_default_order_by = $this->default_order_by;
 319          }
 320  
 321          if (isset($_REQUEST['order_by']))
 322              $order_by = $this->db->sql_escape_string($_REQUEST['order_by']);
 323          else
 324              $order_by = (($_SESSION['EMAILS_ORDER_BY'] != '') ? ($_SESSION['EMAILS_ORDER_BY']) : ($use_default_order_by));
 325  
 326          $log->debug("Exiting getOrderBy method ...");
 327          return $order_by;
 328      }
 329  
 330      // Mike Crowe Mod --------------------------------------------------------
 331  
 332      /** Returns a list of the associated vtiger_users
 333       * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
 334       * All Rights Reserved..
 335       * Contributor(s): ______________________________________..
 336       */
 337  	function get_users($id) {
 338          global $log;
 339          $log->debug("Entering get_users(" . $id . ") method ...");
 340          global $adb;
 341          global $mod_strings;
 342          global $app_strings;
 343  
 344          $id = $_REQUEST['record'];
 345  
 346          $button = '<input title="' . getTranslatedString('LBL_BULK_MAILS') . '" accessykey="F" class="crmbutton small create"
 347                  onclick="this.form.action.value=\"sendmail\";this.form.return_action.value=\"DetailView\";this.form.module.value=\"Emails\";this.form.return_module.value=\"Emails\";"
 348                  name="button" value="' . getTranslatedString('LBL_BULK_MAILS') . '" type="submit">&nbsp;
 349                  <input title="' . getTranslatedString('LBL_BULK_MAILS') . '" accesskey="" tabindex="2" class="crmbutton small edit"
 350                  value="' . getTranslatedString('LBL_SELECT_USER_BUTTON_LABEL') . '" name="Button" language="javascript"
 351                  onclick=\"return window.open("index.php?module=Users&return_module=Emails&action=Popup&popuptype=detailview&select=enable&form=EditView&form_submit=true&return_id=' . $id . '&recordid=' . $id . '","test","width=640,height=520,resizable=0,scrollbars=0");\"
 352                  type="button">';
 353  
 354          $query = 'SELECT vtiger_users.id, vtiger_users.first_name,vtiger_users.last_name, vtiger_users.user_name, vtiger_users.email1, vtiger_users.email2, vtiger_users.secondaryemail , vtiger_users.phone_home, vtiger_users.phone_work, vtiger_users.phone_mobile, vtiger_users.phone_other, vtiger_users.phone_fax from vtiger_users inner join vtiger_salesmanactivityrel on vtiger_salesmanactivityrel.smid=vtiger_users.id and vtiger_salesmanactivityrel.activityid=?';
 355          $result = $adb->pquery($query, array($id));
 356  
 357          $noofrows = $adb->num_rows($result);
 358          $header [] = $app_strings['LBL_LIST_NAME'];
 359  
 360          $header [] = $app_strings['LBL_LIST_USER_NAME'];
 361  
 362          $header [] = $app_strings['LBL_EMAIL'];
 363  
 364          $header [] = $app_strings['LBL_PHONE'];
 365          while ($row = $adb->fetch_array($result)) {
 366  
 367              global $current_user;
 368  
 369              $entries = Array();
 370  
 371              if (is_admin($current_user)) {
 372                  $entries[] = getFullNameFromArray('Users', $row);
 373              } else {
 374                  $entries[] = getFullNameFromArray('Users', $row);
 375              }
 376  
 377              $entries[] = $row['user_name'];
 378              $entries[] = $row['email1'];
 379              if ($email == '')
 380                  $email = $row['email2'];
 381              if ($email == '')
 382                  $email = $row['secondaryemail'];
 383  
 384              $entries[] = $row['phone_home'];
 385              if ($phone == '')
 386                  $phone = $row['phone_work'];
 387              if ($phone == '')
 388                  $phone = $row['phone_mobile'];
 389              if ($phone == '')
 390                  $phone = $row['phone_other'];
 391              if ($phone == '')
 392                  $phone = $row['phone_fax'];
 393  
 394              //Adding Security Check for User
 395  
 396              $entries_list[] = $entries;
 397          }
 398  
 399          if ($entries_list != '')
 400              $return_data = array("header" => $header, "entries" => $entries);
 401  
 402          if ($return_data == null)
 403              $return_data = Array();
 404          $return_data['CUSTOM_BUTTON'] = $button;
 405  
 406          $log->debug("Exiting get_users method ...");
 407          return $return_data;
 408      }
 409  
 410      /**
 411       * Returns a list of the Emails to be exported
 412       */
 413  	function create_export_query(&$order_by, &$where) {
 414          global $log;
 415          global $current_user;
 416          $log->debug("Entering create_export_query(" . $order_by . "," . $where . ") method ...");
 417  
 418          include ("include/utils/ExportUtils.php");
 419  
 420          //To get the Permitted fields query and the permitted fields list
 421          $sql = getPermittedFieldsQuery("Emails", "detail_view");
 422          $fields_list = getFieldsListFromQuery($sql);
 423  
 424          $query = "SELECT $fields_list FROM vtiger_activity
 425              INNER JOIN vtiger_crmentity
 426                  ON vtiger_crmentity.crmid=vtiger_activity.activityid
 427              LEFT JOIN vtiger_users
 428                  ON vtiger_users.id = vtiger_crmentity.smownerid
 429              LEFT JOIN vtiger_seactivityrel
 430                  ON vtiger_seactivityrel.activityid = vtiger_activity.activityid
 431              LEFT JOIN vtiger_contactdetails
 432                  ON vtiger_contactdetails.contactid = vtiger_seactivityrel.crmid
 433              LEFT JOIN vtiger_cntactivityrel
 434                  ON vtiger_cntactivityrel.activityid = vtiger_activity.activityid
 435                  AND vtiger_cntactivityrel.contactid = vtiger_cntactivityrel.contactid
 436              LEFT JOIN vtiger_groups
 437                  ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 438              LEFT JOIN vtiger_salesmanactivityrel
 439                  ON vtiger_salesmanactivityrel.activityid = vtiger_activity.activityid
 440              LEFT JOIN vtiger_emaildetails
 441                  ON vtiger_emaildetails.emailid = vtiger_activity.activityid
 442              LEFT JOIN vtiger_seattachmentsrel
 443                  ON vtiger_activity.activityid=vtiger_seattachmentsrel.crmid
 444              LEFT JOIN vtiger_attachments
 445                  ON vtiger_seattachmentsrel.attachmentsid = vtiger_attachments.attachmentsid";
 446          $query .= getNonAdminAccessControlQuery('Emails', $current_user);
 447          $query .= "WHERE vtiger_activity.activitytype='Emails' AND vtiger_crmentity.deleted=0 ";
 448  
 449          $log->debug("Exiting create_export_query method ...");
 450          return $query;
 451      }
 452  
 453      /**
 454       * Used to releate email and contacts -- Outlook Plugin
 455       */
 456  	function set_emails_contact_invitee_relationship($email_id, $contact_id) {
 457          global $log;
 458          $log->debug("Entering set_emails_contact_invitee_relationship(" . $email_id . "," . $contact_id . ") method ...");
 459          $query = "insert into $this->rel_contacts_table (contactid,activityid) values(?,?)";
 460          $this->db->pquery($query, array($contact_id, $email_id), true, "Error setting email to contact relationship: " . "<BR>$query");
 461          $log->debug("Exiting set_emails_contact_invitee_relationship method ...");
 462      }
 463  
 464      /**
 465       * Used to releate email and salesentity -- Outlook Plugin
 466       */
 467  	function set_emails_se_invitee_relationship($email_id, $contact_id) {
 468          global $log;
 469          $log->debug("Entering set_emails_se_invitee_relationship(" . $email_id . "," . $contact_id . ") method ...");
 470          $query = "insert into $this->rel_serel_table (crmid,activityid) values(?,?)";
 471          $this->db->pquery($query, array($contact_id, $email_id), true, "Error setting email to contact relationship: " . "<BR>$query");
 472          $log->debug("Exiting set_emails_se_invitee_relationship method ...");
 473      }
 474  
 475      /**
 476       * Used to releate email and Users -- Outlook Plugin
 477       */
 478  	function set_emails_user_invitee_relationship($email_id, $user_id) {
 479          global $log;
 480          $log->debug("Entering set_emails_user_invitee_relationship(" . $email_id . "," . $user_id . ") method ...");
 481          $query = "insert into $this->rel_users_table (smid,activityid) values (?,?)";
 482          $this->db->pquery($query, array($user_id, $email_id), true, "Error setting email to user relationship: " . "<BR>$query");
 483          $log->debug("Exiting set_emails_user_invitee_relationship method ...");
 484      }
 485  
 486      // Function to unlink an entity with given Id from another entity
 487  	function unlinkRelationship($id, $return_module, $return_id) {
 488          global $log;
 489  
 490          $sql = 'DELETE FROM vtiger_seactivityrel WHERE activityid=? AND crmid = ?';
 491          $this->db->pquery($sql, array($id, $return_id));
 492  
 493          $sql = 'DELETE FROM vtiger_crmentityrel WHERE (crmid=? AND relmodule=? AND relcrmid=?) OR (relcrmid=? AND module=? AND crmid=?)';
 494          $params = array($id, $return_module, $return_id, $id, $return_module, $return_id);
 495          $this->db->pquery($sql, $params);
 496  
 497          $this->db->pquery('UPDATE vtiger_crmentity SET modifiedtime = ? WHERE crmid = ?', array(date('y-m-d H:i:d'), $id));
 498      }
 499  
 500  	public function getNonAdminAccessControlQuery($module, $user, $scope='') {
 501          require('user_privileges/user_privileges_' . $user->id . '.php');
 502          require('user_privileges/sharing_privileges_' . $user->id . '.php');
 503          $query = ' ';
 504          $tabId = getTabid($module);
 505          if ($is_admin == false && $profileGlobalPermission[1] == 1 && $profileGlobalPermission[2]
 506                  == 1 && $defaultOrgSharingPermission[$tabId] == 3) {
 507              $tableName = 'vt_tmp_u' . $user->id;
 508              $sharingRuleInfoVariable = $module . '_share_read_permission';
 509              $sharingRuleInfo = $sharingRuleInfoVariable;
 510              $sharedTabId = null;
 511              if (!empty($sharingRuleInfo) && (count($sharingRuleInfo['ROLE']) > 0 ||
 512                      count($sharingRuleInfo['GROUP']) > 0)) {
 513                  $tableName = $tableName . '_t' . $tabId;
 514                  $sharedTabId = $tabId;
 515              }
 516              $this->setupTemporaryTable($tableName, $sharedTabId, $user, $current_user_parent_role_seq, $current_user_groups);
 517              $query = " INNER JOIN $tableName $tableName$scope ON $tableName$scope.id = " .
 518                      "vtiger_crmentity$scope.smownerid ";
 519          }
 520          return $query;
 521      }
 522  
 523  	protected function setupTemporaryTable($tableName, $tabId, $user, $parentRole, $userGroups) {
 524          $module = null;
 525          if (!empty($tabId)) {
 526              $module = getTabname($tabId);
 527          }
 528          $query = $this->getNonAdminAccessQuery($module, $user, $parentRole, $userGroups);
 529          $query = "create temporary table IF NOT EXISTS $tableName(id int(11) primary key, shared int(1) default 0) ignore ".$query;
 530          $db = PearDatabase::getInstance();
 531          $result = $db->pquery($query, array());
 532          if(is_object($result)) {
 533              return true;
 534          }
 535          return false;
 536      }
 537  
 538      /*
 539      * Function to get the relation tables for related modules
 540      * @param - $secmodule secondary module name
 541      * returns the array with table names and fieldnames storing relations between module and this module
 542      */
 543  	function setRelationTables($secmodule) {
 544          $rel_tables = array (
 545                  "Leads" => array("vtiger_seactivityrel" => array("activityid", "crmid"), "vtiger_activity" => "activityid"),
 546                  "Vendors" => array("vtiger_seactivityrel" => array("activityid", "crmid"), "vtiger_activity" => "activityid"),
 547                  "Contacts" => array("vtiger_seactivityrel" => array("activityid", "crmid"), "vtiger_activity" => "activityid"),
 548                  "Accounts" => array("vtiger_seactivityrel" => array("activityid", "crmid"), "vtiger_activity" => "activityid"),
 549          );
 550          return $rel_tables[$secmodule];
 551      }
 552  
 553      /*
 554      * Function to get the secondary query part of a report
 555      * @param - $module primary module name
 556      * @param - $secmodule secondary module name
 557      * returns the query string formed on fetching the related data for report for secondary module
 558      */
 559  	function generateReportsSecQuery($module, $secmodule, $queryPlanner){
 560          $focus = CRMEntity::getInstance($module);
 561          $matrix = $queryPlanner->newDependencyMatrix();
 562  
 563          $matrix->setDependency("vtiger_crmentityEmails",array("vtiger_groupsEmails","vtiger_usersEmails","vtiger_lastModifiedByEmails"));
 564          $matrix->setDependency("vtiger_activity",array("vtiger_crmentityEmails","vtiger_email_track"));
 565  
 566          if (!$queryPlanner->requireTable('vtiger_activity', $matrix)) {
 567              return '';
 568          }
 569  
 570          $query = $this->getRelationQuery($module, $secmodule, "vtiger_activity","activityid", $queryPlanner);
 571          if ($queryPlanner->requireTable("vtiger_crmentityEmails")){
 572              $query .= " LEFT JOIN vtiger_crmentity AS vtiger_crmentityEmails ON vtiger_crmentityEmails.crmid=vtiger_activity.activityid and vtiger_crmentityEmails.deleted = 0";
 573          }
 574          if ($queryPlanner->requireTable("vtiger_groupsEmails")){
 575              $query .= " LEFT JOIN vtiger_groups AS vtiger_groupsEmails ON vtiger_groupsEmails.groupid = vtiger_crmentityEmails.smownerid";
 576          }
 577          if ($queryPlanner->requireTable("vtiger_usersEmails")){
 578              $query .= " LEFT JOIN vtiger_users AS vtiger_usersEmails ON vtiger_usersEmails.id = vtiger_crmentityEmails.smownerid";
 579          }
 580          if ($queryPlanner->requireTable("vtiger_lastModifiedByEmails")){
 581              $query .= " LEFT JOIN vtiger_users AS vtiger_lastModifiedByEmails ON vtiger_lastModifiedByEmails.id = vtiger_crmentityEmails.modifiedby and vtiger_seactivityreltmpEmails.activityid = vtiger_activity.activityid";
 582          }
 583          if ($queryPlanner->requireTable("vtiger_createdbyEmails")){
 584              $query .= " left join vtiger_users as vtiger_createdbyEmails on vtiger_createdbyEmails.id = vtiger_crmentityEmails.smcreatorid and vtiger_seactivityreltmpEmails.activityid = vtiger_activity.activityid";
 585          }
 586          if ($queryPlanner->requireTable("vtiger_email_track")){
 587              $query .= " LEFT JOIN vtiger_email_track ON vtiger_email_track.mailid = vtiger_activity.activityid and vtiger_email_track.crmid = ".$focus->table_name.".".$focus->table_index;
 588          }
 589          return $query;
 590      }
 591  
 592      /*
 593       * Function to store the email access count value of emails in 'vtiger_email_track' table
 594       * @param - $mailid
 595       */
 596  	function setEmailAccessCountValue($mailid) {
 597          global $adb;
 598          $successIds = array();
 599          $result = $adb->pquery('SELECT idlists FROM vtiger_emaildetails WHERE emailid=?', array($mailid));
 600          $idlists = $adb->query_result($result,0,'idlists');
 601          $idlistsArray = explode('|', $idlists);
 602  
 603          for ($i=0; $i<(count($idlistsArray)-1); $i++) {
 604              $crmid = explode("@",$idlistsArray[$i]);
 605              array_push($successIds, $crmid[0]);
 606          }
 607          $successIds = array_unique($successIds);
 608          sort($successIds);
 609          for ($i=0; $i<count($successIds); $i++) {
 610              $adb->pquery("INSERT INTO vtiger_email_track(crmid, mailid,  access_count) VALUES(?,?,?)", array($successIds[$i], $mailid, 0));
 611          }
 612      }
 613  
 614  }
 615  
 616  //added for attach the generated pdf with email
 617  function pdfAttach($obj, $module, $file_name, $id) {
 618      global $log;
 619      $log->debug("Entering into pdfAttach() method.");
 620  
 621      global $adb, $current_user;
 622      global $upload_badext;
 623      $date_var = date('Y-m-d H:i:s');
 624  
 625      $ownerid = $obj->column_fields['assigned_user_id'];
 626      if (!isset($ownerid) || $ownerid == '')
 627          $ownerid = $current_user->id;
 628  
 629      $current_id = $adb->getUniqueID("vtiger_crmentity");
 630  
 631      $upload_file_path = decideFilePath();
 632  
 633      //Copy the file from temporary directory into storage directory for upload
 634      $source_file_path = "storage/" . $file_name;
 635      $status = copy($source_file_path, $upload_file_path . $current_id . "_" . $file_name);
 636      //Check wheather the copy process is completed successfully or not. if failed no need to put entry in attachment table
 637      if ($status) {
 638          $query1 = "insert into vtiger_crmentity (crmid,smcreatorid,smownerid,setype,description,createdtime,modifiedtime) values(?,?,?,?,?,?,?)";
 639          $params1 = array($current_id, $current_user->id, $ownerid, $module . " Attachment", $obj->column_fields['description'], $adb->formatDate($date_var, true), $adb->formatDate($date_var, true));
 640          $adb->pquery($query1, $params1);
 641  
 642          $query2 = "insert into vtiger_attachments(attachmentsid, name, description, type, path) values(?,?,?,?,?)";
 643          $params2 = array($current_id, $file_name, $obj->column_fields['description'], 'pdf', $upload_file_path);
 644          $result = $adb->pquery($query2, $params2);
 645  
 646          $query3 = 'insert into vtiger_seattachmentsrel values(?,?)';
 647          $adb->pquery($query3, array($id, $current_id));
 648  
 649          // Delete the file that was copied
 650          checkFileAccessForDeletion($source_file_path);
 651          unlink($source_file_path);
 652  
 653          return true;
 654      } else {
 655          $log->debug("pdf not attached");
 656          return false;
 657      }
 658  }
 659  
 660  //this function check email fields profile permission as well as field access permission
 661  function emails_checkFieldVisiblityPermission($fieldname, $mode='readonly') {
 662      global $current_user;
 663      $ret = getFieldVisibilityPermission('Emails', $current_user->id, $fieldname, $mode);
 664      return $ret;
 665  }
 666  
 667  ?>


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