PageRenderTime 70ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

/ojs/ojs-2.3.0/classes/subscription/SubscriptionTypeDAO.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 478 lines | 284 code | 72 blank | 122 comment | 21 complexity | 19e31c3946528c5e66f5a24b62ae777d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file classes/subscription/SubscriptionTypeDAO.inc.php
  4. *
  5. * Copyright (c) 2003-2009 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class SubscriptionTypeDAO
  9. * @ingroup subscription
  10. * @see SubscriptionType
  11. *
  12. * @brief Operations for retrieving and modifying SubscriptionType objects.
  13. */
  14. // $Id: SubscriptionTypeDAO.inc.php,v 1.29 2009/08/19 16:28:32 michael Exp $
  15. import('subscription.SubscriptionType');
  16. class SubscriptionTypeDAO extends DAO {
  17. /**
  18. * Retrieve a subscription type by ID.
  19. * @param $typeId int
  20. * @return SubscriptionType
  21. */
  22. function &getSubscriptionType($typeId) {
  23. $result =& $this->retrieve(
  24. 'SELECT * FROM subscription_types WHERE type_id = ?', $typeId
  25. );
  26. $returner = null;
  27. if ($result->RecordCount() != 0) {
  28. $returner =& $this->_returnSubscriptionTypeFromRow($result->GetRowAssoc(false));
  29. }
  30. $result->Close();
  31. unset($result);
  32. return $returner;
  33. }
  34. /**
  35. * Retrieve subscription type journal ID by ID.
  36. * @param $typeId int
  37. * @return int
  38. */
  39. function getSubscriptionTypeJournalId($typeId) {
  40. $result =& $this->retrieve(
  41. 'SELECT journal_id FROM subscription_types WHERE type_id = ?', $typeId
  42. );
  43. $returner = isset($result->fields[0]) ? $result->fields[0] : false;
  44. $result->Close();
  45. unset($result);
  46. return $returner;
  47. }
  48. /**
  49. * Retrieve subscription type name by ID.
  50. * @param $typeId int
  51. * @return string
  52. */
  53. function getSubscriptionTypeName($typeId) {
  54. $result =& $this->retrieve(
  55. 'SELECT COALESCE(l.setting_value, p.setting_value) FROM subscription_type_settings l LEFT JOIN subscription_type_settings p ON (p.type_id = ? AND p.setting_name = ? AND p.locale = ?) WHERE l.type_id = ? AND l.setting_name = ? AND l.locale = ?',
  56. array(
  57. $typeId, 'name', Locale::getLocale(),
  58. $typeId, 'name', Locale::getPrimaryLocale()
  59. )
  60. );
  61. $returner = isset($result->fields[0]) ? $result->fields[0] : false;
  62. $result->Close();
  63. unset($result);
  64. return $returner;
  65. }
  66. /**
  67. * Retrieve institutional flag by ID.
  68. * @param $typeId int
  69. * @return int
  70. */
  71. function getSubscriptionTypeInstitutional($typeId) {
  72. $result =& $this->retrieve(
  73. 'SELECT institutional FROM subscription_types WHERE type_id = ?', $typeId
  74. );
  75. $returner = isset($result->fields[0]) ? $result->fields[0] : false;
  76. $result->Close();
  77. unset($result);
  78. return $returner;
  79. }
  80. /**
  81. * Retrieve membership flag by ID.
  82. * @param $typeId int
  83. * @return int
  84. */
  85. function getSubscriptionTypeMembership($typeId) {
  86. $result =& $this->retrieve(
  87. 'SELECT membership FROM subscription_types WHERE type_id = ?', $typeId
  88. );
  89. $returner = isset($result->fields[0]) ? $result->fields[0] : false;
  90. $result->Close();
  91. unset($result);
  92. return $returner;
  93. }
  94. /**
  95. * Retrieve nonExpiring flag by ID.
  96. * @param $typeId int
  97. * @return int
  98. */
  99. function getSubscriptionTypeNonExpiring($typeId) {
  100. $result =& $this->retrieve(
  101. 'SELECT non_expiring FROM subscription_types WHERE type_id = ?', $typeId
  102. );
  103. $returner = isset($result->fields[0]) ? $result->fields[0] : false;
  104. $result->Close();
  105. unset($result);
  106. return $returner;
  107. }
  108. /**
  109. * Retrieve public display flag by ID.
  110. * @param $typeId int
  111. * @return int
  112. */
  113. function getSubscriptionTypeDisablePublicDisplay($typeId) {
  114. $result =& $this->retrieve(
  115. 'SELECT disable_public_display FROM subscription_types WHERE type_id = ?', $typeId
  116. );
  117. $returner = isset($result->fields[0]) ? $result->fields[0] : false;
  118. $result->Close();
  119. unset($result);
  120. return $returner;
  121. }
  122. /**
  123. * Check if a subscription type exists with the given type id for a journal.
  124. * @param $typeId int
  125. * @param $journalId int
  126. * @return boolean
  127. */
  128. function subscriptionTypeExistsByTypeId($typeId, $journalId) {
  129. $result =& $this->retrieve(
  130. 'SELECT COUNT(*)
  131. FROM subscription_types
  132. WHERE type_id = ?
  133. AND journal_id = ?',
  134. array(
  135. $typeId,
  136. $journalId
  137. )
  138. );
  139. $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
  140. $result->Close();
  141. unset($result);
  142. return $returner;
  143. }
  144. /**
  145. * Internal function to return a SubscriptionType object from a row.
  146. * @param $row array
  147. * @return SubscriptionType
  148. */
  149. function &_returnSubscriptionTypeFromRow(&$row) {
  150. $subscriptionType = new SubscriptionType();
  151. $subscriptionType->setTypeId($row['type_id']);
  152. $subscriptionType->setJournalId($row['journal_id']);
  153. $subscriptionType->setCost($row['cost']);
  154. $subscriptionType->setCurrencyCodeAlpha($row['currency_code_alpha']);
  155. $subscriptionType->setNonExpiring($row['non_expiring']);
  156. $subscriptionType->setDuration($row['duration']);
  157. $subscriptionType->setFormat($row['format']);
  158. $subscriptionType->setInstitutional($row['institutional']);
  159. $subscriptionType->setMembership($row['membership']);
  160. $subscriptionType->setDisablePublicDisplay($row['disable_public_display']);
  161. $subscriptionType->setSequence($row['seq']);
  162. $this->getDataObjectSettings('subscription_type_settings', 'type_id', $row['type_id'], $subscriptionType);
  163. HookRegistry::call('SubscriptionTypeDAO::_returnSubscriptionTypeFromRow', array(&$subscriptionType, &$row));
  164. return $subscriptionType;
  165. }
  166. /**
  167. * Get the list of field names for which localized data is used.
  168. * @return array
  169. */
  170. function getLocaleFieldNames() {
  171. return array('name', 'description');
  172. }
  173. /**
  174. * Update the localized settings for this object
  175. * @param $subscriptionType object
  176. */
  177. function updateLocaleFields(&$subscriptionType) {
  178. $this->updateDataObjectSettings('subscription_type_settings', $subscriptionType, array(
  179. 'type_id' => $subscriptionType->getTypeId()
  180. ));
  181. }
  182. /**
  183. * Insert a new SubscriptionType.
  184. * @param $subscriptionType SubscriptionType
  185. * @return boolean
  186. */
  187. function insertSubscriptionType(&$subscriptionType) {
  188. $this->update(
  189. 'INSERT INTO subscription_types
  190. (journal_id, cost, currency_code_alpha, non_expiring, duration, format, institutional, membership, disable_public_display, seq)
  191. VALUES
  192. (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
  193. array(
  194. $subscriptionType->getJournalId(),
  195. $subscriptionType->getCost(),
  196. $subscriptionType->getCurrencyCodeAlpha(),
  197. $subscriptionType->getNonExpiring(),
  198. $subscriptionType->getDuration(),
  199. $subscriptionType->getFormat(),
  200. $subscriptionType->getInstitutional(),
  201. $subscriptionType->getMembership(),
  202. $subscriptionType->getDisablePublicDisplay(),
  203. $subscriptionType->getSequence()
  204. )
  205. );
  206. $subscriptionType->setTypeId($this->getInsertSubscriptionTypeId());
  207. $this->updateLocaleFields($subscriptionType);
  208. return $subscriptionType->getTypeId();
  209. }
  210. /**
  211. * Update an existing subscription type.
  212. * @param $subscriptionType SubscriptionType
  213. * @return boolean
  214. */
  215. function updateSubscriptionType(&$subscriptionType) {
  216. $returner = $this->update(
  217. 'UPDATE subscription_types
  218. SET
  219. journal_id = ?,
  220. cost = ?,
  221. currency_code_alpha = ?,
  222. non_expiring = ?,
  223. duration = ?,
  224. format = ?,
  225. institutional = ?,
  226. membership = ?,
  227. disable_public_display = ?,
  228. seq = ?
  229. WHERE type_id = ?',
  230. array(
  231. $subscriptionType->getJournalId(),
  232. $subscriptionType->getCost(),
  233. $subscriptionType->getCurrencyCodeAlpha(),
  234. $subscriptionType->getNonExpiring(),
  235. $subscriptionType->getDuration(),
  236. $subscriptionType->getFormat(),
  237. $subscriptionType->getInstitutional(),
  238. $subscriptionType->getMembership(),
  239. $subscriptionType->getDisablePublicDisplay(),
  240. $subscriptionType->getSequence(),
  241. $subscriptionType->getTypeId()
  242. )
  243. );
  244. $this->updateLocaleFields($subscriptionType);
  245. return $returner;
  246. }
  247. /**
  248. * Delete a subscription type.
  249. * @param $subscriptionType SubscriptionType
  250. * @return boolean
  251. */
  252. function deleteSubscriptionType(&$subscriptionType) {
  253. return $this->deleteSubscriptionTypeById($subscriptionType->getTypeId());
  254. }
  255. /**
  256. * Delete a subscription type by ID. Note that all subscriptions with this
  257. * type ID are also deleted.
  258. * @param $typeId int
  259. * @return boolean
  260. */
  261. function deleteSubscriptionTypeById($typeId) {
  262. // Delete all subscriptions corresponding to this subscription type
  263. $institutional = $this->getSubscriptionTypeInstitutional($typeId);
  264. if ($institutional) {
  265. $subscriptionDao =& DAORegistry::getDAO('InstitutionalSubscriptionDAO');
  266. } else {
  267. $subscriptionDao =& DAORegistry::getDAO('IndividualSubscriptionDAO');
  268. }
  269. $returner = $subscriptionDao->deleteSubscriptionsByTypeId($typeId);
  270. // Delete subscription type
  271. if ($returner) {
  272. $returner = $this->update('DELETE FROM subscription_types WHERE type_id = ?', $typeId);
  273. }
  274. // Delete all localization settings for this subscription type
  275. if ($returner) {
  276. $this->update('DELETE FROM subscription_type_settings WHERE type_id = ?', $typeId);
  277. }
  278. return $returner;
  279. }
  280. /**
  281. * Delete subscription types by journal ID. Note that all subscriptions with
  282. * corresponding types are also deleted.
  283. * @param $journalId int
  284. * @return boolean
  285. */
  286. function deleteSubscriptionTypesByJournal($journalId) {
  287. $result =& $this->retrieve(
  288. 'SELECT type_id
  289. FROM subscription_types
  290. WHERE journal_id = ?',
  291. $journalId
  292. );
  293. $returner = false;
  294. if ($result->RecordCount() != 0) {
  295. $returner = true;
  296. while (!$result->EOF && $returner) {
  297. $typeId = $result->fields[0];
  298. $returner = $this->deleteSubscriptionTypeById($typeId);
  299. $result->moveNext();
  300. }
  301. }
  302. $result->Close();
  303. unset($result);
  304. return $returner;
  305. }
  306. /**
  307. * Retrieve subscription types matching a particular journal ID.
  308. * @param $journalId int
  309. * @return object DAOResultFactory containing matching SubscriptionTypes
  310. */
  311. function &getSubscriptionTypesByJournalId($journalId, $rangeInfo = null) {
  312. $result =& $this->retrieveRange(
  313. 'SELECT * FROM subscription_types WHERE journal_id = ? ORDER BY seq',
  314. $journalId, $rangeInfo
  315. );
  316. $returner = new DAOResultFactory($result, $this, '_returnSubscriptionTypeFromRow');
  317. return $returner;
  318. }
  319. /**
  320. * Retrieve subscription types matching a particular journal ID and institutional flag.
  321. * @param $journalId int
  322. * @param $institutional bool
  323. * @param $disablePublicDisplay bool
  324. * @return object DAOResultFactory containing matching SubscriptionTypes
  325. */
  326. function &getSubscriptionTypesByInstitutional($journalId, $institutional = false, $disablePublicDisplay = null, $rangeInfo = null) {
  327. if ($institutional) $institutional = 1; else $institutional = 0;
  328. if ($disablePublicDisplay === null) {
  329. $disablePublicDisplaySql = '';
  330. } elseif ($disablePublicDisplay) {
  331. $disablePublicDisplaySql = 'AND disable_public_display = 1';
  332. } else {
  333. $disablePublicDisplaySql = 'AND disable_public_display = 0';
  334. }
  335. $result =& $this->retrieveRange(
  336. 'SELECT *
  337. FROM
  338. subscription_types
  339. WHERE journal_id = ?
  340. AND institutional = ? '
  341. . $disablePublicDisplaySql .
  342. ' ORDER BY seq',
  343. array(
  344. $journalId,
  345. $institutional
  346. ),
  347. $rangeInfo
  348. );
  349. $returner = new DAOResultFactory($result, $this, '_returnSubscriptionTypeFromRow');
  350. return $returner;
  351. }
  352. /**
  353. * Check if at least one subscription type exists for a given journal by institutional flag.
  354. * @param $journalId int
  355. * @param $institutional bool
  356. * @return boolean
  357. */
  358. function subscriptionTypesExistByInstitutional($journalId, $institutional = false) {
  359. if ($institutional) $institutional = 1; else $institutional = 0;
  360. $result =& $this->retrieve(
  361. 'SELECT COUNT(*)
  362. FROM
  363. subscription_types st
  364. WHERE st.journal_id = ?
  365. AND st.institutional = ?',
  366. array(
  367. $journalId,
  368. $institutional
  369. )
  370. );
  371. $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
  372. $result->Close();
  373. unset($result);
  374. return $returner;
  375. }
  376. /**
  377. * Get the ID of the last inserted subscription type.
  378. * @return int
  379. */
  380. function getInsertSubscriptionTypeId() {
  381. return $this->getInsertId('subscription_types', 'type_id');
  382. }
  383. /**
  384. * Sequentially renumber subscription types in their sequence order.
  385. */
  386. function resequenceSubscriptionTypes($journalId) {
  387. $result =& $this->retrieve(
  388. 'SELECT type_id FROM subscription_types WHERE journal_id = ? ORDER BY seq',
  389. $journalId
  390. );
  391. for ($i=1; !$result->EOF; $i++) {
  392. list($subscriptionTypeId) = $result->fields;
  393. $this->update(
  394. 'UPDATE subscription_types SET seq = ? WHERE type_id = ?',
  395. array(
  396. $i,
  397. $subscriptionTypeId
  398. )
  399. );
  400. $result->moveNext();
  401. }
  402. $result->close();
  403. unset($result);
  404. }
  405. }
  406. ?>