File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/modules/youtube/module.youtube.php
<?php
/**
* This file contains the iwp_module_youtube class
*
* @version $Id$
*
*
* @package IWP_Modules
* @subpackage YouTube_Module
*/
/**
* YouTube Integration Class
* This class generates and saves data relating to the YouTube module
*
* @package IWP_Modules
* @subpackage YouTube_Module
*/
class iwp_module_youtube extends iwp_module {
public $moduleName = "youtube";
protected $AppendContent = false;
/**
* Instance
* This static variable holds the current instance of this object being loaded.
* So using the getInstance function anywhere will return the very same instance.
*
* @var object Instance
*/
public static $Instance;
/**
* getInstance
* This is a static function that sets up the class instance and stores it to the static variable. It will then return that instantiation in the future.
*
* @return object Returns the instantiated object
**/
public static function getInstance(){
if(!isset(self::$Instance)){
self::$Instance = new self();
}
return self::$Instance;
}
/**
* Deletes all data associated with this module.
* This is used when cleaning out the data during the importer process
*
* @return void
*/
public function TruncateAllData () {
$this->db->Query('TRUNCATE TABLE ' . $this->getTable());
}
/**
* Returns a list of database tables for this module.
*
* @param boolean $withoutPrefix A boolean for whether or not to include the prefix for the table.
*
* @return string
*/
public function getTables($withoutPrefix=false) {
$tables = array(
'youtube',
);
if($withoutPrefix) {
return $tables;
}
foreach($tables as $k=>$table ){
$tables[$k] = IWP_MODULE_DB_PREFIX . $table;
}
return $tables;
}
/**
* The constructor
*/
public function __construct(){
parent::__construct();
}
/**
* Checks whether this module is a content module or not. If it is, then it can be displayed as an option when creating a content type.
*
* @return boolean True if it is a content module, false otherwise.
*/
public function IsContentModule () {
return true;
}
/**
* AdminGetContentArray
* This function returns the array of options used by this module in the create/edit content page when
* a specified content type has the podcast module as a field.
*
* @return array The array of fields
*/
public function AdminGetContentArray($id = 0){
$arrFields = array(
'youtubeurl' => array(
'type'=> 'textbox',
'default' => '',
'value' => null,
),
);
$id = (int)$id;
if($id > 0){
$data = $this->db->FetchQuery('select * from '. $this->getTable() . ' where contentid='.$id);
$arrFields['youtubeurl']['value'] = $data['processedurl'];
$arrFields['youtubeurl']['prepend'] = '<div style="padding-top: 5px;">'. $this->lang->Get('CurrentVideo') . ': <a href="' . $data['processedurl']. '" target="_blank">' . $data['youtubetitle'] . "</a></div>";
}
$arrFields['youtubeurl']['append'] = '<div class="aside">(' . $this->lang->get('LinkExample') . ')</div>';
return $arrFields;
}
/**
* This function takes in data to make sure that the posted data is valid for saving to the database.
* If there are any errors with the data sent by the user then this returns an array ith the error message and the erroneous field.
*
* @param array $post The array with the posted data
* @param object $ContentType The current content type object, can be used to get content type variables.
*
* @return array|boolean Array if there is an error, or true otherwise.
*/
function ValidateSave($post, $ContentType){
if(isset($post['youtubeurl']) && strlen($post['youtubeurl']) > 1){
preg_match('/v=([^&]*)/i', $post['youtubeurl'], $matches);
if(!isset($matches[1]) || empty($matches[1])){
return array($this->lang->Get('invalid_youtube'), 'youtubevideo');
}
}
return true;
}
/**
* This function takes care of saving the data posted by the user. It is assumed that when this function is run the ValidateSave() function has
* already been run without error. This function can not be run without a valid content ID number.
*
* @param array $post The array with the posted data
* @param integer $contentid The current content id number
* @return mixed Array if there is an error, true otherwise
*/
function SaveField($post, $contentid){
if($contentid < 1){ return array('Invalid Content ID Number', null); }
$this->db->Query('delete from ' . $this->getTable() . ' where contentid='.$contentid);
if(!empty($post['youtubeurl'])){
$Insert = array();
$Insert['rawurl'] = $post['youtubeurl'];
$Insert['contentid'] = $contentid;
preg_match('/v=([^&]*)/i', $post['youtubeurl'], $matches);
if(isset($matches[1])){
$Insert['videocode'] = $matches[1];
$Insert['processedurl'] = 'http://www.youtube.com/watch?v=' . $matches[1];
$video = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/' . $matches[1]);
$Insert['youtubetitle'] = substr((string)$video->title, 0, 250);
$Insert['youtubedesc'] = substr((string)$video->content,0, 250);
if(!$this->db->InsertQuery($this->getTable(), $Insert)){
return array('An error while saving the YouTube video: ' . $this->db->GetErrorMsg(), null);
}
}
}
return true;
}
/**
* This function is passed the ID of the current content item and should load whatever template variables it wants into and array and return it.
* This array is then set into a variable by the modules name.
* Fore Example, if yor module name is 'helloworld' and this is set:
* $array['foo'] = 'bar';
* return $array;
* This makes {$helloworld.foo} accessible in the module template and it will output 'bar'
*
* @param integer $id The ID number of the content item calling for the output
*
* @return array The array of variables to be set.
*/
public function LoadFrontEndData($id){
$returnArray = array();
$returnArray['hasYouTubeVideo'] = false;
if(!iwp_IsId($id)){
return $returnArray;
}
$q = $this->db->query('select * from '. $this->getTable(). ' where contentid='. $id);
$data = $this->db->fetch($q);
// checking if there is at least one attachment
if(!is_array($data) || sizeof($data) < 1){
return $returnArray;
}
$returnArray['hasYouTubeVideo'] = true;
$returnArray = array_merge($returnArray, $data);
return $returnArray;
}
/**
* Creates the database table for this module, dropping any table that gets in its way.
*
* @return Boolean Returns true on success, otherwise false.
*/
public function CreateModuleTable ()
{
$this->table->AddField('id')->SetAsPrimaryKey();
$this->table->AddField('contentid')->SetAsInt();
$this->table->AddField('rawurl')->SetAsVarChar();
$this->table->AddField('processedurl')->SetAsVarChar();
$this->table->AddField('videocode')->SetAsVarChar();
$this->table->AddField('youtubetitle')->SetAsVarChar();
$this->table->AddField('youtubedesc')->SetAsVarChar();
$this->tableSchema = $this->table->GetCreateTable();
}
}