Example #1
0
   /**
	* Import Data
	*/
	function csvImport()
	{
		global $option;
		$db = & JFactory::getDBO();
		$app = & JFactory::getApplication();
		$c = hwd_vs_Config::get_instance();

		$mediatype = JRequest::getWord( 'mediatype' );
		$delimiter = JRequest::getWord( 'delimiter' );
		$maxerrors = JRequest::getInt( 'maxerrors', 1, 'post' );

		$base_Dir = JPATH_SITE.DS.'media'.DS;
		$base_Name = "csv_import";

		$upload_result = hwd_vs_tools::uploadFile( "upfile_0", $base_Name, $base_Dir, 2, 'csv', 1 );
		if ($upload_result[0] == "0") {
			$msg = $upload_result[1];
			$app->enqueueMessage($msg);
			$app->redirect( JURI::root( true ) . '/administrator/index.php?option='.$option.'&task=import' );
		}

		if ($delimiter == "comma") {
			$delimiter = ",";
		} else if ($delimiter == "tab") {
			$delimiter = "|";
		} else if ($delimiter == "pipe") {
			$delimiter = "	";
		}

		include(JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_hwdvideoshare'.DS.'libraries'.DS.'csv_iterator.class.php');
		$csvIterator = new CsvIterator(JPATH_SITE.DS.'media'.DS.$base_Name.'.csv', true, $delimiter, "\"");

		$counter=0;
		$errors=0;
		while ($csvIterator->next())
		{
			$row = $csvIterator->current();

			if ($errors > $maxerrors)
			{
				$msg = "Import stopped! Maximum allowed errors exceeded.";
				$app->enqueueMessage($msg);
				$app->redirect( JURI::root( true ) . '/administrator/index.php?option='.$option.'&task=import' );
			}

			if (!isset($row['Type']) || empty($row['Type'])) { $row['Type'] = null; $errors++; $app->enqueueMessage("Null Type"); continue; }
			if (!isset($row['Code']) || empty($row['Code'])) { $row['Code'] = null; $errors++; $app->enqueueMessage("Null Code"); continue; }
			if (!isset($row['Extension']) || empty($row['Extension'])) { $row['Extension'] = "flv"; }
			if (!isset($row['Thumbnail']) || empty($row['Thumbnail'])) { $row['Thumbnail'] = null; $errors++; $app->enqueueMessage("Null Thumbnail"); continue; }
			if (!isset($row['Title']) || empty($row['Title'])) { $row['Title'] = _HWDVIDS_UNKNOWN; }
			if (!isset($row['Description']) || empty($row['Description'])) { $row['Description'] = _HWDVIDS_UNKNOWN; }
			if (!isset($row['Tags']) || empty($row['Tags'])) { $row['Tags'] = _HWDVIDS_UNKNOWN; }
			if (!isset($row['Category ID']) || empty($row['Category ID'])) { $row['Category ID']= "0"; }
			if (!isset($row['Duration']) || empty($row['Duration'])) { $row['Duration'] = "00:00:00"; }
			if (!isset($row['Allow Comments']) || empty($row['Allow Comments'])) { $row['Allow Comments'] = "1"; }
			if (!isset($row['Allow Embedding']) || empty($row['Allow Embedding'])) { $row['Allow Embedding'] = "1"; }
			if (!isset($row['Allow Ratings']) || empty($row['Allow Ratings'])) { $row['Allow Ratings'] = "1"; }
			if (!isset($row['Access']) || empty($row['Access'])) { $row['Access'] = "public"; }
			if (!isset($row['Number Of Views']) || empty($row['Number Of Views'])) { $row['Number Of Views'] = "0"; }
			if (!isset($row['User ID']) || empty($row['User ID'])) { $row['User ID'] = "0"; }
			if (!isset($row['Featured']) || empty($row['Featured'])) { $row['Featured'] = "0"; }
			if (!isset($row['Published']) || empty($row['Published'])) { $row['Published'] = "1"; }
			if (!isset($row['Upload Date']) || empty($row['Upload Date'])) { $row['Upload Date'] = date('Y-m-d H:i:s'); }
			if (!isset($row['Thumbnail Capture Time']) || empty($row['Thumbnail Capture Time'])) { $row['Thumbnail Capture Time'] = "00:00:02"; }

			// Sanitise
			$row['Type'] = preg_replace("/[^a-zA-Z0-9s_.-]/", "", $row['Type']);
			$row['Code'] = preg_replace("/[^a-zA-Z0-9s_:=?.\/&-]/", "", $row['Code']);
			$row['Extension'] = preg_replace("/[^a-zA-Z0-9s_-]/", "", $row['Extension']);
			$row['Thumbnail'] = $row['Thumbnail'];
			$row['Title'] = addslashes($row['Title']);
			$row['Description'] = addslashes($row['Description']);
			$row['Tags'] = addslashes($row['Tags']);
			$row['Category ID'] = intval($row['Category ID']);
			$row['Duration'] = addslashes($row['Duration']);
			$row['Allow Comments'] = intval($row['Allow Comments']);
			$row['Allow Embedding'] = intval($row['Allow Embedding']);
			$row['Allow Ratings'] = intval($row['Allow Ratings']);
			$row['Access'] = preg_replace("/[^a-zA-Z0-9s]/", "", $row['Access']);
			$row['Number Of Views'] = intval($row['Number Of Views']);
			$row['User ID'] = intval($row['User ID']);
			$row['Featured'] = intval($row['Featured']);
			$row['Published'] = intval($row['Published']);
			$row['Upload Date'] = addslashes($row['Upload Date']);
			$row['Thumbnail Capture Time'] = addslashes($row['Thumbnail Capture Time']);

			if ($row['Type'] == "local") {

				//search for duplications
				$db->SetQuery( 'SELECT count(*)'
							 . ' FROM #__hwdvidsvideos'
							 . ' WHERE video_id = "'.$row['Code'].'"'
							 );
				$total = $db->loadResult();
				echo $db->getErrorMsg();

				if ($total > 0) {
					$errors++;
					$app->enqueueMessage("Already Exists");
					continue;
				}

				if ($c->requiredins) {
					$_POST['video_id'] 		= $row['Code'].".".$row['Extension'];
					if ($row['Extension'] == "flv") {
						$_POST['approved'] 		= "queuedforthumbnail";
					} else {
						$_POST['approved'] 		= "queuedforconversion";
					}
				} else {
					$_POST['video_id'] 		= $row['Code'];
					if ($c->aav == 1) {
						$_POST['approved'] = "yes";
					} else {
						$_POST['approved'] = "pending";
					}
				}

				$_POST['video_type'] 		= "local";

			} else {

				//search for duplications
				$db->SetQuery( 'SELECT count(*)'
							 . ' FROM #__hwdvidsvideos'
							 . ' WHERE video_id = "'.$row['Code'].','.$row['Thumbnail'].'"'
							 );
				$total = $db->loadResult();
				echo $db->getErrorMsg();

				if ($total > 0) {
					$errors++;
					$app->enqueueMessage("Already Exists");
					continue;
				}

				$_POST['video_id'] 		= $row['Code'].','.$row['Thumbnail'];
				$_POST['approved'] 		= "yes";
				$_POST['video_type'] 	= $row['Type'];

			}

			$row_new = new hwdvids_video($db);

			$_POST['title'] 			= $row['Title'];
			$_POST['description'] 		= $row['Description'];
			$_POST['tags'] 				= $row['Tags'];
			$_POST['category_id'] 		= $row['Category ID'];
			$_POST['date_uploaded'] 	= $row['Upload Date'];
			$_POST['video_length'] 		= $row['Duration'];
			$_POST['allow_comments'] 	= $row['Allow Comments'];
			$_POST['allow_embedding'] 	= $row['Allow Embedding'];
			$_POST['allow_ratings'] 	= $row['Allow Ratings'];
			$_POST['public_private'] 	= $row['Access'];
			$_POST['number_of_views'] 	= $row['Number Of Views'];
			$_POST['user_id'] 			= $row['User ID'];
			$_POST['featured'] 		    = $row['Featured'];
			$_POST['published'] 		= $row['Published'];
			$_POST['date_uploaded'] 	= $row['Upload Date'];
			$_POST['thumb_snap'] 		= $row['Thumbnail Capture Time'];

			// bind it to the table
			if (!$row_new->bind($_POST)) {
				echo "<script> alert('".$row_new->getError()."'); window.history.go(-1); </script>\n";
				exit();
			}

			// store it in the db
			if (!$row_new->store()) {
				echo "<script> alert('".$row_new -> getError()."'); window.history.go(-1); </script>\n";
				exit();
			}

			$counter++;
		}

		unlink(JPATH_SITE.DS.'media'.DS.$base_Name.'.csv');
		$msg = "CSV Import has been executed and ".$counter." video have been imported, please check import was successful. (".$errors." Errors Reported)";
		$app->enqueueMessage($msg);
		$app->redirect( JURI::root( true ) . '/administrator/index.php?option='.$option.'&task=import' );
	}
    /**
     * Generates the human readable supported 'third party' website list
     *
     * @return       $code
     */
	function generateSupportedWebsiteList()
	{
		global $j15, $j16;
		$db = & JFactory::getDBO();

		if ($j16)
		{
			$db->SetQuery( 'SELECT * FROM #__extensions WHERE type = "plugin" AND folder = "hwdvs-thirdparty" AND enabled = 1 ORDER BY name ASC');
			$iniFile = JPATH_SITE.'/plugins/hwdvs-thirdparty/thirdpartysupportpack/support.ini';
		}
		else
		{
			$db->SetQuery( 'SELECT * FROM #__plugins WHERE published = 1 AND folder = "hwdvs-thirdparty" ORDER BY name ASC');
			$iniFile = JPATH_SITE.'/plugins/hwdvs-thirdparty/support.ini';
		}

    	$rows = $db->loadObjectList();

		$code = null;
		for ($i=0, $n=count($rows); $i < $n; $i++)
		{
			$row = $rows[$i];

			if ($row->element == "thirdpartysupportpack")
			{
				if (file_exists($iniFile))
				{
					require_once(JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_hwdvideoshare'.DS.'libraries'.DS.'csv_iterator.class.php');
					$csvIterator = new CsvIterator($iniFile, true, ",", "\"");

					while ($csvIterator->next())
					{
						$row = $csvIterator->current();

						if (!isset($row['Name']) || empty($row['Name'])) { continue; }
						if (!isset($row['Website']) || empty($row['Website'])) { continue; }

						$code.= "<a href=\"".$row['Website']."\" target=\"_blank\">".$row['Name']."</a>, ";
					}
				}
			}
			else if ($row->element == "youtube")
			{
				$code.= "<a href=\"http://www.youtube.com\" target=\"_blank\">Youtube.com</a>, ";
			}
			else if ($row->element == "google")
			{
				$code.= "<a href=\"http://video.google.com\" target=\"_blank\">Google.com</a>, ";
			}
		}
		if (substr($code, -2) == ", ") {$code = substr($code, 0, -2);}
		return $code;
	}