[Vtigercrm-commits] [vtiger-commits] r6229 - in /vtigercrm/trunk: data/CRMEntity.php include/utils/CommonUtils.php modules/HelpDesk/HelpDesk.php modules/uploads/add2db.php modules/uploads/downloadfile.php schema/DatabaseSchema.xml soap/customerportal.php

vtigercrm-commits at vtiger.fosslabs.com vtigercrm-commits at vtiger.fosslabs.com
Sat May 20 09:42:54 EDT 2006


Author: saraj
Date: Sat May 20 07:42:43 2006
New Revision: 6229

Log:
* Modified to store and retrieve the files. Now the files will be stored in the path like year/month/week/filename which will be useful to take backup of files

Modified:
    vtigercrm/trunk/data/CRMEntity.php
    vtigercrm/trunk/include/utils/CommonUtils.php
    vtigercrm/trunk/modules/HelpDesk/HelpDesk.php
    vtigercrm/trunk/modules/uploads/add2db.php
    vtigercrm/trunk/modules/uploads/downloadfile.php
    vtigercrm/trunk/schema/DatabaseSchema.xml
    vtigercrm/trunk/soap/customerportal.php

Modified: vtigercrm/trunk/data/CRMEntity.php
==============================================================================
--- vtigercrm/trunk/data/CRMEntity.php (original)
+++ vtigercrm/trunk/data/CRMEntity.php Sat May 20 07:42:43 2006
@@ -286,13 +286,16 @@
 		$filetype= $_FILES['filename']['type'];
 		$filesize = $_FILES['filename']['size'];
 
+		//get the file path inwhich folder we want to upload the file
+		$upload_file_path = decideFilePath();
+
 		if($binFile != '')
 		{
-			if(move_uploaded_file($_FILES["filename"]["tmp_name"],$uploaddir.$binFile))
+			if(move_uploaded_file($_FILES["filename"]["tmp_name"],$upload_file_path.$binFile))
 			{
 				if($filesize != 0)
 				{
-					$data = base64_encode(fread(fopen($uploaddir.$binFile, "r"), $filesize));
+					$data = base64_encode(fread(fopen($upload_file_path.$binFile, "r"), $filesize));
 				}
 			}
 			$current_id = $adb->getUniqueID("crmentity");
@@ -307,7 +310,7 @@
 			$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->formatString("crmentity","createdtime",$date_var).",".$adb->formatString("crmentity","modifiedtime",$date_var).")";
 			$adb->query($sql1);
 
-			$sql2="insert into attachments(attachmentsid, name, description, type) values(".$current_id.",'".$filename."','".$this->column_fields[$descname]."','".$filetype."')";
+			$sql2="insert into attachments(attachmentsid, name, description, type, path) values(".$current_id.",'".$filename."','".$this->column_fields[$descname]."','".$filetype."','".$upload_file_path."')";
 			$result=$adb->query($sql2);
 
 			//TODO -- instead of put contents in db now we should store the file in harddisk

Modified: vtigercrm/trunk/include/utils/CommonUtils.php
==============================================================================
--- vtigercrm/trunk/include/utils/CommonUtils.php (original)
+++ vtigercrm/trunk/include/utils/CommonUtils.php Sat May 20 07:42:43 2006
@@ -1628,33 +1628,6 @@
 }
 
 /**
- * This function is used to get the File Storage Path in the server.
- * @param int $attachmentid - file attachment id ie., crmid of the attachment
- * @param string $filename  - file name
- * return string $filepath  - filepath inwhere the file stored in the server will be return
-*/
-function getFilePath($attachmentid,$filename)
-{
-	global $log;
-	$log->debug("Entering getFilePath(".$attachmentid.",".$filename.") method ...");
-	global $adb;
-	global $root_directory;
-
-	$query = 'select crmid, setype, smownerid, users.user_name from crmentity inner join users on crmentity.smownerid=users.id where crmid='.$attachmentid;
-	$res = $adb->query($query);
-
-	$user_name = $adb->query_result($res,0,'user_name');
-
-	if(is_file($root_directory.'storage/user_'.$user_name.'/attachments/'.$filename))
-		$filepath = $root_directory.'storage/user_'.$user_name.'/attachments/';
-	else
-		$filepath = $root_directory.'test/upload/';
-
-	$log->debug("Exiting getFilePath method ...");
-	return $filepath;
-}
-
-/**
  * This function is used to get the all the modules that have Quick Create Feature.
  * Returns Tab Name and Tablabel.
  */
@@ -2148,5 +2121,58 @@
 	        return $resultant_array;
 }
 
+/**
+ * 	This function is used to decide the File Storage Path in where we will upload the file in the server.
+ * 	return string $filepath  - filepath inwhere the file should be stored in the server will be return
+*/
+function decideFilePath()
+{
+	global $log, $adb;
+	$log->debug("Entering into decideFilePath() method ...");
+
+	$filepath = 'storage/';
+
+	$year  = date('Y');
+	$month = date('F');
+	$day  = date('j');
+	$week   = '';
+
+	if(!is_dir($filepath.$year))
+	{
+		//create new folder
+		mkdir($filepath.$year);
+	}
+
+	if(!is_dir($filepath.$year."/".$month))
+	{
+		//create new folder
+		mkdir($filepath."$year/$month");
+	}
+
+	if($day > 0 && $day <= 7)
+		$week = 'week1';
+	elseif($day > 7 && $day <= 14)
+		$week = 'week2';
+	elseif($day > 14 && $day <= 21)
+		$week = 'week3';
+	elseif($day > 21 && $day <= 28 )
+		$week = 'week4';
+	else
+		$week = 'week5';
+
+	if(!is_dir($filepath.$year."/".$month."/".$week))
+	{
+		//create new folder
+		mkdir($filepath."$year/$month/$week");
+	}
+
+	$filepath = $filepath.$year."/".$month."/".$week."/";
+
+	$log->debug("Year=$year & Month=$month & week=$week && filepath=\"$filepath\"");
+	$log->debug("Exiting from decideFilePath() method ...");
+	
+	return $filepath;
+}
+
 
 ?>

Modified: vtigercrm/trunk/modules/HelpDesk/HelpDesk.php
==============================================================================
--- vtigercrm/trunk/modules/HelpDesk/HelpDesk.php (original)
+++ vtigercrm/trunk/modules/HelpDesk/HelpDesk.php Sat May 20 07:42:43 2006
@@ -154,7 +154,7 @@
 			inner join seattachmentsrel on seattachmentsrel.attachmentsid= attachments.attachmentsid
 			inner join crmentity on crmentity.crmid= seattachmentsrel.crmid
 			inner join crmentity crm2 on crm2.crmid=attachments.attachmentsid
-			inner join users on crm2.smcreatorid= users.id
+			left join users on crm2.smcreatorid= users.id
 		where crmentity.crmid=".$id;	
 		$log->debug("Exiting get_attachments method ...");
 		return getAttachmentsAndNotes('HelpDesk',$query,$id);

Modified: vtigercrm/trunk/modules/uploads/add2db.php
==============================================================================
--- vtigercrm/trunk/modules/uploads/add2db.php (original)
+++ vtigercrm/trunk/modules/uploads/add2db.php Sat May 20 07:42:43 2006
@@ -44,7 +44,10 @@
 	$_FILES["file_".$filecount]["name"] = $binFile;
 	// Vulnerability fix ends
 
-	if(move_uploaded_file($_FILES["file_".$filecount]["tmp_name"],$uploaddir.$crmid."_".$_FILES["file_".$filecount]["name"])) 
+	//decide the file path where we should upload the file in the server
+	$upload_filepath = decideFilePath();
+
+	if(move_uploaded_file($_FILES["file_".$filecount]["tmp_name"],$upload_filepath.$crmid."_".$_FILES["file_".$filecount]["name"])) 
 	{
 		$filename = $crmid.'_'.basename($binFile);
 		$filetype= $_FILES['file_'.$filecount]['type'];
@@ -62,7 +65,7 @@
 			$result = $adb->query($query);
 
 			$sql = "insert into attachments values(";
-			$sql .= $current_id.",'".$filename."','".$description."','".$filetype."')";
+			$sql .= $current_id.",'".$filename."','".$description."','".$filetype."','".$upload_filepath."')";
 			$result = $adb->query($sql);
 
 

Modified: vtigercrm/trunk/modules/uploads/downloadfile.php
==============================================================================
--- vtigercrm/trunk/modules/uploads/downloadfile.php (original)
+++ vtigercrm/trunk/modules/uploads/downloadfile.php Sat May 20 07:42:43 2006
@@ -27,8 +27,8 @@
 {
 	$fileType = @$adb->query_result($result, 0, "type");
 	$name = @$adb->query_result($result, 0, "name");
+	$filepath = @$adb->query_result($result, 0, "path");
 
-	$filepath = getFilePath($attachmentsid,$name);
 	$filesize = filesize($filepath.$name);
 	$fileContent = fread(fopen($filepath.$name, "r"), $filesize);
 

Modified: vtigercrm/trunk/schema/DatabaseSchema.xml
==============================================================================
--- vtigercrm/trunk/schema/DatabaseSchema.xml (original)
+++ vtigercrm/trunk/schema/DatabaseSchema.xml Sat May 20 07:42:43 2006
@@ -808,6 +808,7 @@
 		</field>
 		<field name="description" type="C" size="100" />
 		<field name="type" type="C" size="100" />
+		<field name="path" type="C" size="255" />
 		<index name="attachments_attachmentsid_idx">
 			<col>attachmentsid</col>
 		</index>

Modified: vtigercrm/trunk/soap/customerportal.php
==============================================================================
--- vtigercrm/trunk/soap/customerportal.php (original)
+++ vtigercrm/trunk/soap/customerportal.php Sat May 20 07:42:43 2006
@@ -676,7 +676,7 @@
 	for($i=0;$i<$noofrows;$i++)
 	{
 		$filename = $adb->query_result($res,$i,'name');
-		$filepath = getFilePath($adb->query_result($res,$i,'attachmentsid'),$filename);
+		$filepath = $adb->query_result($res,$i,'path');
 
 		$filesize = filesize($filepath.$filename);
 		$fileid = $adb->query_result($res,$i,'attachmentsid');
@@ -699,13 +699,16 @@
 	global $adb;
 	global $root_directory;
 
+	//decide the file path where we should upload the file in the server
+	$upload_filepath = decideFilePath();
+
 	$upload_dir = $root_directory.'test/upload/';
 	$new_filename = $ticketid.'_'.$filename;
 
 	$data = base64_decode($filecontents);
 
 	//write a file with the passed content
-	$handle = @fopen($upload_dir.$new_filename,'w');
+	$handle = @fopen($upload_filepath.$new_filename,'w');
 	fputs($handle, $data);
 	fclose($handle);	
 
@@ -717,7 +720,7 @@
 	$crmquery = "insert into crmentity (crmid,setype,description,createdtime) values('".$attachmentid."','HelpDesk Attachment','".$description."','".$date_var."')";
 	$crmresult = $adb->query($crmquery);
 
-	$attachmentquery = "insert into attachments values(".$attachmentid.",'".$new_filename."','".$description."','".$filetype."')";
+	$attachmentquery = "insert into attachments values(".$attachmentid.",'".$new_filename."','".$description."','".$filetype."','".$upload_filepath."')";
 	$attachmentreulst = $adb->query($attachmentquery);
 
 	$relatedquery = $sql1 = "insert into seattachmentsrel values('".$ticketid."','".$attachmentid."')";





More information about the vtigercrm-commits mailing list