PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_tuiyo/libraries/tuiyo/database/tables/resources.php

https://github.com/iduknow/ignite
PHP | 797 lines | 459 code | 103 blank | 235 comment | 44 complexity | 81a98aaedbd0af699cb5c799c7b6508b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * ******************************************************************
  4. * All Uploaded Resrouces Class/Object for the Tuiyo platform *
  5. * ******************************************************************
  6. * @copyright : 2008 tuiyo Platform *
  7. * @license : http://platform.tuiyo.com/license BSD License *
  8. * @version : Release: $Id$ *
  9. * @link : http://platform.tuiyo.com/ *
  10. * @author : livingstone[at]drstonyhills[dot]com *
  11. * @access : Public *
  12. * @since : 1.0.0 alpha *
  13. * @package : tuiyo *
  14. * ******************************************************************
  15. */
  16. /**
  17. * no direct access
  18. */
  19. defined('TUIYO_EXECUTE') || die('Restricted access');
  20. /**
  21. * TuiyoTableResources
  22. *
  23. * @package tuiyo
  24. * @author Livingstone Fultang
  25. * @copyright 2009
  26. * @version $Id$
  27. * @access public
  28. */
  29. class TuiyoTableResources extends JTable{
  30. //`resourceID` INTEGER UNSIGNED NOT NULL,
  31. var $resourceID = null;
  32. //`userID` INTEGER UNSIGNED NOT NULL,
  33. var $userID = null;
  34. //`dateAdded` DATE ,
  35. var $dateAdded = null;
  36. //`tags` VARCHAR(100),
  37. var $tags = null;
  38. //`size` INTEGER UNSIGNED,
  39. var $size = null;
  40. //`rating` VARCHAR(45),
  41. var $rating = null;
  42. //`album` VARCHAR(100),
  43. var $album = null;
  44. //`artist` VARCHAR(100),
  45. var $artist = null;
  46. //`author` VARCHAR(100),
  47. var $author = null;
  48. //`composer` VARCHAR(100),
  49. var $composer = null;
  50. //`contentType` VARCHAR(45),
  51. var $contentType = null;
  52. //`copyright` VARCHAR(45),
  53. var $copyright = null;
  54. //`creator` VARCHAR(45),
  55. var $creator = null;
  56. //`dateCreated` DATE ,
  57. var $dateCreated = null;
  58. //`dateLastModified` DATE ,
  59. var $dateLastModified = null;
  60. //`description` VARCHAR(200),
  61. var $description = null;
  62. //`dimension` VARCHAR(45),
  63. var $dimension = null;
  64. //`exposureProgram` VARCHAR(45),
  65. var $exposureProgram = null;
  66. //`exposureBias` VARCHAR(45),
  67. var $exposureBias = null;
  68. //`exposureTime` VARCHAR(45),
  69. var $exposureTime = null;
  70. //`fileName` VARCHAR(100),
  71. var $fileName = null;
  72. //`flashMode` VARCHAR(100),
  73. var $flashMode = null;
  74. //`focalLength` VARCHAR(100),
  75. var $focalLength = null;
  76. //`folder` VARCHAR(255),
  77. var $folder = null;
  78. //`folderPath` VARCHAR(255),
  79. var $filePath = null;
  80. //`genre` VARCHAR(45),
  81. var $genre = null;
  82. //`isAttachment` BOOLEAN,
  83. var $isAttachement = null;
  84. //`isDeleted` BOOLEAN,
  85. var $isDeleted = null;
  86. //`language` VARCHAR(45),
  87. var $language = null;
  88. //`lightSource` VARCHAR(45),
  89. var $lightSource = null;
  90. //`apperture` VARCHAR(45),
  91. var $apperture = null;
  92. //`owner` VARCHAR(45),
  93. var $owner = null;
  94. //`producers` VARCHAR(100),
  95. var $producers = null;
  96. //`path` VARCHAR(100),
  97. var $path = null;
  98. //`senderName` VARCHAR(45),
  99. var $senderName = null;
  100. //`senderEmail` VARCHAR(80),
  101. var $senderEmail = null;
  102. //`type` VARCHAR(80),
  103. var $fileType = null;
  104. //`url` VARCHAR(200),
  105. var $url = null;
  106. //`verticalResolution` VARCHAR(45),
  107. var $verticalResoultion = null;
  108. //`videoCompression` VARCHAR(45),
  109. var $videoCompression = null;
  110. //`wordCount` VARCHAR(45),
  111. var $wordCount = null;
  112. //`year` VARCHAR(45),
  113. var $year = null;
  114. //`isPublic` BOOLEAN NOT NULL,
  115. var $isPublic = null;
  116. //
  117. var $fileTitle = null;
  118. //photos fields
  119. /**
  120. * TuiyoTableResources::__construct()
  121. *
  122. * @param mixed $db
  123. * @return void
  124. */
  125. public function __construct($db = null)
  126. {
  127. parent::__construct("#__tuiyo_resources", "resourceID", $db );
  128. $this->url = JURI::getPath( true );
  129. }
  130. /**
  131. * TuiyoTableResources::saveFile()
  132. *
  133. * @param mixed $fileData
  134. * @param mixed $type
  135. * @return void
  136. */
  137. public function saveFile($fileData, $type){
  138. //1. Validate Type [NOT AGAIN!!]
  139. $fileCache = JPATH_CACHE.DS.basename( $fileData['name'] ) ;
  140. $user = $GLOBALS["API"]->get("user", null);
  141. $fileName = $user->id."_".str_replace(array(" ","(",")","-","&","%",",","#" ), "", $fileData["name"] );
  142. //Check we know who we are dealing with
  143. if($user->joomla->get("guest")){
  144. JFile::delete( $fileCache );
  145. trigger_error("User must be logged in", E_USER_ERROR);
  146. return false;
  147. }
  148. //2. Bind Type Default Parts
  149. $this->fileTitle= JFile::stripExt( $fileData['name'] );
  150. $this->fileName = $fileName ;
  151. $this->fileType = JFile::getExt( $fileCache );
  152. $this->userID = $user->id;
  153. $this->author = $user->username;
  154. $this->owner = $user->username;
  155. $this->fileType = strtoupper( $this->fileType);
  156. $this->contentType = strtoupper( $type );
  157. $this->dateAdded = date('Y-m-d');
  158. $this->dateLastModified = date('Y-m-d' , filemtime( $fileCache ) );
  159. $this->size = filesize ( $fileCache );
  160. //3. Call User Function per Type
  161. $userFunc = array(
  162. "avatar"=> "saveAvatarFile",
  163. "audio" => "saveAudioFile",
  164. "photos"=> "savePhotoFile",
  165. "video" => "saveVideoFile",
  166. "archive"=>"saveArchiveFile",
  167. "wallpaper"=>"saveWallPaper",
  168. "gavatar"=>"saveGroupAvatar"
  169. );
  170. //4. Call user function
  171. return call_user_func( array( $this, $userFunc[$type]), $fileData );
  172. }
  173. /**
  174. *
  175. * TuiyoTableResources::saveImageFile()
  176. * Adds new photo resources to the photos Table
  177. *
  178. * @param mixed $fileData
  179. * @return void
  180. */
  181. public function savePhotoFile($fileData){
  182. //1. Move the file to the actual Photos Directory. i.e files/avatar/62/file.jpg
  183. $photosTable = TuiyoLoader::table("photos" , true );
  184. $user = $GLOBALS["API"]->get("user", null);
  185. $fileCache = JPATH_CACHE.DS.basename( $fileData['name'] ) ;
  186. $filePath = TUIYO_FILES.DS."photos".DS.$user->id.DS;
  187. $newFilePath = $filePath.$this->fileName;
  188. $newThumbFilePath = $filePath."thumb".DS.$this->fileName ;
  189. //If we cannot create a folder
  190. if(!JFolder::exists( $filePath)){
  191. JFolder::create( $filePath );
  192. JPath::setPermissions( $filePath );
  193. }
  194. if(file_exists($newFilePath)){
  195. JFile::delete( $fileCache );
  196. return true;
  197. }
  198. //If we cannot copy the file
  199. if(!JFile::copy($fileCache , $newFilePath )){
  200. trigger_error( "Could not copy the file", E_USER_ERROR );
  201. return false;
  202. }
  203. JFile::delete( $fileCache );
  204. //2. Complete the File Information
  205. $this->filePath = JPath::clean( $filePath );
  206. $this->url = $this->url.str_replace( array(JPATH_ROOT , DS) , array("","/") , $newFilePath );
  207. $this->dimension = json_encode( getimagesize( $newFilePath ) );
  208. //3. Save the Object in the Database
  209. if(!$this->store()){
  210. trigger_error( $this->getError(), E_USER_ERROR);
  211. return false;
  212. }
  213. //4. Add to the Photos Table;
  214. $photosTable->src_original_id = (int)$this->resourceID;
  215. $photosTable->src_thumb_id = (int)$this->createPhotoThumb( 100 );
  216. $photosTable->userid = (int)$user->id;
  217. $photosTable->comment_count = (int)0 ;
  218. $photosTable->aid = (int)0 ;
  219. $photosTable->last_modified = date("Y-m-d H:i:s");
  220. if(!$photosTable->store()){
  221. trigger_error( $photosTable->getError(), E_USER_ERROR);
  222. return false;
  223. }
  224. //Add MultiLine Story
  225. //Check if any photos where uploaded in the last 5 minutes;
  226. $canCreateActivity = $this->canCreateNewActivity( $user->id );
  227. if(!is_object($canCreateActivity)){
  228. $activity = TuiyoAPI::get("activity" );
  229. $root = JURI::root();
  230. $actTitle = "{*thisUser*} uploaded pictures to {*thisGSP1a*} profile";
  231. $actResrces = array(
  232. array(
  233. "type" => "image",
  234. "furl" => $this->url,
  235. "url" => $root.substr( str_replace( array(JPATH_ROOT , DS) , array("","/") , $newThumbFilePath ) , 1 )
  236. )
  237. );
  238. $photoCount = count($actResrces);
  239. $activity->publishMultiLineStory($user, $actTitle, "<i>$photoCount photo(s) uploaded</i>", "photos", NULL, NULL, NULL, $actResrces );
  240. }else{
  241. $activity = TuiyoAPI::get("activity" );
  242. $actTitle = "{*thisUser*} uploaded pictures to {*thisGSP1a*} profile";
  243. $root = JURI::root();
  244. $actResrces1 = json_decode( $canCreateActivity->resources , TRUE);
  245. $actResrces2 = array(
  246. array(
  247. "type" => "image",
  248. "furl" => $this->url,
  249. "url" => $root.substr( str_replace( array(JPATH_ROOT , DS) , array("","/") , $newThumbFilePath ) , 1 )
  250. )
  251. );
  252. $actResrces3 = array_merge($actResrces1 , $actResrces2 );
  253. $photoCount2 = count($actResrces3);
  254. //$canCreateActivity->sharewith = '["%p00%"]'; //@TODO check privacy per user
  255. $canCreateActivity->resources = json_encode( $actResrces3 );
  256. $canCreateActivity->body = "<i class=\"uploadCount\" >$photoCount2 photo(s) uploaded</i>";
  257. $canCreateActivity->store();
  258. }
  259. return true;
  260. }
  261. /**
  262. *
  263. * TuiyoTableResources::lastUploadNotRecent()
  264. * Checks if the last photoupload activity is within the 5 min treshold
  265. *
  266. * @param mixed $userID
  267. * @return void
  268. */
  269. public function canCreateNewActivity($userID){
  270. $tTable =& TuiyoLoader::table("timelinetmpl", TRUE );
  271. $dbo = $this->_db;
  272. $query = "SELECT template, datetime FROM #__tuiyo_timeline "
  273. . "\nWHERE userID=".$dbo->quote( (int)$userID )
  274. . "\nAND source = 'photos'"
  275. . "\nORDER BY datetime DESC LIMIT 1"
  276. ;
  277. $dbo->setQuery( $query );
  278. $row = $dbo->loadObjectList();
  279. if(sizeof($row) < 1) return false;
  280. $now = time();
  281. $time = strtotime($row[0]->datetime);
  282. if($now < ($time + 3600)){
  283. //print_R($tTable);
  284. $ltTable = new TuiyoTableTimelinetmpl( $this->_db );
  285. $ltTable->load( (int)$row[0]->template );
  286. return $ltTable;
  287. }
  288. }
  289. /**
  290. * TuiyoTableResources::loadImageFile()
  291. *
  292. * @param mixed $fileId
  293. * @return void
  294. */
  295. public function loadPhotoFile($fileId = null){}
  296. /**
  297. * TuiyoTableResources::saveAudioFile()
  298. *
  299. * @param mixed $fileData
  300. * @return void
  301. */
  302. public function saveAudioFile($fileData){
  303. //1. Move the file to the actual Avatar Directory. i.e files/avatar/62/file.jpg
  304. $user = $GLOBALS["API"]->get("user", null);
  305. $fileCache = JPATH_CACHE.DS.basename( $fileData['name'] ) ;
  306. $filePath = TUIYO_FILES.DS."music".DS.$user->id.DS;
  307. $newFilePath = $filePath.$this->fileName;
  308. //If we cannot create a folder
  309. if(!JFolder::exists( $filePath)){
  310. JFolder::create( $filePath );
  311. JPath::setPermissions( $filePath );
  312. }
  313. if(file_exists($newFilePath)){
  314. JFile::delete( $fileCache );
  315. return true;
  316. }
  317. //If we cannot copy the file
  318. if(!JFile::copy($fileCache , $newFilePath )){
  319. trigger_error( "Could not copy the file", E_USER_ERROR );
  320. return false;
  321. }
  322. JFile::delete( $fileCache );
  323. //2. Complete the File Information
  324. $this->filePath = JPath::clean( $filePath );
  325. $this->url = $this->url.str_replace( array(JPATH_ROOT , DS) , array("","/") , $newFilePath );
  326. //Tags
  327. $newFilePath = fopen($newFilePath, "r");
  328. fseek($newFilePath, -128, SEEK_END);
  329. $tag = fread($newFilePath, 3);
  330. if ($tag == "TAG") {
  331. $data["song"] = trim(fread($newFilePath, 30));
  332. $this->artist = trim(fread($newFilePath, 30));
  333. $this->album = trim(fread($newFilePath, 30));
  334. $this->year = trim(fread($newFilePath, 4));
  335. //$data["genre"] = $genre_arr[ord(trim(fread($newFilePath, 1)))];
  336. }
  337. fclose($newFilePath);
  338. //3. Save the Object in the Database
  339. if(!$this->store()){
  340. trigger_error( $this->getError(), E_USER_ERROR);
  341. return false;
  342. }
  343. //2. Update the User Table with the current user Avatar Details. I.e Avatar id!
  344. return true;
  345. }
  346. /**
  347. * TuiyoTableResources::loadMp3File()
  348. * Loads all user audio files in the resource table
  349. * @param mixed $fileId
  350. * @return void
  351. */
  352. public function loadAllAudioFiles($userID , $fileIds = array(), $format = FALSE ){
  353. $userID = !empty($userID ) ? (int)$userID : $GLOBALS["API"]->get("user", null)->id ;
  354. $filter = !empty($fileIds)? "\nAND r.resourceID IN (".implode( "," , $fileIds ).")" : "" ;
  355. $dbo = $this->_db;
  356. $query = "SELECT r.artist, r.composer, r.fileName, r.album, r.url, r.year, r.fileTitle, r.rating"
  357. . "\nFROM #__tuiyo_resources r"
  358. . "\nWHERE r.userID = ".$dbo->quote( (int)$userID )
  359. . "\nAND r.contentType = 'AUDIO'"
  360. . $filter
  361. ;
  362. $dbo->setQuery( $query );
  363. $files = $dbo->loadObjectList( );
  364. //Adjust Parameters
  365. if($format){
  366. $formated = array();
  367. foreach($files as $item){
  368. }
  369. $files = $formated ;
  370. }
  371. return (array)$files ;
  372. }
  373. /**
  374. * TuiyoTableResources::saveAvatarFile()
  375. *
  376. * @param mixed $fileData
  377. * @return void
  378. */
  379. public function saveAvatarFile($fileData){
  380. //1. Move the file to the actual Avatar Directory. i.e files/avatar/62/file.jpg
  381. $user = $GLOBALS["API"]->get("user", null);
  382. $fileCache = JPATH_CACHE.DS.basename( $fileData['name'] ) ;
  383. $filePath = TUIYO_FILES.DS."avatars".DS.$user->id.DS;
  384. $newFilePath = $filePath.$this->fileName;
  385. //If we cannot create a folder
  386. if(!JFolder::exists( $filePath)){
  387. JFolder::create( $filePath );
  388. JPath::setPermissions( $filePath );
  389. }
  390. if(file_exists($newFilePath)){
  391. JFile::delete( $newFilePath ); //Just delete the file
  392. //return true;
  393. }
  394. //If we cannot copy the file
  395. if(!JFile::copy($fileCache , $newFilePath )){
  396. trigger_error( "Could not copy the file", E_USER_ERROR );
  397. return false;
  398. }
  399. JFile::delete( $fileCache );
  400. //2. Complete the File Information
  401. $this->filePath = JPath::clean( $filePath );
  402. $this->url = $this->url.str_replace( array(JPATH_ROOT , DS) , array("","/") , $newFilePath );
  403. $this->dimension = json_encode( getimagesize( $newFilePath ) );
  404. //3. Save the Object in the Database
  405. if(!$this->store()){
  406. trigger_error( $this->getError(), E_USER_ERROR);
  407. return false;
  408. }
  409. //print_R("stored okay"); die;
  410. //2. Update the User Table with the current user Avatar Details. I.e Avatar id!
  411. $thumbs = array(
  412. "thumb200" => $this->createAvatarThumb( 200 ),
  413. "thumb70" => $this->createAvatarThumb( 70 ),
  414. "thumb35" => $this->createAvatarThumb( 35 )
  415. );
  416. $paramsTable = TuiyoAPI::get( "params" );
  417. $paramsTable->loadParams( "user.avatar" , $this->userID );
  418. $query = "DELETE FROM #__tuiyo_params "
  419. . "\nWHERE userID='".(int)$this->userID
  420. . "'\nAND application='user.avatar'"
  421. ;
  422. $this->_db->setQuery( $query );
  423. $this->_db->query();
  424. $tableOfParams = TuiyoLoader::table( "params" );
  425. $tableOfParams->load( null );
  426. $tableOfParams->userID = $this->userID;
  427. $tableOfParams->data = json_encode( $thumbs );
  428. $tableOfParams->application = "user.avatar";
  429. if(!$tableOfParams->store()){
  430. trigger_error( $thumbX->getError(), E_USER_ERROR);
  431. return false;
  432. }
  433. return true;
  434. }
  435. /**
  436. * TuiyoTableResources::saveGroupAvatar()
  437. * Saves a group Avatar;
  438. * @param mixed $fileData
  439. * @return
  440. */
  441. public function saveGroupAvatar( $fileData ){
  442. $user = $GLOBALS["API"]->get("user", null);
  443. $groupID = JRequest::getInt("groupID", null );
  444. //Must have a groupID
  445. if(!isset($groupID)||$groupID<1){
  446. JError::raiseError( TUIYO_SERVER_ERROR , _("Invalid group id specified"));
  447. return false;
  448. }
  449. $gModel = TuiyoLoader::model("groups", true );
  450. $gData = $gModel->getGroup( $groupID );
  451. //If is already a member
  452. if(!$gData || $gData->isMember < 1 || $gData->isAdmin < 1 || empty($gData->groupID) || $gData->groupID < 1 ){
  453. JError::raiseError( TUIYO_SERVER_ERROR , _("You do not have permission to modify this group"));
  454. return false;
  455. }
  456. $fileCache = JPATH_CACHE.DS.basename( $fileData['name'] ) ;
  457. $filePath = TUIYO_FILES.DS."groups".DS.$user->id.DS;
  458. $newFilePath = $filePath.$this->fileName;
  459. //If we cannot create a folder
  460. if(!JFolder::exists( $filePath)){
  461. JFolder::create( $filePath );
  462. JPath::setPermissions( $filePath );
  463. }
  464. if(file_exists($newFilePath)){
  465. JFile::delete( $fileCache );
  466. return true;
  467. }
  468. //If we cannot copy the file
  469. if(!JFile::copy($fileCache , $newFilePath )){
  470. trigger_error( "Could not copy the file", E_USER_ERROR );
  471. return false;
  472. }
  473. JFile::delete( $fileCache );
  474. //2. Complete the File Information
  475. $this->filePath = JPath::clean( $filePath );
  476. $this->url = $this->url.str_replace( array(JPATH_ROOT , DS) , array("","/") , $newFilePath );
  477. $this->dimension = json_encode( getimagesize( $newFilePath ) );
  478. //3. Save the Object in the Database
  479. if(!$this->store()){
  480. trigger_error( $this->getError(), E_USER_ERROR);
  481. return false;
  482. }
  483. //2. Update the User Table with the current user Avatar Details. I.e Avatar id!
  484. $thumbs = array(
  485. "thumb200" => substr( $this->createAvatarThumb( 200 ), 1 ),
  486. "thumb70" => substr( $this->createAvatarThumb( 70 ), 1 ),
  487. "thumb35" => substr( $this->createAvatarThumb( 35 ), 1 )
  488. );
  489. $data = array(
  490. "groupID" => $groupID,
  491. "banner" => json_encode( $thumbs ),
  492. "logo" => $thumbs["thumb70"]
  493. );
  494. $gModel->storeGroup( $data, $user->id , false );
  495. return true;
  496. }
  497. /**
  498. * TuiyoTableResources::createPhotoThumb()
  499. * Creates and saves thumbnails for photo albums
  500. * @param mixed $width
  501. * @return void
  502. */
  503. private function createPhotoThumb( $width )
  504. {
  505. $filePath = TUIYO_FILES.DS."photos".DS.$this->userID.DS;
  506. $userAvatar = array();
  507. $imageMani = TuiyoAPI::get( "imagemanipulation" );
  508. $thumbX = clone $this;
  509. $thumbXDir = $filePath."thumb".DS ;
  510. $thumbXtarget = $thumbXDir.$this->fileName ;
  511. $thumbSource = $this->filePath.$this->fileName ;
  512. if(!JFolder::exists( $thumbXDir)){
  513. JFolder::create( $thumbXDir );
  514. JPath::setPermissions( $thumbXDir );
  515. }
  516. if( $imageMani->resizeImage( $thumbSource, $thumbXtarget, $width, $width, true ) ){
  517. $thumbX->resourceID = null;
  518. $thumbX->filePath = JPath::clean( $thumbXDir );
  519. $thumbX->url = JURI::getPath( true ).str_replace( array(JPATH_ROOT , DS) , array("","/") , $thumbXtarget );
  520. $thumbX->fileTitle = $thumbX->fileTitle."_".$width."x".$width ;
  521. //3. Save the Object in the Database
  522. if(!$thumbX->store()){
  523. trigger_error( $thumbX->getError(), E_USER_ERROR);
  524. return false;
  525. }
  526. }
  527. $thumbID = $thumbX->resourceID ;
  528. $thumbX = null;
  529. return (int)$thumbID ;
  530. }
  531. /**
  532. * TuiyoTableResources::createAvatarThumb()
  533. * Creates and saves a thumb for the avatar image.
  534. * @param mixed $width
  535. * @return
  536. */
  537. private function createAvatarThumb( $width ){
  538. $filePath = TUIYO_FILES.DS."avatars".DS.$this->userID.DS;
  539. $userAvatar = array();
  540. $imageMani = TuiyoAPI::get( "imagemanipulation" );
  541. $thumbX = clone $this;
  542. $thumbXDir = $filePath."thumb".$width.DS ;
  543. $thumbXtarget = $thumbXDir.$this->fileName ;
  544. $thumbSource = $this->filePath.$this->fileName ;
  545. if(!JFolder::exists( $thumbXDir)){
  546. JFolder::create( $thumbXDir );
  547. JPath::setPermissions( $thumbXDir );
  548. }
  549. if( $imageMani->resizeImage( $thumbSource, $thumbXtarget, $width, $width, true ) ){
  550. $thumbX->resourceID = null;
  551. $thumbX->filePath = JPath::clean( $thumbXDir );
  552. $thumbX->url = JURI::getPath( true ).str_replace( array(JPATH_ROOT , DS) , array("","/") , $thumbXtarget );
  553. $thumbX->fileTitle = $thumbX->fileTitle."_".$width."x".$width ;
  554. //3. Save the Object in the Database
  555. if(!$thumbX->store()){
  556. //echo $thumbX->getError(); die;
  557. trigger_error( $thumbX->getError(), E_USER_ERROR);
  558. return false;
  559. }
  560. }
  561. $URL = $thumbX->url;
  562. $thumbX = null;
  563. return $URL ;
  564. }
  565. /**
  566. * TuiyoTableResources::saveArchiveFile()
  567. *
  568. * @param mixed $fileData
  569. * @return
  570. */
  571. public function saveArchiveFile($fileData){
  572. //1. Move the file to the actual Avatar Directory. i.e files/avatar/62/file.jpg
  573. $user = $GLOBALS["API"]->get("user", null);
  574. $fileCache = JPATH_CACHE.DS.basename( $fileData['name'] ) ;
  575. $filePath = TUIYO_FILES.DS."archives".DS.$user->id.DS;
  576. $newFilePath = $filePath.$this->fileName;
  577. //If we cannot create a folder
  578. if(!JFolder::exists( $filePath)){
  579. JFolder::create( $filePath );
  580. JPath::setPermissions( $filePath );
  581. }
  582. if(file_exists($newFilePath)){
  583. JFile::delete( $fileCache );
  584. return true;
  585. }
  586. //If we cannot copy the file
  587. if(!JFile::copy($fileCache , $newFilePath )){
  588. trigger_error( _("Could not copy the file"), E_USER_ERROR );
  589. return false;
  590. }
  591. JFile::delete( $fileCache );
  592. //2. Complete the File Information
  593. $this->filePath = JPath::clean( $filePath );
  594. $this->url = $this->url.str_replace( array(JPATH_ROOT , DS) , array("","/") , $newFilePath );
  595. $this->dimension = json_encode( getimagesize( $newFilePath ) );
  596. //3. Save the Object in the Database
  597. if(!$this->store()){
  598. trigger_error( $this->getError(), E_USER_ERROR);
  599. return false;
  600. }
  601. //2. Update the User Table with the current user Avatar Details. I.e Avatar id!
  602. return $this->url;
  603. }
  604. /**
  605. *
  606. * TuiyoTableResources::suggestResource()
  607. * Suggests users or group from string names
  608. *
  609. * @param mixed $string
  610. * @param mixed $userID
  611. * @param integer $limit
  612. * @return Array(
  613. * Object{
  614. * "rID"=> 'the resource ID',
  615. * "rType"=>'friend' or 'group'
  616. * "rName"=>'the resouce name'
  617. * }
  618. * )
  619. */
  620. public function suggestResource($string, $userID, $limit = 10 , $searchGroups = TRUE ){
  621. $dbo = $this->_db ;
  622. $salt = (string)$string ;
  623. $groups = ($searchGroups)
  624. ? "\nUNION SELECT g.groupID as rID, g.gName as rName, 'group' as rType"
  625. . "\nFROM #__tuiyo_groups_members AS m"
  626. . "\nINNER JOIN #__tuiyo_groups AS g"
  627. . "\nON m.groupID = g.groupID"
  628. . "\nLEFT JOIN #__tuiyo_categories AS c"
  629. . "\nON g.catID = c.id"
  630. . "\nWHERE m.userID =".$dbo->quote( (int)$userID )
  631. . "\tAND g.gName LIKE ".$dbo->quote( "%{$salt}%" )
  632. : null;
  633. ; $query = "SELECT u.id as rID, CONCAT(u.name, ' (', u.username,')') as rName , 'friend' as rType"
  634. . "\nFROM #__tuiyo_friends f"
  635. . "\nINNER JOIN #__users AS u ON (u.id = f.thatUserID AND f.thisUserID =" .$dbo->quote( (int)$userID )
  636. . "\tOR u.id = f.thisUserID AND f.thatUserID =" .$dbo->quote( (int)$userID ). ")"
  637. . "\nLEFT JOIN #__tuiyo_users AS p ON p.userID = u.id"
  638. . "\nWHERE (f.thisUserID = ".$dbo->quote( (int)$userID )
  639. . "\tOR f.thatUserID = " .$dbo->quote( (int)$userID )
  640. . "\t) AND (u.username LIKE " .$dbo->quote( "%{$salt}%" )
  641. . "\tAND f.state = 1"
  642. . "\tOR u.name LIKE ".$dbo->quote( "%{$salt}%" )." )"
  643. . $groups ;
  644. $dbo->setQuery( $query, 0 , $limit );
  645. $results = (array)$dbo->loadObjectList();
  646. //echo $dbo->getQuery();
  647. //echo $dbo->getQuery();
  648. $public = new stdClass ;
  649. $public->rID = '00';
  650. $public->rType = 'friend';
  651. $public->rName = '@everyone';
  652. $resultsize = count($results);
  653. $rows = array_merge( $results , array( $resultsize=>$public ) );
  654. //Post Query Manip
  655. return $rows ;
  656. }
  657. /**
  658. * TuiyoTableResources::loadVideoFile()
  659. *
  660. * @param mixed $fileId
  661. * @return void
  662. */
  663. public function loadVideoFile($fileId = null){}
  664. /**
  665. * TuiyoTableResources::saveVideoFile()
  666. *
  667. * @param mixed $fileData
  668. * @return void
  669. */
  670. public function saveVideoFile( $fileData ){}
  671. /**
  672. * TuiyoTableResources::getInstance()
  673. *
  674. * @param mixed $db
  675. * @param bool $ifNotExist
  676. * @return
  677. */
  678. public function getInstance($db=null, $ifNotExist = true)
  679. {
  680. /** Creates new instance if none already exists ***/
  681. static $instance = array();
  682. if(isset($instance)&&!empty($instance)&&$ifNotExist){
  683. if(is_object($instance)){
  684. return $instance;
  685. }else{
  686. unset($instance);
  687. TuiyoTableResources::getInstance($db , $ifNotExist );
  688. }
  689. }else{
  690. $instance = new TuiyoTableResources( $db ) ;
  691. }
  692. return $instance;
  693. }
  694. }