PageRenderTime 71ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/settings/settings.php

https://github.com/ketzaldev/VCD-Db
PHP | 2018 lines | 1079 code | 405 blank | 534 comment | 185 complexity | 0eac8ca74e12c038e88075a140fe049e MD5 | raw file
  1. <?php
  2. /**
  3. * VCD-db - a web based VCD/DVD Catalog system
  4. * Copyright (C) 2003-2006 Konni - konni.com
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * @author Hákon Birgisson <konni@konni.com>
  12. * @package Kernel
  13. * @subpackage Settings
  14. * @version $Id$
  15. */
  16. ?>
  17. <?php
  18. require_once(dirname(__FILE__).'/settingsObj.php');
  19. require_once(dirname(__FILE__).'/sourceSiteObj.php');
  20. require_once(dirname(__FILE__).'/mediaTypeObj.php');
  21. require_once(dirname(__FILE__).'/movieCategoryObj.php');
  22. require_once(dirname(__FILE__).'/borrowerObj.php');
  23. require_once(dirname(__FILE__).'/loanObj.php');
  24. require_once(dirname(__FILE__).'/commentObj.php');
  25. require_once(dirname(__FILE__).'/statisticsObj.php');
  26. require_once(dirname(__FILE__).'/metadataObj.php');
  27. require_once(dirname(__FILE__).'/dvdObj.php');
  28. require_once(dirname(__FILE__).'/rssObj.php');
  29. class vcd_settings implements ISettings {
  30. private $settingsArray = null;
  31. private $mediatypeArray = null;
  32. private $mediatypeFullArray = null;
  33. private $moviecategoryArray = null;
  34. private $sourcesiteArray = null;
  35. private $borrowersArray = null;
  36. /**
  37. *
  38. * @var settingsSQL
  39. */
  40. private $SQL;
  41. public function __construct() {
  42. $this->SQL = new settingsSQL();
  43. }
  44. /**
  45. * Gets all SettingsObj in database
  46. *
  47. * @return array of settingsObj
  48. */
  49. public function getAllSettings() {
  50. try {
  51. if (is_null($this->settingsArray)) {
  52. $this->updateCache();
  53. }
  54. return $this->settingsArray;
  55. } catch (Exception $ex) {
  56. throw $ex;
  57. }
  58. }
  59. /**
  60. * Save settingsObj to database.
  61. * Parameter can either be settingsObj or
  62. * an array of settingsObj
  63. *
  64. * @param mixed $settingsObj
  65. */
  66. public function addSettings($settingsObj) {
  67. try {
  68. if ($settingsObj instanceof settingsObj) {
  69. if ($this->checkDuplicates($settingsObj)) {
  70. throw new VCDConstraintException('Key already exists');
  71. }
  72. if (strcmp($settingsObj->getKey(),"") == 0) {
  73. throw new VCDInvalidArgumentException('Key can not be empty');
  74. }
  75. if (strcmp($settingsObj->getValue(),"") == 0) {
  76. throw new VCDInvalidArgumentException('Value can not be empty');
  77. }
  78. $this->SQL->saveSettings($settingsObj);
  79. } elseif (is_array($settingsObj)) {
  80. $this->SQL->saveSettings($settingsObj);
  81. }
  82. $this->updateCache();
  83. } catch (Exception $ex) {
  84. throw $ex;
  85. }
  86. }
  87. /**
  88. * Update an settingsObj
  89. *
  90. * @param settingsObj $settingsObj
  91. */
  92. public function updateSettings(settingsObj $obj) {
  93. try {
  94. $this->SQL->updateSettings($obj);
  95. $this->updateCache();
  96. } catch (Exception $ex) {
  97. throw $ex;
  98. }
  99. }
  100. /**
  101. * Get a value from settingsObj by certain key
  102. *
  103. * @param string $key
  104. * @return string
  105. */
  106. public function getSettingsByKey($key) {
  107. try {
  108. $obj = $this->getAllSettings();
  109. foreach ($obj as $settingsObj) {
  110. if (strcmp($settingsObj->getKey(),$key) == 0) {
  111. return $settingsObj->getValue();
  112. }
  113. }
  114. throw new VCDProgramException("Key {$key} was not found");
  115. } catch (Exception $ex) {
  116. throw $ex;
  117. }
  118. }
  119. /**
  120. * Delete a settingsObj, returns true if settingsObj with that key was deleted, otherwise false.
  121. *
  122. * @param int $settings_id
  123. * @return boolean
  124. */
  125. public function deleteSettings($settings_id) {
  126. try {
  127. if (!is_numeric($settings_id)) {
  128. throw new VCDInvalidArgumentException('Settings Id must be numeric');
  129. }
  130. $obj = $this->getSettingsByID($settings_id);
  131. if (!$obj instanceof settingsObj) {
  132. throw new VCDInvalidArgumentException('Invalid settings Id value');
  133. }
  134. if ($obj->isProtected()) {
  135. throw new VCDConstraintException('Key is protected');
  136. }
  137. $this->SQL->deleteSettings($settings_id);
  138. $this->updateCache();
  139. return true;
  140. } catch (Exception $ex) {
  141. throw $ex;
  142. }
  143. }
  144. /**
  145. * Get a settingsObj by id
  146. *
  147. * @param int $settings_id
  148. * @return settingsObj
  149. */
  150. public function getSettingsByID($settings_id) {
  151. try {
  152. if (!is_numeric($settings_id)) {
  153. throw new VCDInvalidArgumentException('Settings Id must be numeric');
  154. }
  155. return $this->SQL->getSettingsByID($settings_id);
  156. } catch (Exception $ex) {
  157. throw $ex;
  158. }
  159. }
  160. /* Source Site public functions */
  161. /**
  162. * Get all sourcesiteObjects from database
  163. *
  164. * @return array of sourceSiteObj
  165. */
  166. public function getSourceSites() {
  167. try {
  168. $this->updateSiteCache();
  169. return $this->sourcesiteArray;
  170. } catch (Exception $ex) {
  171. throw $ex;
  172. }
  173. }
  174. /**
  175. * Get sourcesiteObj by id
  176. *
  177. * @param int $source_id
  178. * @return sourceSiteObj
  179. */
  180. public function getSourceSiteByID($source_id) {
  181. try {
  182. if (!is_numeric($source_id)) {
  183. throw new VCDInvalidArgumentException('Source Id must be numeric');
  184. }
  185. foreach ($this->getSourceSites() as $obj) {
  186. if ($obj->getsiteID() == $source_id) {
  187. return $obj;
  188. }
  189. }
  190. return null;
  191. } catch (Exception $ex) {
  192. throw $ex;
  193. }
  194. }
  195. /**
  196. * Get a sourceSiteObj by alias
  197. *
  198. * @param string $strAlias
  199. * @return sourceSiteObj
  200. */
  201. public function getSourceSiteByAlias($strAlias) {
  202. try {
  203. foreach ($this->getSourceSites() as $obj) {
  204. if (strcmp(strtolower($strAlias), strtolower($obj->getAlias())) == 0) {
  205. return $obj;
  206. }
  207. }
  208. return null;
  209. } catch (Exception $ex) {
  210. throw $ex;
  211. }
  212. }
  213. /**
  214. * Save a new SourceSiteObj to database
  215. *
  216. * @param sourceSiteObj $obj
  217. */
  218. public function addSourceSite(sourceSiteObj $obj) {
  219. try {
  220. if ($obj->isFetchable() && strcmp($obj->getClassName(),"") == 0) {
  221. throw new VCDConstraintException('When sourcesite is marked fetchable, classname must be defined.');
  222. }
  223. $this->SQL->addSourceSite($obj);
  224. $this->updateSiteCache();
  225. } catch (Exception $ex) {
  226. throw $ex;
  227. }
  228. }
  229. /**
  230. * Delete a sourcesiteObj by id
  231. *
  232. * @param int $source_id
  233. */
  234. public function deleteSourceSite($source_id) {
  235. try {
  236. if (!is_numeric($source_id)) {
  237. throw new VCDInvalidArgumentException('Source Id must be numeric');
  238. }
  239. $this->SQL->deleteSourceSite($source_id);
  240. $this->updateSiteCache();
  241. } catch (Exception $ex) {
  242. throw $ex;
  243. }
  244. }
  245. /**
  246. * Update an existing sourceSiteObj
  247. *
  248. * @param sourceSiteObj $obj
  249. */
  250. public function updateSourceSite(sourceSiteObj $obj) {
  251. try {
  252. if ($obj->isFetchable() && strcmp($obj->getClassName(),"") == 0) {
  253. throw new VCDConstraintException('When sourcesite is marked fetchable, classname must be defined.');
  254. }
  255. if (strcmp($obj->getImage(),"") != 0) {
  256. $filename = '../images/logos/'.$obj->getImage();
  257. if (!fs_file_exists($filename)) {
  258. throw new VCDProgramException("File {$filename} was not found.");
  259. }
  260. }
  261. $this->SQL->updateSourceSite($obj);
  262. $this->updateSiteCache();
  263. } catch (Exception $ex) {
  264. throw $ex;
  265. }
  266. }
  267. /*
  268. Media Type Functions
  269. */
  270. /**
  271. * Get an array parent media types in database
  272. *
  273. * @return array of medaTypeObj
  274. */
  275. public function getAllMediatypes() {
  276. try {
  277. if (is_null($this->mediatypeArray)) {
  278. $this->updateMediaCache();
  279. }
  280. return $this->mediatypeArray;
  281. } catch (Exeption $ex) {
  282. throw $ex;
  283. }
  284. }
  285. /**
  286. * Get an array of all media types in database
  287. *
  288. * @return array of medaTypeObj
  289. */
  290. public function getAllMediatypesFull() {
  291. try {
  292. if (is_null($this->mediatypeFullArray)) {
  293. $this->updateMediaCache();
  294. }
  295. return $this->mediatypeFullArray;
  296. } catch (Exeption $ex) {
  297. throw $ex;
  298. }
  299. }
  300. /**
  301. * Get a mediaTypeObj by id
  302. *
  303. * @param int $media_id
  304. * @return mediaTypeObj
  305. */
  306. public function getMediaTypeByID($media_id) {
  307. try {
  308. if (!is_numeric($media_id)) {
  309. throw new VCDInvalidArgumentException('Media Id must be numeric');
  310. }
  311. foreach ($this->getAllMediatypes() as $obj) {
  312. if ($media_id == $obj->getmediaTypeID()) {
  313. return $obj;
  314. }
  315. // Also check the children
  316. foreach ($obj->getChildren() as $childObj) {
  317. if ($media_id == $childObj->getmediaTypeID()) {
  318. return $childObj;
  319. }
  320. }
  321. }
  322. return null;
  323. } catch (Exception $ex) {
  324. throw $ex;
  325. }
  326. }
  327. /**
  328. * Save a new mediaTypeObj to database
  329. *
  330. * @param mediaTypeObj $obj
  331. */
  332. public function addMediaType(mediaTypeObj $obj) {
  333. try {
  334. $this->SQL->addMediaType($obj);
  335. $this->updateMediaCache();
  336. } catch (Exception $ex) {
  337. throw $ex;
  338. }
  339. }
  340. /**
  341. * Delete mediaTypeObj from database, returns true if deletion is successful otherwise false.
  342. *
  343. * @param int $mediatype_id
  344. * @return boolean
  345. */
  346. public function deleteMediaType($mediatype_id) {
  347. try {
  348. if (!is_numeric($mediatype_id)) {
  349. throw new VCDInvalidArgumentException('Mediatype Id must be numeric');
  350. }
  351. $tempObj = $this->getMediaTypeByID($mediatype_id);
  352. if (!$tempObj instanceof mediaTypeObj) {
  353. return false;
  354. }
  355. if ($tempObj->getChildrenCount() > 0) {
  356. throw new VCDConstraintException('Cannot delete mediatype with active subcategories');
  357. }
  358. if ($this->SQL->getCountByMediaType($mediatype_id) > 0) {
  359. throw new VCDConstraintException('Media type is in use. Cannot delete.');
  360. }
  361. $this->SQL->deleteMediaType($mediatype_id);
  362. $this->updateMediaCache();
  363. return true;
  364. } catch (Exception $ex) {
  365. throw $ex;
  366. }
  367. }
  368. /**
  369. * Update a mediaTypeObj
  370. *
  371. * @param mediaTypeObj $obj
  372. */
  373. public function updateMediaType(mediaTypeObj $obj) {
  374. try {
  375. $this->SQL->updateMediaType($obj);
  376. } catch (Exception $ex) {
  377. throw $ex;
  378. }
  379. }
  380. /**
  381. * Get all mediaTypes that are assigned to this vcdID
  382. *
  383. * @param int $vcd_id
  384. * @return array of mediaTypeObj
  385. */
  386. public function getMediaTypesOnCD($vcd_id) {
  387. try {
  388. $arrMediaTypesIDs = $this->SQL->getMediaTypesOnCD($vcd_id);
  389. if (is_array($arrMediaTypesIDs) && sizeof($arrMediaTypesIDs) > 0) {
  390. $arrMediaTypes = array();
  391. foreach ($arrMediaTypesIDs as $mediatypeid) {
  392. array_push($arrMediaTypes, $this->getMediaTypeByID($mediatypeid));
  393. }
  394. unset($arrMediaTypesIDs);
  395. return $arrMediaTypes;
  396. } else {
  397. throw new VCDProgramException("CD with id {$vcd_id} has no assigned media types");
  398. }
  399. } catch (Exception $ex) {
  400. throw $ex;
  401. }
  402. }
  403. /**
  404. * Get all mediatype in use by user
  405. *
  406. * @param int $user_id
  407. * @return array of mediaTypeObj
  408. */
  409. public function getMediaTypesInUseByUserID($user_id) {
  410. try {
  411. if (!is_numeric($user_id)) {
  412. throw new VCDInvalidArgumentException('User Id must be numeric');
  413. }
  414. $media_array = $this->SQL->getMediaTypesInUseByUserID($user_id);
  415. $i = 0;
  416. foreach ($media_array as $itemArray) {
  417. $catObj = $this->getMediaTypeByID($itemArray[0]);
  418. $media_array[$i++][1] = $catObj->getDetailedName();
  419. }
  420. asort($media_array);
  421. return aSortBySecondIndex($media_array,1);
  422. } catch (Exception $ex) {
  423. throw $ex;
  424. }
  425. }
  426. /**
  427. * Get all mediatype in use
  428. *
  429. * @return array of mediaTypeObj
  430. */
  431. public function getMediaTypesInUse()
  432. {
  433. try
  434. {
  435. $media_array = $this->SQL->getMediaTypesInUse();
  436. $i = 0;
  437. foreach ($media_array as $itemArray)
  438. {
  439. $catObj = $this->getMediaTypeByID($itemArray[0]);
  440. $media_array[$i++][1] = $catObj->getDetailedName();
  441. }
  442. asort($media_array);
  443. return aSortBySecondIndex($media_array,1);
  444. }
  445. catch (Exception $ex)
  446. {
  447. throw $ex;
  448. }
  449. }
  450. /**
  451. * Get the count of mediaTypes by certain categoryID and userID
  452. *
  453. * @param int $user_id
  454. * @param int $category_id
  455. * @return int
  456. */
  457. public function getMediaCountByCategoryAndUserID($user_id, $category_id) {
  458. try {
  459. if (!(is_numeric($user_id) && is_numeric($category_id))) {
  460. throw new VCDInvalidArgumentException('User Id and Category Id must be numeric');
  461. }
  462. return $this->SQL->getMediaCountByCategoryAndUserID($user_id, $category_id);
  463. } catch (Exception $ex) {
  464. throw $ex;
  465. }
  466. }
  467. /**
  468. * Get the count of mediaTypes by certain categoryID
  469. *
  470. * @param int $category_id
  471. * @return int
  472. */
  473. public function getMediaCountByCategory($category_id)
  474. {
  475. try
  476. {
  477. if (is_numeric($category_id))
  478. {
  479. return $this->SQL->getMediaCountByCategory($category_id);
  480. }
  481. else
  482. {
  483. throw new Exception('Parameters must be numeric');
  484. }
  485. }
  486. catch (Exception $ex) {
  487. throw $ex;
  488. }
  489. }
  490. /**
  491. * Get a mediatypeObj by mediatype name. If media type is not found, null is returned.
  492. *
  493. * @param string $media_name | The name of the media type
  494. * @return mediaTypeObj | The object found.
  495. */
  496. public function getMediaTypeByName($media_name) {
  497. try {
  498. foreach ($this->getAllMediaTypesFull() as $movieMediaTypeObj) {
  499. $typename = $movieMediaTypeObj->getName();
  500. if (strcmp(strtolower(substr($media_name, -strlen($typename))), strtolower($typename)) == 0) {
  501. return $movieMediaTypeObj;
  502. }
  503. }
  504. return null;
  505. } catch (Exception $ex) {
  506. throw $ex;
  507. }
  508. }
  509. /*
  510. Movie Category Functions
  511. */
  512. /**
  513. * Get an array of all movieCategoryObj in database
  514. *
  515. * @return array of movieCategoryObj
  516. */
  517. public function getAllMovieCategories() {
  518. try {
  519. if (is_null($this->moviecategoryArray)) {
  520. $this->updateCategoryCache();
  521. }
  522. return $this->moviecategoryArray;
  523. } catch (Exception $ex) {
  524. throw $ex;
  525. }
  526. }
  527. /**
  528. * Get an array of all movie categories that are in use.
  529. *
  530. * @return array of movieCategoryObj
  531. */
  532. public function getMovieCategoriesInUse() {
  533. try {
  534. return $this->SQL->getMovieCategoriesInUse();
  535. } catch (Exception $ex) {
  536. throw $ex;
  537. }
  538. }
  539. /**
  540. * Get all movie categories in use by user_id
  541. *
  542. * @param int $user_id
  543. * @return array of movieCategoryObj
  544. */
  545. public function getCategoriesInUseByUserID($user_id) {
  546. try {
  547. if (!is_numeric($user_id)) {
  548. throw new VCDInvalidArgumentException('User Id must be numeric');
  549. }
  550. return $this->SQL->getCategoriesInUseByUserID($user_id);
  551. } catch (Exception $ex) {
  552. throw $ex;
  553. }
  554. }
  555. /**
  556. * Get one instance of movieCategoryObj by ID
  557. *
  558. * @param int $category_id
  559. * @return movieCategoryObj
  560. */
  561. public function getMovieCategoryByID($category_id) {
  562. try {
  563. foreach ($this->getAllMovieCategories() as $movieCategoryObj) {
  564. if ($movieCategoryObj->getID() == $category_id) {
  565. return $movieCategoryObj;
  566. }
  567. }
  568. return null;
  569. } catch (Exception $ex) {
  570. throw $ex;
  571. }
  572. }
  573. /**
  574. * Save a new movieCategoyObj to database
  575. *
  576. * @param movieCategoyObj $obj
  577. */
  578. public function addMovieCategory(movieCategoryObj $obj) {
  579. try {
  580. $this->SQL->addMovieCategory($obj);
  581. } catch (Exception $ex) {
  582. throw $ex;
  583. }
  584. }
  585. /**
  586. * Delete a movieCategoryObj from database
  587. *
  588. * @param int $category_id
  589. */
  590. public function deleteMovieCategory($category_id) {
  591. try {
  592. if (!is_numeric($category_id)) {
  593. throw new VCDInvalidArgumentException('Category Id must be numeric');
  594. }
  595. // check if category is in use ..
  596. foreach ($this->getMovieCategoriesInUse() as $obj) {
  597. if ($obj->getID() == $category_id) {
  598. throw new VCDConstraintException('Cannot delete category that is in use.');
  599. }
  600. }
  601. $this->SQL->deleteMovieCategory($category_id);
  602. $this->updateCategoryCache();
  603. } catch (Exception $ex) {
  604. throw $ex;
  605. }
  606. }
  607. /**
  608. * Update an instance of movieCategoryObj in database
  609. *
  610. * @param movieCategoryObj $obj
  611. */
  612. public function updateMovieCategory(movieCategoryObj $obj) {
  613. try {
  614. $this->SQL->updateMovieCategory($obj);
  615. } catch (Exception $ex) {
  616. throw $ex;
  617. }
  618. }
  619. /**
  620. * Get a moviecategory_id by name
  621. *
  622. * @param string $category_name | The category name
  623. * @param bool $localized | Is the category name in English or not
  624. * @return int
  625. */
  626. public function getCategoryIDByName($category_name, $localized=false) {
  627. try {
  628. foreach ($this->getAllMovieCategories() as $movieCategoryObj) {
  629. if (strcmp(strtolower($category_name), strtolower($movieCategoryObj->getName($localized))) == 0) {
  630. return $movieCategoryObj->getID();
  631. }
  632. }
  633. return 0;
  634. } catch (Exception $ex) {
  635. throw $ex;
  636. }
  637. }
  638. /**
  639. * Get the categoryId of a movie by the movie ID
  640. *
  641. * @param int $itemId | The ID of the item to lookup
  642. * @return int
  643. */
  644. public function getCategoryIDByItemId($itemId) {
  645. try {
  646. if (!is_numeric($itemId)) {
  647. throw new VCDInvalidArgumentException('Item ID must be numeric.');
  648. }
  649. return $this->SQL->getCategoryIDByItemId($itemId);
  650. } catch (Exception $ex) {
  651. throw $ex;
  652. }
  653. }
  654. /*
  655. Borrowers functions
  656. */
  657. /**
  658. * Get borrower object by ID
  659. *
  660. * @param int $borrower_id
  661. * @return borrowerObj
  662. */
  663. public function getBorrowerByID($borrower_id) {
  664. try {
  665. if (!VCDUtils::isLoggedIn()) {
  666. throw new VCDSecurityException('Unauthorized function call.');
  667. }
  668. if (!is_numeric($borrower_id)) {
  669. throw new VCDInvalidArgumentException('Borrower Id must be numeric');
  670. }
  671. if (is_null($this->borrowersArray)) {
  672. // should not need this, just a precaution
  673. $this->getBorrowersByUserID(VCDUtils::getUserID());
  674. }
  675. foreach ($this->borrowersArray as $obj) {
  676. if (strcmp($borrower_id, $obj->getID()) == 0) {
  677. return $obj;
  678. }
  679. }
  680. // No borrower found, should never happen
  681. throw new VCDProgramException("Borrower with Id {$borrower_id} does not exist.");
  682. } catch (Exception $ex) {
  683. throw $ex;
  684. }
  685. }
  686. /**
  687. * Get an array of all borrower objects belonging to the specified user ID.
  688. *
  689. * @param int $user_id
  690. * @return array
  691. */
  692. public function getBorrowersByUserID($user_id) {
  693. try {
  694. if (is_null($this->borrowersArray)) {
  695. $this->updateBorrowersCache($user_id);
  696. }
  697. return $this->borrowersArray;
  698. } catch (Exception $ex) {
  699. throw $ex;
  700. }
  701. }
  702. /**
  703. * Add a new borrower object to database
  704. *
  705. * @param borrowerObj $obj
  706. */
  707. public function addBorrower(borrowerObj $obj) {
  708. try {
  709. $this->SQL->addBorrower($obj);
  710. } catch (Exception $ex) {
  711. throw $ex;
  712. }
  713. }
  714. /**
  715. * Update borrowe object in database
  716. *
  717. * @param borrowerObj $obj
  718. */
  719. public function updateBorrower(borrowerObj $obj) {
  720. try {
  721. $this->SQL->updateBorrower($obj);
  722. } catch (Exception $ex) {
  723. throw $ex;
  724. }
  725. }
  726. /**
  727. * Delete a borrower from database and all related records to him.
  728. *
  729. * @param borrowerObj $obj
  730. */
  731. public function deleteBorrower(borrowerObj $obj) {
  732. try {
  733. // Check if user is allowd to delete this borrowerObj
  734. $user_id = VCDUtils::getUserID();
  735. if ($obj->getOwnerID() != $user_id) {
  736. throw new VCDConstraintException("You have no permission to delete borrower " . $obj->getName());
  737. }
  738. // Delete the borrower loan history records
  739. $this->deleteLoanRecords($obj->getID());
  740. $this->SQL->deleteBorrower($obj->getID());
  741. } catch (Exception $ex) {
  742. throw $ex;
  743. }
  744. }
  745. /*
  746. Loan system functions
  747. */
  748. /**
  749. * Loan movies to borrower. Param $arrMovieIDs must contain array of movie ID's.
  750. * Returns true on success otherwise false.
  751. *
  752. * @param int $borrower_id
  753. * @param array $arrMovieIDs
  754. * @return bool
  755. */
  756. public function loanCDs($borrower_id, $arrMovieIDs) {
  757. try {
  758. if (!is_array($arrMovieIDs)) {
  759. throw new VCDInvalidArgumentException('Expected Ids as an array');
  760. }
  761. if (!is_numeric($borrower_id)) {
  762. throw new VCDInvalidArgumentException('Borrower Id must be numeric');
  763. }
  764. foreach ($arrMovieIDs as $cd_id) {
  765. $this->SQL->loanCDs(VCDUtils::getUserID(), $borrower_id, $cd_id);
  766. }
  767. return true;
  768. } catch (Exception $ex) {
  769. throw $ex;
  770. }
  771. }
  772. /**
  773. * Return a movie from loan, returns true on success otherwise false.
  774. *
  775. * @param int $loan_id
  776. * @return bool
  777. */
  778. public function loanReturn($loan_id) {
  779. try {
  780. if (!is_numeric($loan_id)) {
  781. throw new VCDInvalidArgumentException('Loan Id must be numeric');
  782. }
  783. $this->SQL->loanReturn($loan_id);
  784. return true;
  785. } catch (Exception $ex) {
  786. throw $ex;
  787. }
  788. }
  789. /**
  790. * Get all loans by specified user, param $show_returned specifies if only movies currently in loan
  791. * should be returned or all movies ever to be loaned. Returns an array of loan objects.
  792. *
  793. * @param int $user_id
  794. * @param bool $show_returned
  795. * @return array
  796. */
  797. public function getLoans($user_id, $show_returned) {
  798. try {
  799. if (!is_numeric($user_id)) {
  800. throw new VCDInvalidArgumentException('User Id must be numeric');
  801. }
  802. $loanArr = $this->SQL->getLoans($user_id, $show_returned);
  803. $outArr = array();
  804. foreach ($loanArr as $data) {
  805. $inArr = array($data[0], $data[1], $data[2], $this->getBorrowerByID($data[3]), $data[4], $data[5]);
  806. $obj = new loanObj($inArr);
  807. array_push($outArr, $obj);
  808. }
  809. unset($loanArr);
  810. unset($inArr);
  811. return $outArr;
  812. } catch (Exception $ex) {
  813. throw $ex;
  814. }
  815. }
  816. /**
  817. * Delete all loan records by borrower id
  818. *
  819. * @param int $borrower_id
  820. */
  821. private function deleteLoanRecords($borrower_id) {
  822. try {
  823. if (!is_numeric($borrower_id)) {
  824. throw new VCDInvalidArgumentException('Borrower Id must be numeric');
  825. }
  826. $this->SQL->deleteLoanRecords($borrower_id);
  827. } catch (Exception $ex) {
  828. throw $ex;
  829. }
  830. }
  831. /**
  832. * Get all loans by specified borrower id. $show_returned can specify weither to show all movies ever loaned
  833. * to that borrower or only the movies that he currently has in loan. Returns an array of loan objects.
  834. *
  835. * @param int $user_id
  836. * @param int $borrower_id
  837. * @param bool $show_returned
  838. * @return array
  839. */
  840. public function getLoansByBorrowerID($user_id, $borrower_id, $show_returned = false) {
  841. try {
  842. if (!is_numeric($user_id) || !is_numeric($borrower_id)) {
  843. throw new VCDInvalidArgumentException('User Id and Borrower Id must be numeric');
  844. }
  845. $arr = $this->getLoans($user_id, $show_returned);
  846. // Filter out the ones for the current borrower
  847. $userloans = array();
  848. foreach ($arr as $loanObj) {
  849. if ($loanObj->getBorrowerID() == $borrower_id) {
  850. array_push($userloans, $loanObj);
  851. }
  852. }
  853. unset($arr);
  854. return $userloans;
  855. } catch (Exception $ex) {
  856. throw $ex;
  857. }
  858. }
  859. /* Notification */
  860. /**
  861. * Send email notifycation. Send out email notification to all users that are wathing for
  862. * new movies to be inserted in the database. Returns true on successful email delivery otherwise false.
  863. *
  864. * @param vcdObj $obj
  865. * @return bool
  866. */
  867. public function notifyOfNewEntry(vcdObj $obj) {
  868. try {
  869. // First, find all the users that want to be notified
  870. $notifyPropObj = $this->User()->getPropertyByKey('NOTIFY');
  871. if (!$notifyPropObj instanceof userPropertiesObj) {
  872. throw new VCDProgramException('Property key Notify was not found, aborting.');
  873. }
  874. $notifyUsers = $this->User()->getAllUsersWithProperty($notifyPropObj->getpropertyID());
  875. if (is_array($notifyUsers) && sizeof($notifyUsers) > 0) {
  876. $arrEmails = array();
  877. foreach ($notifyUsers as $userObj) {
  878. // If the movie is an adult film .. only notify those with adult enabled ..
  879. if ($obj->isAdult() && !(bool)$userObj->getPropertyByKey('SHOW_ADULT')) {continue;}
  880. array_push($arrEmails, $userObj->getEmail());
  881. }
  882. unset($notifyUsers);
  883. $body = createNotifyEmailBody($obj);
  884. VCDUtils::sendMail($arrEmails, 'New entry in VCD-db', $body, true);
  885. }
  886. } catch (Exception $ex) {
  887. throw $ex;
  888. }
  889. }
  890. /* Rss Feeds */
  891. /**
  892. * Add a new feed to database.
  893. *
  894. * @param rssObj $obj
  895. */
  896. public function addRssfeed(rssObj $obj) {
  897. try {
  898. $this->SQL->addRssfeed($obj);
  899. } catch (Exception $ex) {
  900. throw $ex;
  901. }
  902. }
  903. /**
  904. * Get a single RSS feed by ID
  905. *
  906. * Returns array containing the RSS feed information.
  907. *
  908. * @param int $feed_id
  909. * @return array
  910. */
  911. public function getRssfeed($feed_id) {
  912. try {
  913. if (!is_numeric($feed_id)) {
  914. throw new VCDInvalidArgumentException('Feed Id must be numeric');
  915. }
  916. return $this->SQL->getRSSfeedByID($feed_id);
  917. } catch (Exception $ex) {
  918. throw $ex;
  919. }
  920. }
  921. /**
  922. * Update RSS feed entry in the database.
  923. *
  924. * @param rssObj $obj
  925. */
  926. public function updateRssfeed(rssObj $obj) {
  927. try {
  928. $this->SQL->updateRssfeed($obj);
  929. } catch (Exception $ex) {
  930. throw $ex;
  931. }
  932. }
  933. /**
  934. * Get all RSS feeds by specified user ID. Returns an array containing all RSS feeds
  935. *
  936. * @param int $user_id
  937. * @return array
  938. */
  939. public function getRssFeedsByUserId($user_id) {
  940. try {
  941. return $this->SQL->getRssFeedsByUserId($user_id);
  942. } catch (Exception $ex) {
  943. throw $ex;
  944. }
  945. }
  946. /**
  947. * Delete specified RSS feed from database
  948. *
  949. * @param int $feed_id
  950. */
  951. public function delFeed($feed_id) {
  952. try {
  953. if (!is_numeric($feed_id)) {
  954. throw new VCDInvalidArgumentException('Feed Id must be numeric');
  955. }
  956. $this->SQL->delFeed($feed_id);
  957. } catch (Exception $ex) {
  958. throw $ex;
  959. }
  960. }
  961. /* Wishlist */
  962. /**
  963. * Add a new movie to user's wishlist
  964. *
  965. * @param int $vcd_id
  966. * @param int $user_id
  967. */
  968. public function addToWishList($vcd_id, $user_id) {
  969. try {
  970. if (!(is_numeric($user_id) && is_numeric($vcd_id))) {
  971. throw new VCDInvalidArgumentException('Movie Id and User Id must be numeric');
  972. }
  973. $this->SQL->addToWishList($vcd_id, $user_id);
  974. } catch (Exception $ex) {
  975. throw $ex;
  976. }
  977. }
  978. /**
  979. * Get user's wishlist
  980. *
  981. * @param int $user_id
  982. * @return array
  983. */
  984. public function getWishList($user_id) {
  985. try {
  986. if (!is_numeric($user_id)) {
  987. throw new VCDInvalidArgumentException('User Id must be numeric');
  988. }
  989. $wishlistArr = $this->SQL->getWishList($user_id);
  990. if (VCDUtils::isLoggedIn() && VCDUtils::getUserID() != $user_id) {
  991. // User is view-ing others wishlist, lets check if user owns movies from this wishlist
  992. $ArrVCDids = $this->SQL->getVCDIDsByUser(VCDUtils::getUserID());
  993. if (is_array($ArrVCDids) && sizeof($ArrVCDids) > 0) {
  994. // Loop through the list
  995. $comparedArr = array();
  996. if (sizeof($wishlistArr) > 0) {
  997. foreach ($wishlistArr as $item) {
  998. $iown = 0;
  999. if (in_array($item['id'], $ArrVCDids)) {
  1000. $iown = 1;
  1001. }
  1002. array_push($comparedArr, array('id' => $item['id'], 'title' => $item['title'], 'mine' => $iown));
  1003. }
  1004. }
  1005. unset($wishlistArr);
  1006. unset($ArrVCDids);
  1007. return $comparedArr;
  1008. }
  1009. } else {
  1010. return $wishlistArr;
  1011. }
  1012. } catch (Exception $ex) {
  1013. throw $ex;
  1014. }
  1015. }
  1016. /**
  1017. * Check if the specified movie is on user's wishlist
  1018. *
  1019. * @param int $vcd_id
  1020. * @return bool
  1021. */
  1022. public function isOnWishList($vcd_id) {
  1023. try {
  1024. if (!is_numeric($vcd_id)) {
  1025. throw new VCDInvalidArgumentException('Movie Id must be numeric');
  1026. }
  1027. $user_id = VCDUtils::getUserID();
  1028. if (is_null($user_id)) {
  1029. throw new VCDException('User is not logged in.');
  1030. }
  1031. return $this->SQL->isOnWishList($vcd_id, $user_id);
  1032. } catch (Exception $ex) {
  1033. throw $ex;
  1034. }
  1035. }
  1036. /**
  1037. * Remove movie from user's wishlist
  1038. *
  1039. * @param int $vcd_id
  1040. * @param int $user_id
  1041. */
  1042. public function removeFromWishList($vcd_id, $user_id) {
  1043. try {
  1044. if (!(is_numeric($user_id) && is_numeric($vcd_id))) {
  1045. throw new VCDInvalidArgumentException('Movie Id and User Id must be numeric');
  1046. }
  1047. $this->SQL->removeFromWishList($vcd_id, $user_id);
  1048. } catch (Exception $ex) {
  1049. throw $ex;
  1050. }
  1051. }
  1052. /**
  1053. * See if any public wishlists are available
  1054. *
  1055. * @param int $user_id
  1056. * @return bool
  1057. */
  1058. public function isPublicWishLists($user_id) {
  1059. try {
  1060. if (is_numeric($user_id)) {
  1061. $propertyObj = $this->User()->getPropertyByKey(vcd_user::$PROPERTY_WISHLIST);
  1062. if ($propertyObj instanceof userPropertiesObj ) {
  1063. return $this->SQL->isPublicWishLists($user_id, $propertyObj->getpropertyID());
  1064. }
  1065. }
  1066. return false;
  1067. } catch (Exception $ex) {
  1068. throw $ex;
  1069. }
  1070. }
  1071. /* Comments */
  1072. /**
  1073. * Add a comment to specified movie
  1074. *
  1075. * @param commentObj $obj
  1076. */
  1077. public function addComment(commentObj $obj) {
  1078. try {
  1079. $this->SQL->addComment($obj);
  1080. } catch (Exception $ex) {
  1081. throw $ex;
  1082. }
  1083. }
  1084. /**
  1085. * Delete commnent from database.
  1086. *
  1087. * @param int $comment_id
  1088. */
  1089. public function deleteComment($comment_id) {
  1090. try {
  1091. if (!is_numeric($comment_id)) {
  1092. throw new VCDInvalidArgumentException('Comment Id must be numeric');
  1093. }
  1094. $this->SQL->deleteComment($comment_id);
  1095. } catch (Exception $ex) {
  1096. throw $ex;
  1097. }
  1098. }
  1099. /**
  1100. * Get a comment by ID
  1101. *
  1102. * @param int $comment_id
  1103. * @return commentObj
  1104. */
  1105. public function getCommentByID($comment_id) {
  1106. try {
  1107. if (!is_numeric($comment_id)) {
  1108. throw new VCDInvalidArgumentException('Comment Id must be numeric');
  1109. }
  1110. return $this->SQL->getCommentByID($comment_id);
  1111. } catch (Exception $ex) {
  1112. throw $ex;
  1113. }
  1114. }
  1115. /**
  1116. * Get all comments by user ID, returns array of comment objects
  1117. *
  1118. * @param int $user_id
  1119. * @return array
  1120. */
  1121. public function getAllCommentsByUserID($user_id) {
  1122. try {
  1123. if (!is_numeric($user_id)) {
  1124. throw new VCDInvalidArgumentException('User Id must be numeric');
  1125. }
  1126. return $this->SQL->getAllCommentsByUserID($user_id);
  1127. } catch (Exception $ex) {
  1128. throw $ex;
  1129. }
  1130. }
  1131. /**
  1132. * Get all comments for specified movie. Returns array of comment objects.
  1133. *
  1134. * @param int $vcd_id
  1135. * @return array
  1136. */
  1137. public function getAllCommentsByVCD($vcd_id) {
  1138. try {
  1139. if (!is_numeric($vcd_id)) {
  1140. throw new VCDInvalidArgumentException('Movie Id must be numeric');
  1141. }
  1142. return $this->SQL->getAllCommentsByVCD($vcd_id);
  1143. } catch (Exception $ex) {
  1144. throw $ex;
  1145. }
  1146. }
  1147. /**
  1148. * Get site statistics.
  1149. *
  1150. * @return statisticsObj
  1151. */
  1152. public function getStatsObj() {
  1153. try {
  1154. $obj = $this->SQL->getStatsObj();
  1155. $maxRecords = 6;
  1156. $arrAllCats = $obj->getBiggestCats();
  1157. $arrMonCats = $obj->getBiggestMonhtlyCats();
  1158. $obj->resetCategories();
  1159. $counter = 0;
  1160. $arrMonCatObjs = array();
  1161. foreach ($arrMonCats as $item) {
  1162. if ($counter >= $maxRecords) { break; }
  1163. $currObj = $this->getMovieCategoryByID($item[0]);
  1164. $currObj->setCategoryCount($item[1]);
  1165. array_push($arrMonCatObjs, $currObj);
  1166. $counter++;
  1167. }
  1168. $obj->setBiggestMonhtlyCats($arrMonCatObjs);
  1169. $counter = 0;
  1170. $arrCatObjs = array();
  1171. foreach ($arrAllCats as $itemArr) {
  1172. if ($counter >= $maxRecords) { break; }
  1173. $catObj = $this->getMovieCategoryByID($itemArr[0]);
  1174. $catObj->setCategoryCount($itemArr[1]);
  1175. array_push($arrCatObjs, $catObj);
  1176. $counter++;
  1177. }
  1178. $obj->setBiggestCats($arrCatObjs);
  1179. return $obj;
  1180. } catch (Exception $ex) {
  1181. throw $ex;
  1182. }
  1183. }
  1184. /**
  1185. * Return a array containing 3 arrays with statistics about users movies.
  1186. *
  1187. * @param int $user_id
  1188. * @return array
  1189. */
  1190. public function getUserStatistics($user_id) {
  1191. try {
  1192. if (!is_numeric($user_id)) {
  1193. throw new VCDInvalidArgumentException('User Id must be numeric');
  1194. }
  1195. return $this->SQL->getUserStatistics($user_id);
  1196. } catch (Exception $ex) {
  1197. throw $ex;
  1198. }
  1199. }
  1200. /* Metadata objects */
  1201. /**
  1202. * Add metadata to database. Param $arrObj can either be metadataObj or an array of metadata objects.
  1203. * If metadata object with same record_id, user_id and metadata name exists already, that
  1204. * entry is updated instead of inserting duplicate record.
  1205. *
  1206. * @param mixed $arrObj
  1207. * @param bool $forceCheck | Force to check the mediatypeID field also.
  1208. */
  1209. public function addMetadata($arrObj, $forceCheck = true) {
  1210. try {
  1211. if ($forceCheck) {
  1212. if (is_array($arrObj)) {
  1213. foreach ($arrObj as $metaObj) {
  1214. $this->addMetadata($metaObj, true);
  1215. }
  1216. } else {
  1217. if (!$arrObj instanceof metadataObj ) {
  1218. throw new VCDProgramException('Expected metadata object.');
  1219. }
  1220. // Check for new metadataObj that is allowed duplicate
  1221. if (is_null($arrObj->getMetadataID()) && $arrObj->isDuplicatesAllowed()) {
  1222. if (strcmp(trim($arrObj->getMetadataValue()), "") != 0) {
  1223. $this->SQL->addMetadata($arrObj);
  1224. return;
  1225. }
  1226. }
  1227. // If metadataObj already has metadata_id update is called
  1228. if (!is_null($arrObj->getMetadataID()) && is_numeric($arrObj->getMetadataID())) {
  1229. $this->updateMetadata($arrObj);
  1230. return;
  1231. }
  1232. $oldArr = $this->getMetadata($arrObj->getRecordID(), $arrObj->getUserID(), $arrObj->getMetadataName(), $arrObj->getmediaTypeID());
  1233. $oldObj = null;
  1234. if (is_array($oldArr) && sizeof($oldArr) == 1) {
  1235. $oldObj = $oldArr[0];
  1236. $arrObj->setMetadataID($oldObj->getMetadataID());
  1237. $this->updateMetadata($arrObj);
  1238. } else {
  1239. // if the data is empty .. we return without throwing an error.
  1240. if (strcmp(trim($arrObj->getMetadataValue()), "") != 0) {
  1241. $this->SQL->addMetadata($arrObj);
  1242. }
  1243. }
  1244. }
  1245. return;
  1246. }
  1247. if ($arrObj instanceof metadataObj ) {
  1248. $oldObj = $this->getMetadata($arrObj->getRecordID(), $arrObj->getUserID(), $arrObj->getMetadataName());
  1249. if (is_array($oldObj) && sizeof($oldObj) == 1) {
  1250. $arrObj->setMetadataID($oldObj[0]->getMetadataID());
  1251. $this->updateMetadata($arrObj);
  1252. } else {
  1253. // do we have a valid metadataTypeObj parent ?
  1254. if ($arrObj->getMetadataTypeID() == -1) {
  1255. // not a valid parent, lets construct it
  1256. $metaTypeObj = $arrObj->getMetaDataTypeInstance();
  1257. $metaTypeObj = $this->addMetaDataType($metaTypeObj);
  1258. $arrObj->setMetaDataTypeID($metaTypeObj->getMetadataTypeID());
  1259. }
  1260. if (strcmp(trim($arrObj->getMetadataValue()), "") != 0) {
  1261. $this->SQL->addMetadata($arrObj);
  1262. }
  1263. }
  1264. } elseif (is_array($arrObj)) {
  1265. foreach ($arrObj as $metaObj) {
  1266. $oldObj = null;
  1267. $arr = $this->getMetadata($metaObj->getRecordID(), $metaObj->getUserID(), $metaObj->getMetadataName());
  1268. if (is_array($arr) && sizeof($arr) == 1) {
  1269. $oldObj = $arr[0];
  1270. }
  1271. if ($metaObj instanceof metadataObj ) {
  1272. if ($oldObj instanceof metadataObj) {
  1273. $metaObj->setMetadataID($oldObj->getMetadataID());
  1274. $this->updateMetadata($metaObj);
  1275. } else {
  1276. // do we have a valid metadataTypeObj parent ?
  1277. if ($metaObj->getMetadataTypeID() == -1) {
  1278. // not a valid parent, lets construct it
  1279. $metaTypeObj = $arrObj->getMetaDataTypeInstance();
  1280. $metaTypeObj = $this->addMetaDataType($metaTypeObj);
  1281. $arrObj->setMetaDataTypeID($metaTypeObj->getMetadataTypeID());
  1282. }
  1283. if (strcmp(trim($metaObj->getMetadataValue()), "") != 0) {
  1284. $this->SQL->addMetadata($metaObj);
  1285. }
  1286. }
  1287. }
  1288. }
  1289. }
  1290. } catch (Exception $ex) {
  1291. throw $ex;
  1292. }
  1293. }
  1294. /**
  1295. * Update metadata object
  1296. *
  1297. * @param metadataObj $obj
  1298. */
  1299. public function updateMetadata(metadataObj $obj) {
  1300. try {
  1301. $this->SQL->updateMetadata($obj);
  1302. } catch (Exception $ex) {
  1303. throw $ex;
  1304. }
  1305. }
  1306. /**
  1307. * Update metadata type object
  1308. *
  1309. * @param metadataTypeObj $obj
  1310. */
  1311. public function updateMetadataType(metadataTypeObj $obj) {
  1312. try {
  1313. $this->SQL->updateMetadataType($obj);
  1314. } catch (Exception $ex) {
  1315. throw $ex;
  1316. }
  1317. }
  1318. /**
  1319. * Delete metadata object
  1320. *
  1321. * @param int $metadata_id
  1322. */
  1323. public function deleteMetadata($metadata_id) {
  1324. try {
  1325. if (!is_numeric($metadata_id)) {
  1326. throw new VCDInvalidArgumentException('Metadata Id must be numeric');
  1327. }
  1328. // Check if user has rights to delete this metadata ..
  1329. $metaDataObj = $this->SQL->getMetadataById($metadata_id);
  1330. if ($metaDataObj instanceof metadataObj ) {
  1331. if ($metaDataObj->getUserID() == VCDUtils::getUserID() ) {
  1332. $this->SQL->deleteMetadata($metadata_id);
  1333. } else {
  1334. throw new VCDConstraintException('You do not have rights to delete this metadata entry.');
  1335. }
  1336. }
  1337. } catch (Exception $ex) {
  1338. throw $ex;
  1339. }
  1340. }
  1341. /**
  1342. * Get specific metadata. If an entry with specific parameters does not exists,
  1343. * null is returned. Otherwise array of metadata is returned
  1344. *
  1345. * @param int $record_id
  1346. * @param int $user_id
  1347. * @param int $metadata_name
  1348. * @param int $mediatype_id | MediaType ID of movieObj. This forces deeper check.
  1349. * @return array
  1350. */
  1351. public function getMetadata($record_id, $user_id = null, $metadata_name, $mediatype_id = null) {
  1352. try {
  1353. if (is_numeric($record_id) && is_numeric($user_id)) {
  1354. // reverse metadataObj SYS constant to correct string
  1355. if (is_numeric($metadata_name)) {
  1356. $mappingName = metadataTypeObj::getSystemTypeMapping($metadata_name);
  1357. if ($mappingName) {
  1358. return $this->SQL->getMetadata($record_id, $user_id, $mappingName, $mediatype_id);
  1359. } else {
  1360. throw new VCDProgramException('System mapping for metadataType not found.');
  1361. }
  1362. } else {
  1363. return $this->SQL->getMetadata($record_id, $user_id, $metadata_name, $mediatype_id);
  1364. }
  1365. } else {
  1366. if (is_numeric($record_id)) {
  1367. // Get metadata only based on the Record ID
  1368. return $this->SQL->getMetadata($record_id, $user_id, $metadata_name, $mediatype_id);
  1369. } else {
  1370. return null;
  1371. }
  1372. }
  1373. } catch (Exception $ex) {
  1374. throw $ex;
  1375. }
  1376. }
  1377. /**
  1378. * Get single MetadataObject by metadata ID
  1379. *
  1380. * @param int $metadata_id | The MetaData ID
  1381. * @return metadataObj
  1382. */
  1383. public function getMetadataById($metadata_id) {
  1384. try {
  1385. if (!is_numeric($metadata_id)) {
  1386. throw new VCDInvalidArgumentException('Metadata Id must be numeric');
  1387. }
  1388. return $this->SQL->getMetadataById($metadata_id);
  1389. } catch (Exception $ex) {
  1390. throw $ex;
  1391. }
  1392. }
  1393. /**
  1394. * Get an array of all records id's in metadata objects belonging to specified user_id and metadata_name.
  1395. *
  1396. * @param int $user_id
  1397. * @param string $metadata_name
  1398. * @return array
  1399. */
  1400. public function getRecordIDsByMetadata($user_id, $metadata_name) {
  1401. try {
  1402. if (is_numeric($user_id) && strcmp($metadata_name, "") != 0) {
  1403. if (is_numeric($metadata_name)) {
  1404. $sysName = metadataTypeObj::getSystemTypeMapping($metadata_name);
  1405. if ($sysName) {
  1406. return $this->SQL->getRecordIDsByMetadata($user_id, $sysName);
  1407. } else {
  1408. return null;
  1409. }
  1410. } else {
  1411. return $this->SQL->getRecordIDsByMetadata($user_id, $metadata_name);
  1412. }
  1413. }
  1414. return null;
  1415. } catch (Exception $ex) {
  1416. throw $ex;
  1417. }
  1418. }
  1419. /**
  1420. * Add a new metadataTypeObj to the database. The updated metadataTypeObj is then returned.
  1421. *
  1422. * @param metadataTypeObj $obj
  1423. * @return metadataTypeObj
  1424. */
  1425. public function addMetaDataType(metadataTypeObj $obj) {
  1426. try {
  1427. return $this->SQL->addMetaDataType($obj);
  1428. } catch (Exception $ex) {
  1429. throw $ex;
  1430. }
  1431. }
  1432. /**
  1433. * Get a metadatatypes from database. If name is provided, search by name,
  1434. * otherwise search by id. Function returns a metadataTypeObject.
  1435. *
  1436. * @param string $name | The metadata type name
  1437. * @param int $id | The metadata id
  1438. * @return metadataTypeObj
  1439. */
  1440. public function getMetadataType($name = null, $id = null) {
  1441. try {
  1442. return $this->SQL->getMetadataType($name, $id);
  1443. } catch (Exception $ex) {
  1444. throw $ex;
  1445. }
  1446. }
  1447. /**
  1448. * Get all known metadatatypes from database. If $user_id is provided, only metadatatypes created by that
  1449. * user_id will be returned. Function returns array of metadataTypeObjects.
  1450. *
  1451. * @param int $user_id | The user_id to filter metadatatypes to, null = no filter
  1452. * @return array
  1453. */
  1454. public function getMetadataTypes($user_id = null) {
  1455. try {
  1456. return $this->SQL->getMetadataTypes($user_id);
  1457. } catch (Exception $ex) {
  1458. throw $ex;
  1459. }
  1460. }
  1461. /**
  1462. * Delete metadataType object. Only user defined metadata can be deleted by it's creator.
  1463. *
  1464. * @param int $metatype_id | The metadataType Id to delete
  1465. */
  1466. public function deleteMetaDataType($metatype_id) {
  1467. try {
  1468. if (is_numeric($metatype_id)) {
  1469. // Check if the user trying to delete the object is actually the owner of the metadataType.
  1470. $canDelete = false;
  1471. $metaArr = $this->getMetadataTypes(VCDUtils::getUserID());
  1472. foreach ($metaArr as $metatypeObj) {
  1473. if ($metatypeObj->getMetadataTypeID() === $metatype_id || VCDAuthentication::isAdmin()) {
  1474. $canDelete = true;
  1475. break;
  1476. }
  1477. }
  1478. if ($canDelete) {
  1479. $this->SQL->deleteMetaDataType($metatype_id);
  1480. } else {
  1481. throw new VCDConstraintException('You are not the owner of this entry, aborting.');
  1482. }
  1483. }
  1484. } catch (Exception $ex) {
  1485. throw $ex;
  1486. }
  1487. }
  1488. /**
  1489. * Delete NFO Metadata from DB and delete the file aswell from filelevel.
  1490. *
  1491. * @param int $metadata_id | The metadata ID to delete
  1492. */
  1493. public function deleteNFO($metadata_id) {
  1494. try {
  1495. if (VCDUtils::isLoggedIn() && is_numeric($metadata_id)) {
  1496. // Get the metadata Object
  1497. $metaObj = $this->getMetadataById($metadata_id);
  1498. // Check if the logged in user is actually the owner of the file
  1499. if ($metaObj instanceof metadataObj && $metaObj->getUserID() == VCDUtils::getUserID()) {
  1500. // But before we delete the NFO file, make sure no one else is linking to it ..
  1501. $useCount = $this->SQL->getMetadataValueCount($metaObj);
  1502. if (is_numeric($useCount) && $useCount == 1) {
  1503. // No one else is using this NFO, safe to delete
  1504. // Delete the file from filelevel
  1505. $filename = NFO_PATH . $metaObj->getMetadataValue();
  1506. fs_unlink($filename);
  1507. }
  1508. // Delete the metadataObj from DB
  1509. $this->deleteMetadata($metadata_id);
  1510. } else {
  1511. throw new VCDConstraintException('You do not have access to delete this file.');
  1512. }
  1513. }
  1514. } catch (Exception $ex) {
  1515. throw $ex;
  1516. }
  1517. }
  1518. /*
  1519. Private functions below
  1520. */
  1521. /**
  1522. * Update the internal settings cache.
  1523. *
  1524. */
  1525. private function updateCache() {
  1526. try {
  1527. $this->settingsArray = $this->SQL->getAllSettings();
  1528. } catch (Exception $ex) {
  1529. throw $ex;
  1530. }
  1531. }
  1532. /**
  1533. * Update the internal media type cache
  1534. *
  1535. */
  1536. private function updateMediaCache() {
  1537. $this->mediatypeArray = array();
  1538. $this->mediatypeFullArray = array();
  1539. $arrAllMedia = $this->SQL->getAllMediaTypes();
  1540. // filter out the parent
  1541. foreach ($arrAllMedia as $mediaTypeObj) {
  1542. array_push($this->mediatypeFullArray, $mediaTypeObj);
  1543. if ($mediaTypeObj->isParent()) {
  1544. // Get it's children
  1545. foreach ($arrAllMedia as $childObj) {
  1546. if ($mediaTypeObj->getmediaTypeID() == $childObj->getParentID()) {
  1547. $mediaTypeObj->addChild($childObj);
  1548. }
  1549. }
  1550. array_push($this->mediatypeArray, $mediaTypeObj);
  1551. }
  1552. }
  1553. unset($arrAllMedia);
  1554. }
  1555. /**
  1556. * Update the internal category cache.
  1557. *
  1558. */
  1559. private function updateCategoryCache() {
  1560. try {
  1561. $this->moviecategoryArray = $this->SQL->getAllMovieCategories();
  1562. } catch (Exception $ex) {
  1563. throw $ex;
  1564. }
  1565. }
  1566. /**
  1567. * Update the internal sourceSite obj cache.
  1568. *
  1569. */
  1570. private function updateSiteCache() {
  1571. if (is_null($this->sourcesiteArray)) {
  1572. $this->sourcesiteArray = $this->SQL->getSourceSites();
  1573. }
  1574. }
  1575. /**
  1576. * Update the internal Borrowers object cache.
  1577. *
  1578. * @param int $user_id
  1579. */
  1580. private function updateBorrowersCache($user_id) {
  1581. if (is_null($this->borrowersArray)) {
  1582. $this->borrowersArray = $this->SQL->getBorrowersByUserID($user_id);
  1583. }
  1584. }
  1585. /**
  1586. * Check for duplicate keys in the settings objects.
  1587. *
  1588. * @param settingsObj $settingsObj
  1589. * @return bool
  1590. */
  1591. private function checkDuplicates($settingsObj) {
  1592. if (is_null($this->settingsArray))
  1593. $this->updateCache();
  1594. foreach ($this->settingsArray as $cacheObj) {
  1595. if (strcmp(strtoupper($settingsObj->getKey()),strtoupper($cacheObj->getKey())) == 0) {
  1596. return true;
  1597. }
  1598. }
  1599. return false;
  1600. }
  1601. /**
  1602. * Get an instance of the vcd_user class
  1603. *
  1604. * @return vcd_user
  1605. */
  1606. private function User() {
  1607. return VCDClassFactory::getInstance('vcd_user');
  1608. }
  1609. }
  1610. ?>