YetiForceCompany / YetiForceCRM

Our team created for you one of the most innovative CRM systems that supports mainly business processes and allows for customization according to your needs. Be ahead of your competition and implement YetiForce!
https://yetiforce.com
Other
1.73k stars 748 forks source link

Custom uitype no longer works due to changes in save.php #4885

Closed PercyP closed 7 years ago

PercyP commented 7 years ago

I updated from v4.0 to 4.2 and have spent a few days trying to get a custom uitype to work (created by a 3rd party). I have just one outstanding problem with it. I have to add some code to the /modules/vtiger/actions/save.php but when I do, I cannot save any module data. This worked perfectly on version 4.0. The code I am adding is:

`//added by jayant for signature :start---------------Contacts // //isset($request->get('jbase64')) && if( !empty($request->get('imagedata')) ) { $currentUserModel = Users_Record_Model::getCurrentUserModel();
foreach($request->get('imagedata') as $fieldname => $imagedata ){ if(!empty(trim($imagedata))){ $data = $imagedata;
$uploadPath = $this->decideFilePath();

                list($type, $data) = explode(';', $data);
                list(, $data)      = explode(',', $data);
                $data = base64_decode($data);

                $userId = $recordModel->get('assigned_user_id');

                $fileName=$userId.'_'.$recordModel->getId();

                $relatedRecordId = $recordModel->getId();

                $attachid = $db->getUniqueId('vtiger_crmentity');

                if(file_put_contents($uploadPath.$attachid . "_" . $fieldname.'_'.$fileName.'.png',$data)!== false)
                {
                    $description = $fileName;
                    $date_var = $db->formatDate(date('YmdHis'), true);
                    $usetime = $db->formatDate($date_var, true);
                    $user_id = $currentUserModel->getId();

                    $db->pquery("INSERT INTO vtiger_crmentity(crmid, smcreatorid, smownerid,
                    modifiedby, setype, description, createdtime, modifiedtime, presence, deleted)
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                            array($attachid, $userId, $userId, $userId, "eSignature Attachment", $description, $usetime, $usetime, 1, 0));  
                     $file_name = $fieldname.'_'.$fileName.'.png';

                    $insert = "INSERT INTO vtiger_attachments (attachmentsid, name, description, type, path) VALUES ($attachid, '".$file_name."' ,'".$description."' ,'image/png', '".$uploadPath."')"; 

                    $db->pquery($insert,array());   
                    $attachmentId=$attachid;
                }

                // we need tabid 
                //SELECT tabid FROM `vtiger_tab` where name = ''
                $tabid_data = $db->pquery("SELECT tabid FROM `vtiger_tab` where name = ?",array($request->get('module'))); 
                $tabid = $db->fetch_array($tabid_data);

                //SELECT tablename,columnname FROM `vtiger_field` where uitype = '979' and tabid = 6
                $table_data = $db->pquery("SELECT tablename FROM `vtiger_field` where uitype = '979' and tabid = ? and columnname = ?",array($tabid['tabid'],$fieldname));  

                $tabledata = $db->fetch_array($table_data);

                //getting firstcoloum of cf any table
                 //SELECT entityidfield  FROM `vtiger_entityname`  where tabid = 6 and modulename = 'Accounts'

                $entityidfield = $db->pquery("SELECT entityidfield  FROM `vtiger_entityname`  where tabid = ? and modulename = ? ",array($tabid['tabid'],$request->get('module'))); 
                $entityidfield = $db->fetch_array($entityidfield);
                $eid = $entityidfield['entityidfield'];

                $query = "UPDATE ". $tabledata['tablename'] ." SET ".$fieldname." = ?  WHERE    ".$eid ." = ?";

                //$db->pquery($query,array($fieldname.'_'.$fileName.'.png',$recordModel->getId()));

                $db->pquery($query,array($uploadPath.$attachid.'_'.$fieldname.'_'.$fileName.'.png',$recordModel->getId()));

                // Link file attached to document
                $attachmentrel = "INSERT INTO vtiger_seattachmentsrel(crmid, attachmentsid) VALUES($relatedRecordId,$attachmentId)";
                $db->pquery($attachmentrel,array()); 
            }
        }
        // end of esign
    } 
        // delete esign image
    if($request->get('signDeleted')) {
        $imageIds = $request->get('imageid');
        foreach($imageIds as $imageId) {                 
            $status = $recordModel->deleteSign($imageId);
        }
    } `

Which I insert at line 120. So my Save.php looks like this:

`<?php /* +***

class Vtiger_Save_Action extends Vtiger_Action_Controller {

/**
 * @var Vtiger_Record_Model
 */
protected $record = false;

public function checkPermission(\App\Request $request)
{
    $moduleName = $request->getModule();
    $record = $request->get('record');

    if (!empty($record)) {
        $recordModel = $this->record ? $this->record : Vtiger_Record_Model::getInstanceById($record, $moduleName);
        if (!$recordModel->isEditable()) {
            throw new \Exception\NoPermittedToRecord('LBL_PERMISSION_DENIED');
        }
    } else {
        $recordModel = Vtiger_Record_Model::getCleanInstance($moduleName);
        if (!$recordModel->isCreateable()) {
            throw new \Exception\NoPermittedToRecord('LBL_PERMISSION_DENIED');
        }
    }
}

public function preProcess(\App\Request $request)
{
    parent::preProcess($request);
    if (App\Session::has('baseUserId') && !empty(App\Session::get('baseUserId'))) {
        $baseUserId = App\Session::get('baseUserId');
        $user = new Users();
        $currentUser = $user->retrieveCurrentUserInfoFromFile($baseUserId);
        vglobal('current_user', $currentUser);
        App\User::setCurrentUserId($baseUserId);
    }
}

public function preProcessAjax(\App\Request $request)
{
    parent::preProcessAjax($request);
    if (App\Session::has('baseUserId') && !empty(App\Session::get('baseUserId'))) {
        $baseUserId = App\Session::get('baseUserId');
        $user = new Users();
        $currentUser = $user->retrieveCurrentUserInfoFromFile($baseUserId);
        vglobal('current_user', $currentUser);
        App\User::setCurrentUserId($baseUserId);
    }
}

public function process(\App\Request $request)
{
    $recordModel = $this->saveRecord($request);
    if ($request->get('relationOperation')) {
        $parentModuleName = $request->get('sourceModule');
        $parentRecordId = $request->get('sourceRecord');
        $parentRecordModel = Vtiger_Record_Model::getInstanceById($parentRecordId, $parentModuleName);
        $loadUrl = $parentRecordModel->getDetailViewUrl();
    } else if ($request->get('returnToList')) {
        $loadUrl = $recordModel->getModule()->getListViewUrl();
    } else {
        $loadUrl = $recordModel->getDetailViewUrl();
    }
    header("Location: $loadUrl");
    } 
/**
 * Function to save record
 * @param \App\Request $request - values of the record
 * @return Vtiger_Record_Model - record Model of saved record
 */
public function saveRecord(\App\Request $request)
{                           
    $recordModel = $this->getRecordModelFromRequest($request);
    $recordModel->save();
    if ($request->get('relationOperation')) {
        $parentModuleName = $request->get('sourceModule');
        $parentModuleModel = Vtiger_Module_Model::getInstance($parentModuleName);
        $parentRecordId = $request->get('sourceRecord');
        $relatedModule = $recordModel->getModule();
        $relatedRecordId = $recordModel->getId();

        $relationModel = Vtiger_Relation_Model::getInstance($parentModuleModel, $relatedModule);
        if ($relationModel) {
            $relationModel->addRelation($parentRecordId, $relatedRecordId);
        }

//added by jayant for signature :start---------------Contacts // //isset($request->get('jbase64')) && if( !empty($request->get('imagedata')) ) { $currentUserModel = Users_Record_Model::getCurrentUserModel();
foreach($request->get('imagedata') as $fieldname => $imagedata ){ if(!empty(trim($imagedata))){ $data = $imagedata;
$uploadPath = $this->decideFilePath();

                list($type, $data) = explode(';', $data);
                list(, $data)      = explode(',', $data);
                $data = base64_decode($data);

                $userId = $recordModel->get('assigned_user_id');

                $fileName=$userId.'_'.$recordModel->getId();

                $relatedRecordId = $recordModel->getId();

                $attachid = $db->getUniqueId('vtiger_crmentity');

                if(file_put_contents($uploadPath.$attachid . "_" . $fieldname.'_'.$fileName.'.png',$data)!== false)
                {
                    $description = $fileName;
                    $date_var = $db->formatDate(date('YmdHis'), true);
                    $usetime = $db->formatDate($date_var, true);
                    $user_id = $currentUserModel->getId();

                    $db->pquery("INSERT INTO vtiger_crmentity(crmid, smcreatorid, smownerid,
                    modifiedby, setype, description, createdtime, modifiedtime, presence, deleted)
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                            array($attachid, $userId, $userId, $userId, "eSignature Attachment", $description, $usetime, $usetime, 1, 0));  
                     $file_name = $fieldname.'_'.$fileName.'.png';

                    $insert = "INSERT INTO vtiger_attachments (attachmentsid, name, description, type, path) VALUES ($attachid, '".$file_name."' ,'".$description."' ,'image/png', '".$uploadPath."')"; 

                    $db->pquery($insert,array());   
                    $attachmentId=$attachid;
                }

                // we need tabid 
                //SELECT tabid FROM `vtiger_tab` where name = ''
                $tabid_data = $db->pquery("SELECT tabid FROM `vtiger_tab` where name = ?",array($request->get('module'))); 
                $tabid = $db->fetch_array($tabid_data);

                //SELECT tablename,columnname FROM `vtiger_field` where uitype = '979' and tabid = 6
                $table_data = $db->pquery("SELECT tablename FROM `vtiger_field` where uitype = '979' and tabid = ? and columnname = ?",array($tabid['tabid'],$fieldname));  

                $tabledata = $db->fetch_array($table_data);

                //getting firstcoloum of cf any table
                 //SELECT entityidfield  FROM `vtiger_entityname`  where tabid = 6 and modulename = 'Accounts'

                $entityidfield = $db->pquery("SELECT entityidfield  FROM `vtiger_entityname`  where tabid = ? and modulename = ? ",array($tabid['tabid'],$request->get('module'))); 
                $entityidfield = $db->fetch_array($entityidfield);
                $eid = $entityidfield['entityidfield'];

                $query = "UPDATE ". $tabledata['tablename'] ." SET ".$fieldname." = ?  WHERE    ".$eid ." = ?";

                //$db->pquery($query,array($fieldname.'_'.$fileName.'.png',$recordModel->getId()));

                $db->pquery($query,array($uploadPath.$attachid.'_'.$fieldname.'_'.$fileName.'.png',$recordModel->getId()));

                // Link file attached to document
                $attachmentrel = "INSERT INTO vtiger_seattachmentsrel(crmid, attachmentsid) VALUES($relatedRecordId,$attachmentId)";
                $db->pquery($attachmentrel,array()); 
            }
        }
        // end of esign
    } 

    // delete esign image
    if($request->get('signDeleted')) {
        $imageIds = $request->get('imageid');
        foreach($imageIds as $imageId) {                 
            $status = $recordModel->deleteSign($imageId);
        }
    }                     
    }
if ($request->get('imgDeleted')) {
        $imageIds = $request->get('imageid');
        foreach ($imageIds as &$imageId) {
            $recordModel->deleteImage($imageId);
        }
    }
    return $recordModel;
}

/**
 * Function to get the record model based on the request parameters
 * @param \App\Request $request
 * @return Vtiger_Record_Model or Module specific Record Model instance
 */
protected function getRecordModelFromRequest(\App\Request $request)
{
    $moduleName = $request->getModule();
    $recordId = $request->get('record');
    if (!empty($recordId)) {
        $recordModel = $this->record ? $this->record : Vtiger_Record_Model::getInstanceById($recordId, $moduleName);
    } else {
        $recordModel = $this->record ? $this->record : Vtiger_Record_Model::getCleanInstance($moduleName);
    }
    $fieldModelList = $recordModel->getModule()->getFields();
    foreach ($fieldModelList as $fieldName => &$fieldModel) {
        if (!$fieldModel->isWritable()) {
            continue;
        }
        if ($request->has($fieldName) && $fieldModel->get('uitype') === 300) {
            $recordModel->set($fieldName, $request->getForHtml($fieldName, null));
        } elseif ($request->has($fieldName)) {
            $recordModel->set($fieldName, $fieldModel->getUITypeModel()->getDBValue($request->get($fieldName, null), $recordModel));
        } elseif ($recordModel->isNew()) {
            $defaultValue = $fieldModel->getDefaultFieldValue();
            if ($defaultValue !== '') {
                $recordModel->set($fieldName, $defaultValue);
            }
        }
    }
    return $recordModel;
}

public function validateRequest(\App\Request $request)
{
    return $request->validateWriteAccess();
}

}
` In phperror.php it shows the following errors:

`[14-Aug-2017 14:33:48 Europe/London] PHP Fatal error: Uncaught Error: Call to undefined method Vtiger_Record_Model::getJsignDetails() in /home/mysite/public_html/crm/cache/templates_c/basic/e2549a66197684cedb5de36a611b87cefbe2ae9c_0.file.JsignDetailView.tpl.php:27 Stack trace:

0 /home/mysite/public_html/crm/vendor/smarty/smarty/libs/sysplugins/smarty_template_resource_base.php(128): content_5991a408a8d105_65482006(Object(Smarty_Internal_Template))

1 /home/mysite/public_html/crm/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php(172): Smarty_Template_Resource_Base->getRenderedTemplateCode(Object(Smarty_Internal_Template))

2 /home/mysite/public_html/crm/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php(206): Smarty_Template_Compiled->render(Object(Smarty_Internal_Template))

3 /home/mysite/public_html/crm/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php(372): Smarty_Internal_Template->render()

4 /home/mysite/public_html/crm/cache/templates_c/basic/67c73e333f79b8d14c83b09c844e1578ec132a80_0 in /home/mysite/public_html/crm/cache/templates_c/basic/e2549a66197684cedb5de36a611b87cefbe2ae9c_0.file.JsignDetailView.tpl.php on line 27

[14-Aug-2017 19:28:06 Europe/London] PHP Warning: Declaration of Vtiger_Jsign_UIType::getDisplayValue($value) should be compatible with Vtiger_Base_UIType::getDisplayValue($value, $record = false, $recordInstance = false, $rawText = false) in /home/mysite/public_html/crm/modules/Vtiger/uitypes/Jsign.php on line 32 [14-Aug-2017 20:22:10 Europe/London] PHP Parse error: syntax error, unexpected 'protected' (T_PROTECTED) in /home/mysite/public_html/crm/modules/Vtiger/actions/Save.php on line 221 ` The line at 221 (in save.php) is:

protected function getRecordModelFromRequest(\App\Request $request)

With my added code none of the modules save anymore, I get error 500, if I take it out it works, so it is obviously something in my code that needs changing. I noticed you have replaced 'vtiger' with 'App' could this be my problem?

Any guidance would be most appreciated

Kind regards

PercyP commented 7 years ago

Hi, I have managed to get it working by removing protected from this line:

protected function getRecordModelFromRequest(\App\Request $request)

Will this have any impact on anything else?

PercyP commented 7 years ago

Hi,

in the end I had to go back to the developer and get the code changes made. It now works :-D