[Vtigercrm-developers] Redone data/CRMEntity.php insertIntoAttachment function for 4.2.x

Dennis Grant dgrant at accuratetechnologies.com
Thu May 11 08:39:12 PDT 2006


Attached is a revised version of the insertIntoAttachment function from
data/CRMEntity.php

Share and enjoy!

DG


------8<-----SNIP------8<-------SNIP-----------

function insertIntoAttachment($id,$module)
  {

        // Heavily modified by DG 10-11 May 2006 to:
        //   1)  Fix inexplicable behaviour of sticking text of email
into the description fields of everything
        //   2)  Attachments to Emails now show up as attachments to
Contact and Account

    $date_var = date('YmdHis');
    global $current_user;
    global $adb;
    global $root_directory;
    global $vtlog;  // Activate logging

    $ownerid = $this->column_fields['assigned_user_id'];
    $adb->println("insertattach ownerid=".$ownerid." mod=".$module);
    $adb->println($this->column_fields);

        if(!isset($ownerid) || $ownerid=='')            $ownerid =
$current_user->id;
    $uploaddir = $root_directory ."/test/upload/" ;// set this to
wherever
    $binFile = $_FILES['filename']['name'];
    $filename = basename($binFile);
    $filetype= $_FILES['filename']['type'];
    $filesize = $_FILES['filename']['size'];

    if($binFile != '')
    {
 
if(move_uploaded_file($_FILES["filename"]["tmp_name"],$uploaddir.$_FILES
["filename"]["name"]))
      {
        //                      $binFile = $_FILES['filename']['name'];
        //                      $filename = basename($binFile);
        //                      $filetype= $_FILES['filename']['type'];
        //                      $filesize = $_FILES['filename']['size'];
        if($filesize != 0)
        {
          $data = base64_encode(fread(fopen($uploaddir.$binFile, "r"),
$filesize));
        }
      }
      $current_id = $adb->getUniqueID("crmentity");

      if($module=='Emails') {
                $idname='emailid';
                $tablename='emails';
                $descname='description';

                // DG 10 May 2006
                // Try and get the subject for the email instead, and
put that into the Description
                // This fixes the ludicrous behaviour that the text of
the email was duplicated in triplicate
                // in the description fields of the crmentity and the
attachment
                $dg_subject = $this->column_fields['subject'];

                if ($dg_subject) {
                        $vtlog->logthis("DGDEBUG: Found Subject:
".$dg_subject,'warn');
                }
                else {
                        $vtlog->logthis("DGDEBUG: No subject match in
activity for activityid = ".$id,'warn');
}

        }
        else {
                $idname='notesid';
                $tablename='notes';
                $descname='notecontent';
        }

        $sql="update ".$tablename." set filename='".$filename."' where
".$idname."=".$id;
      $adb->query($sql);

        // DG 10 May 2006
        // If we don't get a subject for the email, preserve the old
behavior
        // Should never ever happen... but I like to play it safe
        if ($dg_subject) {
                $sql1 = "insert into crmentity
(crmid,smcreatorid,smownerid,setype,description,createdtime,modifiedtime
) values(".$current_id.",".$current_user->id.",".$ownerid.",'".$module."
Attachment','".$dg_subject."',".$adb->formatString("crmentity","createdt
ime",$date_var).",".$adb->formatString("crmentity","modifiedtime",$date_
var).")";
        }
        else {
                $sql1 = "insert into crmentity
(crmid,smcreatorid,smownerid,setype,description,createdtime,modifiedtime
) values(".$current_id.",".$current_user->id.",".$ownerid.",'".$module."
Attachment','".$this->column_fields['description']."',".$adb->formatStri
ng("crmentity","createdtime",$date_var).",".$adb->formatString("crmentit
y","modifiedtime",$date_var).")";
        }

      $adb->query($sql1);

      //$this->id = $current_id;

        // DG 10 May 2006
        // Same deal - use the email subject if we have it, otherwise
use the previous brain-dead behaviour
        if ($dg_subject) {
                $sql2="insert into attachments(attachmentsid, name,
description, type, attachmentsize, attachmentcontents)
values(".$current_id.",'".$filename."','".$dg_subject."','".$filetype."'
,'".$filesize."',".$adb->getEmptyBlob().")";
        }
        else {
                $sql2="insert into attachments(attachmentsid, name,
description, type, attachmentsize, attachmentcontents)
values(".$current_id.",'".$filename."','".$this->column_fields[$descname
]."','".$filetype."','".$filesize."',".$adb->getEmptyBlob().")";
        }

      $result=$adb->query($sql2);

      if($result!=false)
        $result =
$adb->updateBlob('attachments','attachmentcontents',"attachmentsid='".$c
urrent_id."' and name='".$filename."'",$data);

     if($_REQUEST['mode'] == 'edit')
     {
        if($id != '' && $_REQUEST['fileid'] != '')
        {
                $delquery = 'delete from seattachmentsrel where crmid =
'.$id.' and attachmentsid = '.$_REQUEST['fileid'];
                $adb->query($delquery);
        }
     }
        if($module == 'Notes')
        {
                $query = "delete from seattachmentsrel where crmid =
".$id;
$adb->query($query);
        }
        // DG 11 May 2006
        // My customers want email attachments to show up on Contacts
and Accounts
        // so look them up and stick them in the appropriate places
        if (($module == 'Emails') && ($dg_subject)) {

                // Look up the Contact associated with the Email
                $dg_sql = "select crmid from seactivityrel where
activityid = ".$id;
                $dg_result = $adb->query($dg_sql);
                $dg_contactid =
$adb->query_result($dg_result,0,"crmid");
                $vtlog->logthis("DGDEBUG: Found matching Contact:
".$dg_contactid." for Email: ".$id,'warn');

                // If we found the Contact, get the Account too
                if ($dg_contactid) {
                        $dg_sql = "select accountid from contactdetails
where contactid = ".$dg_contactid;
                        $dg_result = $adb->query($dg_sql);
                        $dg_accountid =
$adb->query_result($dg_result,0,"accountid");
                        $vtlog->logthis("DGDEBUG: Found matching
Account: ".$dg_accountid." for Contact: ".$dg_contactid,'warn');
                }
        }

        // DG 11 May 2006
        // At this point, if everything went right, we should have a
$dg_contactid and a $dg_accountid, plus the $id and $currentid
        // So we add the base first, then try each of the others

        // The email->attachment
        $vtlog->logthis("DGDEBUG: inserting into seattachmentsrel from
data/CRMEntity.php 2",'warn');
        $sql3='insert into seattachmentsrel
values('.$id.','.$current_id.')';
        $adb->query($sql3);

        // The Contact->attachment
        if ($dg_contactid) {
                $dg_sql = 'insert into seattachmentsrel
values('.$dg_contactid.','.$current_id.')';
                $adb->query($dg_sql);
                $vtlog->logthis("DGDEBUG: inserting Contact:
".$dg_contactid." for attachment ".$current_id." into seattachmentsrel
from  data/CRMEntity.php 2",'warn');
        }

        // The Account->attachment
        if ($dg_accountid) {
                $dg_sql = 'insert into seattachmentsrel
values('.$dg_accountid.','.$current_id.')';
                $adb->query($dg_sql);
                $vtlog->logthis("DGDEBUG: inserting Account:
".$dg_accountid." for attachment ".$current_id." into seattachmentsrel
from  data/CRMEntity.php 2",'warn');
        }

    }
  }




More information about the vtigercrm-developers mailing list