PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/class/class.set.php

http://candydolldb.googlecode.com/
PHP | 648 lines | 410 code | 88 blank | 150 comment | 62 complexity | 8024ad922647ff49b2057b266bce0a3a MD5 | raw file
  1. <?php
  2. class Set
  3. {
  4. private $ID;
  5. private $Model;
  6. private $Prefix;
  7. private $Name;
  8. private $DatesPic = array();
  9. private $DatesVid = array();
  10. private $ContainsWhat = SET_CONTENT_NONE;
  11. private $AmountPicsInDB = 0;
  12. private $AmountVidsInDB = 0;
  13. /**
  14. * @param int $set_id
  15. * @param string $set_prefix
  16. * @param string $set_name
  17. * @param int $set_containswhat
  18. * @param int $model_id
  19. * @param string $model_firstname
  20. * @param string $model_lastname
  21. * @param int $set_amount_pics_in_db
  22. * @param int $set_amount_vids_in_db
  23. */
  24. public function __construct(
  25. $set_id = NULL, $set_prefix = NULL, $set_name = NULL, $set_containswhat = SET_CONTENT_NONE,
  26. $model_id = NULL, $model_firstname = NULL, $model_lastname = NULL,
  27. $set_amount_pics_in_db = 0, $set_amount_vids_in_db = 0)
  28. {
  29. $this->ID = $set_id;
  30. $this->Prefix = $set_prefix;
  31. $this->Name = $set_name;
  32. $this->ContainsWhat = $set_containswhat;
  33. /* @var $m Model */
  34. $m = new Model($model_id, $model_firstname, $model_lastname);
  35. $this->Model = $m;
  36. $this->AmountPicsInDB = $set_amount_pics_in_db;
  37. $this->AmountVidsInDB = $set_amount_vids_in_db;
  38. }
  39. /**
  40. * @return int
  41. */
  42. public function getID()
  43. { return $this->ID; }
  44. /**
  45. * @param int $ID
  46. */
  47. public function setID($ID)
  48. { $this->ID = $ID; }
  49. /**
  50. * @return Model
  51. */
  52. public function getModel()
  53. { return $this->Model; }
  54. /**
  55. * @return int
  56. */
  57. public function getModelID()
  58. { return $this->Model ? $this->Model->getID() : NULL; }
  59. /**
  60. * @param int $Model
  61. */
  62. public function setModel($Model)
  63. { $this->Model = $Model; }
  64. /**
  65. * @return string
  66. */
  67. public function getName()
  68. { return $this->Name; }
  69. /**
  70. * @param string $Name
  71. */
  72. public function setName($Name)
  73. { $this->Name = $Name; }
  74. /**
  75. * @return string
  76. */
  77. public function getPrefix()
  78. { return $this->Prefix; }
  79. /**
  80. * @param string $Prefix
  81. */
  82. public function setPrefix($Prefix)
  83. { $this->Prefix = $Prefix; }
  84. /**
  85. * @return array(Date)
  86. */
  87. public function getDatesPic()
  88. { return $this->DatesPic; }
  89. /**
  90. * @param array(Date) $DatesPic
  91. */
  92. public function setDatesPic($DatesPic)
  93. { $this->DatesPic = $DatesPic; }
  94. /**
  95. * @return array(Date)
  96. */
  97. public function getDatesVid()
  98. { return $this->DatesVid; }
  99. /**
  100. * @param array(Date) $DatesVid
  101. */
  102. public function setDatesVid($DatesVid)
  103. { $this->DatesVid = $DatesVid; }
  104. /**
  105. * @return int
  106. */
  107. public function getContainsWhat()
  108. { return $this->ContainsWhat; }
  109. /**
  110. * @param int $What
  111. */
  112. public function setContainsWhat($What)
  113. { $this->ContainsWhat = $What; }
  114. /**
  115. * @return int
  116. */
  117. public function getAmountPicsInDB()
  118. { return $this->AmountPicsInDB; }
  119. /**
  120. * @param int $AmountPicsInDB
  121. */
  122. public function setAmountPicsInDB($AmountPicsInDB)
  123. { $this->AmountPicsInDB = $AmountPicsInDB; }
  124. /**
  125. * @return int
  126. */
  127. public function getAmountVidsInDB()
  128. { return $this->AmountVidsInDB; }
  129. /**
  130. * @param int $AmountVidsInDB
  131. */
  132. public function setAmountVidsInDB($AmountVidsInDB)
  133. { $this->AmountVidsInDB = $AmountVidsInDB; }
  134. /**
  135. * @return bool
  136. */
  137. public function getSetIsDirtyPic()
  138. {
  139. if(($this->ContainsWhat & SET_CONTENT_IMAGE) > 0)
  140. { return (($this->getAmountPicsInDB() % 25 != 0) || $this->getAmountPicsInDB() == 0); }
  141. else
  142. { return FALSE; }
  143. }
  144. /**
  145. * @return bool
  146. */
  147. public function getSetIsDirtyVid()
  148. {
  149. if(($this->ContainsWhat & SET_CONTENT_VIDEO) > 0)
  150. { return ($this->getAmountVidsInDB() < 1); }
  151. else
  152. { return FALSE; }
  153. }
  154. /**
  155. * @return bool
  156. */
  157. public function getSetIsDirty()
  158. {
  159. return ($this->getSetIsDirtyPic() || $this->getSetIsDirtyVid());
  160. }
  161. /**
  162. * Gets an array of Sets from the database, or NULL on failure.
  163. * @param SetSearchParameters $SearchParameters
  164. * @param string $OrderClause
  165. * @param string $LimitClause
  166. * @return Array(Set) | NULL
  167. */
  168. public static function GetSets($SearchParameters = NULL, $OrderClause = 'model_firstname ASC, model_lastname ASC, set_prefix ASC, set_name ASC', $LimitClause = NULL)
  169. {
  170. global $dbi;
  171. $SearchParameters = $SearchParameters ? $SearchParameters : new SetSearchParameters();
  172. $OrderClause = empty($OrderClause) ? 'model_firstname ASC, model_lastname ASC, set_prefix ASC, set_name ASC' : $OrderClause;
  173. $q = sprintf("
  174. SELECT
  175. `set_id`,`set_prefix`,`set_name`,`set_containswhat`,`model_id`,`model_firstname`,`model_lastname`,`set_amount_pics_in_db`,`set_amount_vids_in_db`
  176. FROM
  177. `vw_Set`
  178. WHERE
  179. mut_deleted = -1
  180. %1\$s
  181. ORDER BY
  182. %2\$s
  183. %3\$s",
  184. $SearchParameters->getWhere(),
  185. $OrderClause,
  186. $LimitClause ? ' LIMIT '.$LimitClause : NULL
  187. );
  188. if(!($stmt = $dbi->prepare($q)))
  189. {
  190. $e = new SQLerror($dbi->errno, $dbi->error);
  191. Error::AddError($e);
  192. return NULL;
  193. }
  194. DBi::BindParamsToSelect($SearchParameters, $stmt);
  195. if($stmt->execute())
  196. {
  197. $OutArray = array();
  198. $stmt->bind_result($set_id, $set_prefix, $set_name, $set_containswhat, $model_id, $model_firstname, $model_lastname, $set_amount_pics_in_db, $set_amount_vids_in_db);
  199. while($stmt->fetch())
  200. {
  201. $o = new self($set_id, $set_prefix, $set_name, $set_containswhat, $model_id, $model_firstname, $model_lastname, $set_amount_pics_in_db, $set_amount_vids_in_db);
  202. $OutArray[] = $o;
  203. }
  204. $stmt->close();
  205. return $OutArray;
  206. }
  207. else
  208. {
  209. $e = new SQLerror($dbi->errno, $dbi->error);
  210. Error::AddError($e);
  211. return NULL;
  212. }
  213. }
  214. /**
  215. * Inserts supplied Set into the database.
  216. * @param Set $Set
  217. * @param User $CurrentUser
  218. * @return bool
  219. */
  220. public static function Insert($Set, $CurrentUser)
  221. {
  222. return self::InsertMulti(array($Set), $CurrentUser);
  223. }
  224. /**
  225. * Inserts supplied Sets into the database.
  226. * @param array(Set) $Sets
  227. * @param User $CurrentUser
  228. * @return bool
  229. */
  230. public static function InsertMulti($Sets, $CurrentUser)
  231. {
  232. global $dbi;
  233. $outBool = TRUE;
  234. $mut_id = $CurrentUser->getID();
  235. $mut_date = time();
  236. if(!is_array($Sets))
  237. { return FALSE; }
  238. $q = sprintf("
  239. INSERT INTO `Set` (
  240. `model_id`,
  241. `set_prefix`,
  242. `set_name`,
  243. `set_containswhat`,
  244. `mut_id`,
  245. `mut_date`
  246. ) VALUES (
  247. ?, ?, ?, ?, ?, ?
  248. )
  249. ");
  250. if(!($stmt = $dbi->prepare($q)))
  251. {
  252. $e = new SQLerror($dbi->errno, $dbi->error);
  253. Error::AddError($e);
  254. return FALSE;
  255. }
  256. $stmt->bind_param('issiii',
  257. $model_id,
  258. $set_prefix,
  259. $set_name,
  260. $set_containswhat,
  261. $mut_id,
  262. $mut_date
  263. );
  264. foreach($Sets as $Set)
  265. {
  266. $model_id = $Set->getModelID();
  267. $set_prefix = $Set->getPrefix();
  268. $set_name = $Set->getName();
  269. $set_containswhat = $Set->getContainsWhat();
  270. $outBool = $stmt->execute();
  271. if($outBool)
  272. {
  273. $Set->setID($dbi->insert_id);
  274. }
  275. else
  276. {
  277. $e = new SQLerror($dbi->errno, $dbi->error);
  278. Error::AddError($e);
  279. }
  280. }
  281. $stmt->close();
  282. return $outBool;
  283. }
  284. /**
  285. * Updates the databaserecord of supplied Set.
  286. * @param Set $Set
  287. * @param User $CurrentUser
  288. * @return bool
  289. */
  290. public static function Update($Set, $CurrentUser)
  291. {
  292. return self::UpdateMulti(array($Set), $CurrentUser);
  293. }
  294. /**
  295. * Updates the databaserecords of supplied Sets.
  296. * @param array(Set) $Sets
  297. * @param User $CurrentUser
  298. * @return bool
  299. */
  300. public static function UpdateMulti($Sets, $CurrentUser)
  301. {
  302. global $dbi;
  303. $outBool = TRUE;
  304. $mut_id = $CurrentUser->getID();
  305. $mut_date = time();
  306. if(!is_array($Sets))
  307. { return FALSE; }
  308. $q = sprintf("
  309. UPDATE `Set` SET
  310. `model_id` = ?,
  311. `set_prefix` = ?,
  312. `set_name` = ?,
  313. `set_containswhat` = ?,
  314. `mut_id` = ?,
  315. `mut_date` = ?
  316. WHERE
  317. `set_id` = ?
  318. ");
  319. if(!($stmt = $dbi->prepare($q)))
  320. {
  321. $e = new SQLerror($dbi->errno, $dbi->error);
  322. Error::AddError($e);
  323. return FALSE;
  324. }
  325. $stmt->bind_param('issiiii',
  326. $model_id,
  327. $set_prefix,
  328. $set_name,
  329. $set_containswhat,
  330. $mut_id,
  331. $mut_date,
  332. $id
  333. );
  334. foreach($Sets as $Set)
  335. {
  336. $model_id = $Set->getModelID();
  337. $set_prefix = $Set->getPrefix();
  338. $set_name = $Set->getName();
  339. $set_containswhat = $Set->getContainsWhat();
  340. $id = $Set->getID();
  341. $outBool = $stmt->execute();
  342. if(!$outBool)
  343. {
  344. $e = new SQLerror($dbi->errno, $dbi->error);
  345. Error::AddError($e);
  346. }
  347. }
  348. $stmt->close();
  349. return $outBool;
  350. }
  351. /**
  352. * Removes the specified Set from the database.
  353. * @param Set $Set
  354. * @param User $CurrentUser
  355. * @return bool
  356. */
  357. public static function Delete($Set, $CurrentUser)
  358. {
  359. return self::DeleteMulti(array($Set), $CurrentUser);
  360. }
  361. /**
  362. * Removes the specified Sets from the database.
  363. * @param array(Set) $Sets
  364. * @param User $CurrentUser
  365. * @return bool
  366. */
  367. public static function DeleteMulti($Sets, $CurrentUser)
  368. {
  369. global $dbi;
  370. $outBool = TRUE;
  371. $mut_id = $CurrentUser->getID();
  372. $mut_deleted = time();
  373. if(!is_array($Sets))
  374. { return FALSE; }
  375. $q = sprintf("
  376. UPDATE `Set` SET
  377. `mut_id` = ?,
  378. `mut_deleted` = ?
  379. WHERE
  380. `set_id` = ?
  381. ");
  382. if(!($stmt = $dbi->prepare($q)))
  383. {
  384. $e = new SQLerror($dbi->errno, $dbi->error);
  385. Error::AddError($e);
  386. return FALSE;
  387. }
  388. $stmt->bind_param('iii',
  389. $mut_id,
  390. $mut_deleted,
  391. $id
  392. );
  393. foreach($Sets as $Set)
  394. {
  395. $id = $Set->getID();
  396. $outBool = $stmt->execute();
  397. if(!$outBool)
  398. {
  399. $e = new SQLerror($dbi->errno, $dbi->error);
  400. Error::AddError($e);
  401. }
  402. }
  403. $stmt->close();
  404. return $outBool;
  405. }
  406. /**
  407. * Filters an array of Sets, and returns only those who match the specified criteria.
  408. * @param array(Set) $SetArray
  409. * @param int $ModelID
  410. * @param int $SetID
  411. * @param string $Name
  412. * @param string $Prefix
  413. * @return array(Set)
  414. */
  415. public static function Filter($SetArray, $ModelID = NULL, $SetID = NULL, $Name = NULL, $Prefix = NULL)
  416. {
  417. $OutArray = array();
  418. /* @var $Set Set */
  419. foreach($SetArray as $Set)
  420. {
  421. if(
  422. (is_null($ModelID) || $Set->getModel()->getID() == $ModelID) &&
  423. (is_null($SetID) || $Set->getID() == $SetID) &&
  424. (is_null($Prefix) || strlen($Prefix) == 0 || $Set->getPrefix() == $Prefix) &&
  425. (
  426. is_null($Name) ||
  427. strlen($Name) == 0 ||
  428. $Set->getName() == $Name ||
  429. sprintf('%1$s%2$s', $Set->getModel()->GetShortName(), $Set->getName()) == $Name
  430. )
  431. ){
  432. $OutArray[] = $Set;
  433. }
  434. }
  435. return $OutArray;
  436. }
  437. /**
  438. * Return a concatenated, condensed string of all the array's values,
  439. * For example '1,2,3,4,6,7,8,10,13' becomes '1-8, 10, 13'.
  440. * @param array(Set) $inArray
  441. */
  442. public static function RangeString($inArray)
  443. {
  444. if(!is_array($inArray) || count($inArray) == 0){
  445. return NULL;
  446. }
  447. $s = count($inArray) == 1 ? 'Set ' : 'Sets ';
  448. for ($i = 0; $i < count($inArray); $i++)
  449. {
  450. /* @var $previousSet Set */
  451. /* @var $currentSet Set */
  452. /* @var $nextSet Set */
  453. $previousSet = $i == 0 ? NULL : $inArray[$i -1];
  454. $currenSet = $inArray[$i];
  455. $nextSet = $i == count($inArray)-1 ? NULL : $inArray[$i +1];
  456. if($previousSet == NULL){
  457. $s .= (int)$currenSet->getName();
  458. }
  459. else if(
  460. (int)$currenSet->getName() == ((int)$previousSet->getName()) + 1
  461. &&
  462. $nextSet != NULL
  463. &&
  464. (int)$currenSet->getName() == ((int)$nextSet->getName()) - 1)
  465. {
  466. continue;
  467. }
  468. else if(
  469. (int)$previousSet->getName() == ((int)$currenSet->getName()) -1)
  470. {
  471. $s .= '-'.((int)$currenSet->getName());
  472. }
  473. else if(
  474. (int)$previousSet->getName() != ((int)$currenSet->getName()) -1)
  475. {
  476. $s .= ', '.((int)$currenSet->getName());
  477. }
  478. }
  479. return $s;
  480. }
  481. /**
  482. * @param Set $m
  483. * @param Set $n
  484. */
  485. public static function CompareAsc($m, $n)
  486. {
  487. if($m->getName() == $n->getName()){
  488. return 0;
  489. }
  490. $mNumeric = preg_match('/^[0-9]{2,3}$/', $m->getName());
  491. $nNumeric = preg_match('/^[0-9]{2,3}$/', $n->getName());
  492. if(($mNumeric && $nNumeric) || (!$mNumeric && !$nNumeric))
  493. {
  494. return strnatcasecmp($m->getName(), $n->getName());
  495. }
  496. else
  497. {
  498. return ($mNumeric ? 1 : -1);
  499. }
  500. }
  501. }
  502. class SetSearchParameters extends SearchParameters
  503. {
  504. private $paramtypes = '';
  505. private $values = array();
  506. private $where = '';
  507. /**
  508. * @param int $SingleID
  509. * @param array(int) $MultipleIDs
  510. * @param int $SingleModelID
  511. * @param array(int) $MultipleModelIDs
  512. * @param string $ModelFullName
  513. */
  514. public function __construct(
  515. $SingleID = FALSE, $MultipleIDs = FALSE, $SingleModelID = FALSE, $MultipleModelIDs = FALSE, $ModelFullName = FALSE)
  516. {
  517. parent::__construct();
  518. if($SingleID !== FALSE)
  519. {
  520. $this->paramtypes .= "i";
  521. $this->values[] = $SingleID;
  522. $this->where .= " AND set_id = ?";
  523. }
  524. if(is_array($MultipleIDs) && count($MultipleIDs) > 0)
  525. {
  526. $this->paramtypes .= str_repeat('i', count($MultipleIDs));
  527. $this->values = array_merge($this->values, $MultipleIDs);
  528. $this->where .= sprintf(" AND set_id IN ( %1s ) ",
  529. implode(', ', array_fill(0, count($MultipleIDs), '?'))
  530. );
  531. }
  532. if($SingleModelID !== FALSE)
  533. {
  534. $this->paramtypes .= "i";
  535. $this->values[] = $SingleModelID;
  536. $this->where .= " AND model_id = ?";
  537. }
  538. if(is_array($MultipleModelIDs) && count($MultipleModelIDs) > 0)
  539. {
  540. $this->paramtypes .= str_repeat('i', count($MultipleModelIDs));
  541. $this->values = array_merge($this->values, $MultipleModelIDs);
  542. $this->where .= sprintf(" AND model_id IN ( %1s ) ",
  543. implode(', ', array_fill(0, count($MultipleModelIDs), '?'))
  544. );
  545. }
  546. if($ModelFullName !== FALSE)
  547. {
  548. $this->paramtypes .= 's';
  549. $this->values[] = '%'.$ModelFullName.'%';
  550. $this->where .= " AND CONCAT_WS(' ', model_firstname, model_lastname) LIKE ?";
  551. }
  552. }
  553. public function getWhere()
  554. { return $this->where; }
  555. public function getValues()
  556. { return $this->values; }
  557. public function getParamTypes()
  558. { return $this->paramtypes; }
  559. }
  560. ?>