From 04b264fd07f1b3f24587f17d8bdab8c68079a058 Mon Sep 17 00:00:00 2001
From: CaMer0n <cameron@e107coders.org>
Date: Sat, 14 Nov 2009 04:13:11 +0000
Subject: [PATCH] Media Manager work..

---
 e107_admin/image.php             | 141 +++++++++++++++++++++++++++----
 e107_admin/sql/core_sql.php      |   8 +-
 e107_handlers/e107_class.php     |  19 +++--
 e107_handlers/e_parse_class.php  |  19 +++--
 e107_handlers/form_handler.php   |  12 ++-
 e107_handlers/upload_handler.php |   6 +-
 e107_media/cache/index.html      |   0
 e107_media/files/index.html      |   0
 e107_media/icons/index.html      |   0
 e107_media/images/index.html     |   0
 e107_media/index.html            |   0
 e107_media/logs/index.html       |   0
 e107_media/temp/index.html       |   0
 13 files changed, 167 insertions(+), 38 deletions(-)
 create mode 100644 e107_media/cache/index.html
 create mode 100644 e107_media/files/index.html
 create mode 100644 e107_media/icons/index.html
 create mode 100644 e107_media/images/index.html
 create mode 100644 e107_media/index.html
 create mode 100644 e107_media/logs/index.html
 create mode 100644 e107_media/temp/index.html

diff --git a/e107_admin/image.php b/e107_admin/image.php
index 1ea62c2ec..2dc0ef0f8 100644
--- a/e107_admin/image.php
+++ b/e107_admin/image.php
@@ -9,8 +9,8 @@
  * Image Administration Area
  *
  * $Source: /cvs_backup/e107_0.8/e107_admin/image.php,v $
- * $Revision: 1.28 $
- * $Date: 2009-11-08 13:21:56 $
+ * $Revision: 1.29 $
+ * $Date: 2009-11-14 04:13:10 $
  * $Author: e107coders $
  *
 */
@@ -75,12 +75,98 @@ class media_admin extends e_admin_dispatcher
 
 	$var['editor']['text'] = "Image Manipulation (future release)";
 	$var['editor']['link'] = e_SELF."?editor";*/
+	
+	protected $mimePaths = array(
+			"text"			=> 'files',
+			'multipart'		=> 'files',
+			'application'	=> 'files',
+			'audio'			=> 'audio',
+			'image'			=> 'images',
+			'video'			=> 'video',
+			'other'			=> 'files'
+	);
 
 	protected $adminMenuAliases = array(
 		'main/edit'	=> 'main/list'				
 	);	
 	
 	protected $menuTitle = LAN_MEDIAMANAGER;
+		
+	function init()
+	{
+		$this->observeUploaded();
+	}	
+		
+		
+	function observeUploaded()
+	{
+		$sql = e107::getDb();
+		$mes = e107::getMessage();
+		
+		if(!varset($_POST['uploadfiles']) && !varset($_POST['etrigger_submit']))
+		{
+			return;
+		}
+		
+		$pref['upload_storagetype'] = "1";
+		require_once(e_HANDLER."upload_handler.php"); //TODO - still not a class!
+		$uploaded = process_uploaded_files(e_MEDIA.'temp/');
+		
+		foreach($uploaded as $upload)
+		{
+			$oldpath = 'temp/'.$upload['name'];
+			$newpath =$this->getPath($upload['type']).'/'.$upload['name'];
+						
+			$insert = array(
+				'media_type'		=> $upload['type'],
+				'media_name'		=> $upload['name'],
+				'media_description'	=> '',
+				'media_url'			=> "{e_MEDIA}".$newpath,
+				'media_datestamp'	=> time(),
+				'media_size'		=> $upload['size'],
+				'media_usedby'		=> '',
+				'media_tags'		=> '',
+				'media_author'		=> USERID
+			);
+			
+			// Temporary workaround for class limitation. 
+			/*
+			 * We need to process the data before it's saved to the DB. 
+			 * When the upload is done with ajax, we need to prepopulate the form with the data above.
+			 */
+			
+			if(rename(e_MEDIA.$oldpath, e_MEDIA.$newpath))
+			{
+				if($sql->db_Insert('core_media',$insert)) 
+				{
+					$mes->add("Added ".$upload['name']." to DB", E_MESSAGE_SUCCESS);	
+				} 				
+			}			
+		}
+		
+		$message .= print_a($uploaded,TRUE);
+
+		$mes->add($message, E_MESSAGE_DEBUG);
+		
+		
+	}
+	
+	function onDelete() // call when 'delete' is executed. - delete the file with the db record (optional pref)
+	{
+		
+	}
+	
+	function getPath($type)
+	{
+		list($pmime,$tmp) = explode('/',$type);
+		$dir = $this->mimePaths[$pmime]."/".date("Y-m"); 
+		if(!is_dir(e_MEDIA.$dir))
+		{
+			mkdir(e_MEDIA.$dir,0755);	
+		}
+		return $dir;		
+	}
+	
 }
 
 
@@ -91,41 +177,64 @@ class media_admin_ui extends e_admin_ui
 		protected $pluginName = 'core';
 		protected $table = "core_media";
 		
-		protected $listQry = "SELECT * FROM #core_media"; // without any Order or Limit. 
+	//	protected $listQry = "SELECT * FROM #core_media"; // without any Order or Limit. 
 		
 	//	//protected $editQry = "SELECT * FROM #comments WHERE comment_id = {ID}";
 		
+		protected $tableJoin = array(
+			'u.user' => array('leftField' => 'media_author', 'rightField' => 'user_id', 'fields' => 'user_id,user_loginname,user_name')
+		);
+		
 		protected $pid = "media_id";
 		protected $perPage = 10;
 		protected $batchDelete = true;
 		
 		//TODO - finish 'user' type, set 'data' to all editable fields, set 'noedit' for all non-editable fields
-    	protected $fields = array(
+    	/*
+    	 * We need a column with a preview that is generated from the path of another field. 
+    	 * ie. the preview column should show a thumbnail which is generated from the media_url column. 
+    	 * It needs to also take into consideration the type of media (image, video etc) which comes from another field. 
+    	 */
+			
+		protected $fields = array(
 			'checkboxes'			=> array('title'=> '',				'type' => null,			'data'=> null,		'width' =>'5%', 'forced'=> TRUE, 'thclass'=>'center', 'class'=>'center'),
-			'media_id'				=> array('title'=> LAN_ID,				'type' => 'int',		'data'=> 'int',		'width' =>'5%', 'forced'=> TRUE),
-       		'media_preview' 		=> array('title'=> "Preview",		'type' => null,			'data'=> null,		'width' => '10%'), // Generate on the fly.
-       		'media_upload' 			=> array('title'=> "Upload File",	'type' => 'upload',		'data'=> null,		'readParm' => 'hidden',	'width' => '10%'), // Generate on the fly.  
-			'media_title' 			=> array('title'=> LAN_TITLE,			'type' => 'text',		'data'=> 'str',		'width' => '5%'),
+			'media_id'				=> array('title'=> LAN_ID,			'type' => 'int',		'data'=> 'int',		'width' =>'5%', 'forced'=> TRUE),
+      		'media_url' 			=> array('title'=> LAN_URL,			'type' => 'image',		'data'=> 'str',		'thclass' => 'center', 'class'=>'center', 'filter' => true, 'batch' => true,	'width' => 'auto'),	 
+		
+	   	//	'media_preview' 		=> array('title'=> "Preview",		'type' => 'image',			'data'=> null,		'width' => '10%'), 
+       		'media_upload' 			=> array('title'=> "Upload File",	'type' => 'upload',		'data'=> null,		'readParm' => 'hidden',	'width' => '10%'),  
+			'media_name' 			=> array('title'=> LAN_TITLE,		'type' => 'text',		'data'=> 'str',		'width' => '5%'),
 			'media_caption' 		=> array('title'=> "Caption",		'type' => 'text',		'data'=> 'str',		'width' => '5%'),
-         	'media_description' 	=> array('title'=> LAN_DESCRIPTION,	'type' => 'textarea',	'data'=> 'str',		'width' => 'auto', 'thclass' => 'left first'), // Display name
-         	'media_category' 		=> array('title'=> LAN_CATEGORY,		'type' => 'int',		'data'=> 'int',		'width' => '30%', 'readParms' => 'expand=...&truncate=50&bb=1'), // Display name
-			'media_datestamp' 		=> array('title'=> LAN_DATESTAMP,		'type' => 'datestamp',	'data'=> 'int',		'width' => 'auto'),	// User date
-            'media_url' 			=> array('title'=> LAN_URL,			'type' => 'url',		'data'=> 'str',		'thclass' => 'center', 'class'=>'center', 'filter' => true, 'batch' => true,	'width' => 'auto'),	 	// Photo
-			'media_userclass' 		=> array('title'=> LAN_USERCLASS,		'type' => 'userclass',	'data'=> 'str',		'width' => '10%', 'thclass' => 'center' ),	 // Real name (no real vetting)
-			'media_tags' 			=> array('title'=> "Tags/Keywords",	'type' => 'text',		'data'=> 'str',		'width' => '10%',  'filter'=>TRUE,'batch'=>TRUE ),	 // No real vetting
+         	'media_description' 	=> array('title'=> LAN_DESCRIPTION,	'type' => 'textarea',	'data'=> 'str',		'width' => 'auto', 'thclass' => 'left first'), 
+         	'media_category' 		=> array('title'=> LAN_CATEGORY,	'type' => 'int',		'data'=> 'int',		'width' => '30%'), 
+			'media_type' 			=> array('title'=> "Mime Type",		'type' => 'text',		'data'=> 'str',		'width' => '30%'), 
+		//	'media_author'			=> array('title'=> LAN_AUTHOR,		'type' => 'user',		'data'=> 'int'),
+			'media_author' 			=> array('title'=> LAN_USER,		'type' => 'user',			'data'=> 'int', 'width' => 'auto', 'thclass' => 'center', 'class'=>'center', 'writeParms' => 'currentInit=1', 'filter' => true, 'batch' => true, 'nolist' => false	),	
+			'media_datestamp' 		=> array('title'=> LAN_DATESTAMP,	'type' => 'datestamp',	'data'=> 'int',		'width' => 'auto'),	// User date
+          	'media_size' 			=> array('title'=> "Size",			'type' => 'int',		'data'=> 'int',		'width' => 'auto'), 
+			'media_dimensions' 		=> array('title'=> "Dimensions",	'type' => 'text',		'data'=> 'str',		'width' => 'auto'), 
+			'media_userclass' 		=> array('title'=> LAN_USERCLASS,	'type' => 'userclass',	'data'=> 'str',		'width' => '10%', 'thclass' => 'center' ),	 
+			'media_tags' 			=> array('title'=> "Tags/Keywords",	'type' => 'text',		'data'=> 'str',		'width' => '10%',  'filter'=>TRUE,'batch'=>TRUE ),	
+			'u.user_name' 			=> array('title'=> "User name",		'type' => 'user',			'width' => 'auto', 'noedit' => true, 'readParms'=>'idField=media_author&link=1'),	// User name
+       		'u.user_loginname' 		=> array('title'=> "User login",	'type' => 'user',			'width' => 'auto', 'noedit' => true, 'readParms'=>'idField=media_author&link=1'),	// User login name			
 			'options' 				=> array('title'=> LAN_OPTIONS,		'type' => null,			'data'=> null,		'forced'=>TRUE, 'width' => '10%', 'thclass' => 'center last', 'class' => 'center')
 		);
 
 
-		protected $fieldpref = array('checkboxes', 'media_id', 'media_thumb', 'media_title', 'media_caption', 'media_description', 'media_category', 'media_datestamp','media_userclass', 'options');
-		
+	//	protected $fieldpref = array('checkboxes','media_url', 'media_id', 'media_thumb', 'media_title', 'media_caption', 'media_description', 'media_category', 'media_datestamp','media_userclass', 'options');
 		
+
+
+
 		/*
 		protected $prefs = array( 
 			'pref_type'	   				=> array('title'=> 'type', 'type'=>'text'),
 			'pref_folder' 				=> array('title'=> 'folder', 'type' => 'boolean'),	
 			'pref_name' 				=> array('title'=> 'name', 'type' => 'text')		
 		);*/
+	
+		
+
 		
 }
 
diff --git a/e107_admin/sql/core_sql.php b/e107_admin/sql/core_sql.php
index 4b97c6fbe..77288b941 100644
--- a/e107_admin/sql/core_sql.php
+++ b/e107_admin/sql/core_sql.php
@@ -9,8 +9,8 @@
  * Core SQL
  *
  * $Source: /cvs_backup/e107_0.8/e107_admin/sql/core_sql.php,v $
- * $Revision: 1.35 $
- * $Date: 2009-11-08 13:40:23 $
+ * $Revision: 1.36 $
+ * $Date: 2009-11-14 04:13:10 $
  * $Author: e107coders $
 */
 
@@ -125,12 +125,16 @@ CREATE TABLE core (
 
 CREATE TABLE core_media (
   media_id int(10) unsigned NOT NULL auto_increment,
+  media_type varchar(15) NOT NULL default '',
+  media_name varchar(255) NOT NULL default '',
   media_caption varchar(255) NOT NULL default '',
   media_description varchar(255) NOT NULL default '',
   media_category int(10) unsigned NOT NULL default '0',
   media_datestamp int(10) unsigned NOT NULL default '0',
   media_author int(10) unsigned NOT NULL default '0',
   media_url varchar(255) NOT NULL default '',
+  media_size int(20) unsigned NOT NULL default '0',
+  media_dimensions varchar(25) NOT NULL default '',
   media_userclass varchar(255) NOT NULL default '',
   media_usedby text NOT NULL,
   media_tags text NOT NULL,
diff --git a/e107_handlers/e107_class.php b/e107_handlers/e107_class.php
index 3a242d2e3..5358a5b4f 100644
--- a/e107_handlers/e107_class.php
+++ b/e107_handlers/e107_class.php
@@ -9,9 +9,9 @@
  * e107 Main
  *
  * $Source: /cvs_backup/e107_0.8/e107_handlers/e107_class.php,v $
- * $Revision: 1.67 $
- * $Date: 2009-11-12 16:47:36 $
- * $Author: secretr $
+ * $Revision: 1.68 $
+ * $Date: 2009-11-14 04:13:10 $
+ * $Author: e107coders $
 */
 
 if (!defined('e107_INIT')) { exit; }
@@ -1154,7 +1154,9 @@ class e107
 	{
 		global $DOWNLOADS_DIRECTORY, $ADMIN_DIRECTORY, $IMAGES_DIRECTORY, $THEMES_DIRECTORY, $PLUGINS_DIRECTORY,
 		$FILES_DIRECTORY, $HANDLERS_DIRECTORY, $LANGUAGES_DIRECTORY, $HELP_DIRECTORY, $CACHE_DIRECTORY,
-		$NEWSIMAGES_DIRECTORY, $CUSTIMAGES_DIRECTORY, $UPLOADS_DIRECTORY,$_E107;
+		$UPLOADS_DIRECTORY,$_E107, $MEDIA_DIRECTORY;
+
+	//	global $NEWSIMAGES_DIRECTORY, $CUSTIMAGES_DIRECTORY; 
 
 		$path = ""; $i = 0;
 
@@ -1210,6 +1212,7 @@ class e107
 			define("e_HANDLER", e_BASE.$HANDLERS_DIRECTORY);
 			define("e_LANGUAGEDIR", e_BASE.$LANGUAGES_DIRECTORY);
 			define("e_DOCS", e_BASE.$HELP_DIRECTORY);
+			define("e_MEDIA", e_BASE.$MEDIA_DIRECTORY);
 //
 // HTTP absolute paths
 //
@@ -1220,6 +1223,7 @@ class e107
 			define("e_FILE_ABS", e_HTTP.$FILES_DIRECTORY);
 			define("e_HANDLER_ABS", e_HTTP.$HANDLERS_DIRECTORY);
 			define("e_LANGUAGEDIR_ABS", e_HTTP.$LANGUAGES_DIRECTORY);
+			define("e_MEDIA_ABS", e_HTTP.$MEDIA_DIRECTORY);
 
 			if(isset($_SERVER['DOCUMENT_ROOT']))
 			{
@@ -1238,9 +1242,10 @@ class e107
 			}
 			else
 			{
-            	define("e_CACHE", e_BASE.$FILES_DIRECTORY."cache/");
+            	define("e_CACHE", e_MEDIA."cache/");
 			}
-
+/*
+ 
 			if($NEWSIMAGES_DIRECTORY)
 			{
             	define("e_NEWSIMAGE", e_BASE.$NEWSIMAGES_DIRECTORY);
@@ -1260,7 +1265,7 @@ class e107
 			{
             	define("e_CUSTIMAGE", e_IMAGE."custom/");
 			}
-
+*/
 			if ($DOWNLOADS_DIRECTORY{0} == "/")
 			{
 				define("e_DOWNLOAD", $DOWNLOADS_DIRECTORY);
diff --git a/e107_handlers/e_parse_class.php b/e107_handlers/e_parse_class.php
index 5961006bf..9a66db072 100644
--- a/e107_handlers/e_parse_class.php
+++ b/e107_handlers/e_parse_class.php
@@ -9,9 +9,9 @@
 * Text processing and parsing functions
 *
 * $Source: /cvs_backup/e107_0.8/e107_handlers/e_parse_class.php,v $
-* $Revision: 1.79 $
-* $Date: 2009-11-12 21:41:34 $
-* $Author: marj_nl_fr $
+* $Revision: 1.80 $
+* $Date: 2009-11-14 04:13:10 $
+* $Author: e107coders $
 *
 */
 if (!defined('e107_INIT')) { exit(); }
@@ -1503,7 +1503,8 @@ class e_parse
 				$e107->getFolder('files'),
 				$e107->getFolder('themes'),
 			//	$e107->getFolder('downloads'),
-				$e107->getFolder('handlers')
+				$e107->getFolder('handlers'),
+				$e107->getFolder('media')
 			);
 
 			switch ($mode)
@@ -1518,7 +1519,8 @@ class e_parse
 						e_FILE_ABS,
 						e_THEME_ABS,
 				//		e_DOWNLOAD_ABS, //impossible when download is done via php.
-						e_HANDLER_ABS
+						e_HANDLER_ABS,
+						e_MEDIA_ABS
 					);
 				break;
 
@@ -1532,7 +1534,8 @@ class e_parse
 						SITEURL.$e107->getFolder('files'),
 						SITEURL.$e107->getFolder('themes'),
 					//	SITEURL.$e107->getFolder('downloads'),
-						SITEURL.$e107->getFolder('handlers')
+						SITEURL.$e107->getFolder('handlers'),
+						SITEURL.$e107->getFolder('media')
 					);
 				break;
 			}
@@ -1545,7 +1548,9 @@ class e_parse
 				 "{e_FILE}",
 				 "{e_THEME}",
 				 //,"{e_DOWNLOAD}"
-				 "{e_HANDLER}");
+				 "{e_HANDLER}",
+				 "{e_MEDIA}"
+			);
 
 			if (ADMIN)
 			{
diff --git a/e107_handlers/form_handler.php b/e107_handlers/form_handler.php
index 8970caec1..4e808f0ed 100644
--- a/e107_handlers/form_handler.php
+++ b/e107_handlers/form_handler.php
@@ -9,9 +9,9 @@
  * Form Handler
  *
  * $Source: /cvs_backup/e107_0.8/e107_handlers/form_handler.php,v $
- * $Revision: 1.79 $
- * $Date: 2009-11-13 14:45:00 $
- * $Author: secretr $
+ * $Revision: 1.80 $
+ * $Date: 2009-11-14 04:13:10 $
+ * $Author: e107coders $
  *
 */
 
@@ -1177,6 +1177,8 @@ class e_form
 	function renderElement($key, $value, $attributes)
 	{
 		$parms = vartrue($attributes['writeParms'], array());
+		$tp = e107::getParser();
+		
 		if(is_string($parms)) parse_str($parms, $parms);
 		
 		switch($attributes['type'])
@@ -1284,6 +1286,10 @@ class e_form
 			case 'method': // Custom Function 
 				return call_user_func_array(array($this, $key), array($value, 'write', $parms));
 			break;
+			
+			case 'upload':
+				return $tp->parseTemplate("{UPLOADFILE=".e_UPLOAD."}");
+			break;
 
 			default:
 				return $value;
diff --git a/e107_handlers/upload_handler.php b/e107_handlers/upload_handler.php
index 496b45f6e..29e07a84d 100644
--- a/e107_handlers/upload_handler.php
+++ b/e107_handlers/upload_handler.php
@@ -9,8 +9,8 @@
  * File Upload Handler
  *
  * $Source: /cvs_backup/e107_0.8/e107_handlers/upload_handler.php,v $
- * $Revision: 1.23 $
- * $Date: 2009-11-07 02:10:34 $
+ * $Revision: 1.24 $
+ * $Date: 2009-11-14 04:13:11 $
  * $Author: e107coders $
  */
 
@@ -24,7 +24,7 @@ include_lan(e_LANGUAGEDIR.e_LANGUAGE."/lan_upload_handler.php");
 //define("UH_DEBUG",TRUE);
 define("UH_DEBUG", FALSE);
 
-define('e_UPLOAD_TEMP_DIR', e_FILE.'temp/'); // Temporary directory
+define('e_UPLOAD_TEMP_DIR', e_MEDIA.'temp/'); // Temporary directory
 define('e_READ_FILETYPES', 'filetypes.xml'); // Upload permissions
 define('e_SAVE_FILETYPES', 'filetypes_.xml');
 
diff --git a/e107_media/cache/index.html b/e107_media/cache/index.html
new file mode 100644
index 000000000..e69de29bb
diff --git a/e107_media/files/index.html b/e107_media/files/index.html
new file mode 100644
index 000000000..e69de29bb
diff --git a/e107_media/icons/index.html b/e107_media/icons/index.html
new file mode 100644
index 000000000..e69de29bb
diff --git a/e107_media/images/index.html b/e107_media/images/index.html
new file mode 100644
index 000000000..e69de29bb
diff --git a/e107_media/index.html b/e107_media/index.html
new file mode 100644
index 000000000..e69de29bb
diff --git a/e107_media/logs/index.html b/e107_media/logs/index.html
new file mode 100644
index 000000000..e69de29bb
diff --git a/e107_media/temp/index.html b/e107_media/temp/index.html
new file mode 100644
index 000000000..e69de29bb