Skip to content
Snippets Groups Projects
CRMEntity.php 129 KiB
Newer Older
Prasad's avatar
Prasad committed
<?php
/*********************************************************************************
 * The contents of this file are subject to the SugarCRM Public License Version 1.1.2
 * ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is:  SugarCRM Open Source
 * The Initial Developer of the Original Code is SugarCRM, Inc.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.;
 * All Rights Reserved.
 * Contributor(s): ______________________________________.
 ********************************************************************************/
/*********************************************************************************
 * $Header: /advent/projects/wesat/vtiger_crm/vtigercrm/data/CRMEntity.php,v 1.16 2005/04/29 04:21:31 mickie Exp $
 * Description:  Defines the base class for all data entities used throughout the
 * application.  The base class including its methods and variables is designed to
 * be overloaded with module-specific methods and variables particular to the
 * module's base entity class.
 ********************************************************************************/

include_once('config.php');
require_once('include/logging.php');
require_once('data/Tracker.php');
require_once('include/utils/utils.php');
require_once('include/utils/UserInfoUtil.php');
require_once("include/Zend/Json.php");
Prasad's avatar
Prasad committed
require_once 'include/RelatedListView.php';
Prasad's avatar
Prasad committed

#[\AllowDynamicProperties]
Prasad's avatar
Prasad committed
class CRMEntity {

	var $ownedby;
Prasad's avatar
Prasad committed
	var $recordSource = 'CRM';
Prasad's avatar
Prasad committed

	/**
	 * Detect if we are in bulk save mode, where some features can be turned-off
	 * to improve performance.
	 */
	static function isBulkSaveMode() {
		global $VTIGER_BULK_SAVE_MODE;
		if (isset($VTIGER_BULK_SAVE_MODE) && $VTIGER_BULK_SAVE_MODE) {
			return true;
		}
		return false;
	}

	static function getInstance($module) {
		$modName = $module;
		if ($module == 'Calendar' || $module == 'Events') {
			$module = 'Calendar';
			$modName = 'Activity';
		}
		// File access security check
		if (!class_exists($modName)) {
			checkFileAccessForInclusion("modules/$module/$modName.php");
			require_once("modules/$module/$modName.php");
		}
		$focus = new $modName();
		$focus->moduleName = $module;
Prasad's avatar
Prasad committed
		$focus->column_fields = new TrackableObject();
		$focus->column_fields = getColumnFields($module);
		if (method_exists($focus, 'initialize')) $focus->initialize();
Prasad's avatar
Prasad committed
		return $focus;
	}

Prasad's avatar
Prasad committed
	/**
	 * Function which indicates whether to chagne the modified time or not while saving
	 */
	static function isTimeStampNoChangeMode(){
		global $VTIGER_TIMESTAMP_NO_CHANGE_MODE;
		if (isset($VTIGER_TIMESTAMP_NO_CHANGE_MODE) && $VTIGER_TIMESTAMP_NO_CHANGE_MODE) {
			return true;
		}
		return false;
	}

	/**
	 * Function which will used to initialize object properties 
	 */
	function initialize() {
		$moduleName = $this->moduleName;
		$moduleModel = Vtiger_Module_Model::getInstance($moduleName);
		if($moduleModel && !$moduleModel->isEntityModule()) {
			return;
		}

		$userSpecificTableIgnoredModules = array('SMSNotifier', 'PBXManager', 'ModComments');
Prasad's avatar
Prasad committed
		if(in_array($moduleName, $userSpecificTableIgnoredModules)) return;

		$userSpecificTable = Vtiger_Functions::getUserSpecificTableName($moduleName);
		if(!in_array($userSpecificTable, $this->tab_name)) {
			$this->tab_name[] = $userSpecificTable;
			$this->tab_name_index [$userSpecificTable] = 'recordid';
		}
	}

Prasad's avatar
Prasad committed
	function saveentity($module, $fileid = '') {
		global $current_user, $adb; //$adb added by raju for mass mailing
		$insertion_mode = $this->mode;

		$columnFields = $this->column_fields;
		$anyValue = false;
		foreach ($columnFields as $value) {
			if(!empty($value)) {
				$anyValue = true;
				break;
			}
		}
		if(!$anyValue) {
			die("<center>" .getTranslatedString('LBL_MANDATORY_FIELD_MISSING')."</center>");
		}

Prasad's avatar
Prasad committed
		// added to support files transformation for file upload fields like uitype 69, 
		if(!empty($_FILES) && php7_count($_FILES)) {
Prasad's avatar
Prasad committed
			$_FILES = Vtiger_Util_Helper::transformUploadedFiles($_FILES, true);
		}
Prasad's avatar
Prasad committed

Prasad's avatar
Prasad committed
		$this->db->startTransaction();
Prasad's avatar
Prasad committed
		foreach ($this->tab_name as $table_name) {
			if ($table_name == "vtiger_crmentity") {
				$this->insertIntoCrmEntity($module, $fileid);
			} else {
				$this->insertIntoEntityTable($table_name, $module, $fileid);
			}
		}
Prasad's avatar
Prasad committed
		$columnFields->restartTracking();
Prasad's avatar
Prasad committed
		//Calling the Module specific save code
		$this->save_module($module);

		$this->db->completeTransaction();

		// vtlib customization: Hook provide to enable generic module relation.
		if ( (isset($_REQUEST['createmode']) && $_REQUEST['createmode'] == 'link') && 
			!isset($_REQUEST['__linkcreated'])) {
Prasad's avatar
Prasad committed
			$_REQUEST['__linkcreated'] = true;
Prasad's avatar
Prasad committed
			$for_module = vtlib_purify($_REQUEST['return_module']);
			$for_crmid = vtlib_purify($_REQUEST['return_id']);
			$with_module = $module;
			$with_crmid = $this->id;

			$on_focus = CRMEntity::getInstance($for_module);

			if ($for_module && $for_crmid && $with_module && $with_crmid) {
				relateEntities($on_focus, $for_module, $for_crmid, $with_module, $with_crmid);
			}
		}
		// END
	}

	/**
Prasad's avatar
Prasad committed
	 * This function is used to upload the attachment in the server and save that attachment information in db.
	 * @param int $id  - entity id to which the file to be uploaded
	 * @param string $module  - the current module name
	 * @param array $file_details  - array which contains the file information(name, type, size, tmp_name and error)
	 * return void
Prasad's avatar
Prasad committed
	 */
Prasad's avatar
Prasad committed
	function uploadAndSaveFile($id, $module, $file_details, $attachmentType='Attachment') {
Prasad's avatar
Prasad committed
		global $log;
		$log->debug("Entering into uploadAndSaveFile($id,$module,$file_details) method.");

		global $adb, $current_user;
		global $upload_badext;

		$date_var = date("Y-m-d H:i:s");

		//to get the owner id
		$ownerid = $this->column_fields['assigned_user_id'];
		if (!isset($ownerid) || $ownerid == '')
			$ownerid = $current_user->id;

		if (isset($file_details['original_name']) && $file_details['original_name'] != null) {
			$file_name = $file_details['original_name'];
		} else {
			$file_name = $file_details['name'];
		}
Prasad's avatar
Prasad committed
		//only images are allowed for Image Attachmenttype
		$mimeType = vtlib_mime_content_type($file_details['tmp_name']);
		$mimeTypeContents = explode('/', $mimeType);
		// For contacts and products we are sending attachmentType as value
		if ($attachmentType == 'Image' || ($file_details['size'] && $mimeTypeContents[0] == 'image')) {
			$save_file = validateImageFile($file_details);
		}
                $log->debug("File Validation status in Check1 save_file => $save_file");
Prasad's avatar
Prasad committed
			return false;
		}
Prasad's avatar
Prasad committed

		//only images are allowed for these modules
		if ($module == 'Contacts' || $module == 'Products') {
			$save_file = validateImageFile($file_details);
                $log->debug("File Validation status in Check2 save_file => $save_file");
Loading
Loading full blame...