PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/campsite/src/classes/Section.php

https://github.com/joechrysler/Campsite
PHP | 657 lines | 401 code | 76 blank | 180 comment | 55 complexity | f98a6b13c0489c9e3d90b9305796266c MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @package Campsite
  4. */
  5. /**
  6. * Includes
  7. */
  8. require_once($GLOBALS['g_campsiteDir'].'/db_connect.php');
  9. require_once($GLOBALS['g_campsiteDir'].'/classes/DatabaseObject.php');
  10. require_once($GLOBALS['g_campsiteDir'].'/classes/DbObjectArray.php');
  11. require_once($GLOBALS['g_campsiteDir'].'/classes/SQLSelectClause.php');
  12. require_once($GLOBALS['g_campsiteDir'].'/classes/Log.php');
  13. require_once($GLOBALS['g_campsiteDir'].'/classes/CampCacheList.php');
  14. require_once($GLOBALS['g_campsiteDir'].'/template_engine/classes/CampTemplate.php');
  15. /**
  16. * @package Campsite
  17. */
  18. class Section extends DatabaseObject {
  19. var $m_dbTableName = 'Sections';
  20. var $m_keyColumnNames = array(
  21. 'IdPublication',
  22. 'NrIssue',
  23. 'IdLanguage',
  24. 'Number');
  25. var $m_columnNames = array(
  26. 'IdPublication',
  27. 'NrIssue',
  28. 'IdLanguage',
  29. 'Number',
  30. 'Name',
  31. 'ShortName',
  32. 'Description',
  33. 'SectionTplId',
  34. 'ArticleTplId');
  35. var $m_languageName = null;
  36. /**
  37. * A section is a part of an issue.
  38. * @param int $p_publicationId
  39. * @param int $p_issueNumber
  40. * @param int $p_languageId
  41. * @param int $p_sectionNumber
  42. */
  43. public function Section($p_publicationId = null, $p_issueNumber = null,
  44. $p_languageId = null, $p_sectionNumber = null)
  45. {
  46. parent::DatabaseObject($this->m_columnNames);
  47. $this->m_data['IdPublication'] = $p_publicationId;
  48. $this->m_data['NrIssue'] = $p_issueNumber;
  49. $this->m_data['IdLanguage'] = $p_languageId;
  50. $this->m_data['Number'] = $p_sectionNumber;
  51. if ($this->keyValuesExist()) {
  52. $this->fetch();
  53. }
  54. } // fn Section
  55. /**
  56. * Create a new Section.
  57. * @param string $p_name
  58. * @param string $p_shortName
  59. */
  60. public function create($p_name, $p_shortName, $p_columns = null)
  61. {
  62. if (!is_array($p_columns)) {
  63. $p_columns = array();
  64. }
  65. $p_columns['Name'] = $p_name;
  66. $p_columns['ShortName'] = $p_shortName;
  67. $success = parent::create($p_columns);
  68. if ($success) {
  69. if (function_exists("camp_load_translation_strings")) {
  70. camp_load_translation_strings("api");
  71. }
  72. $logtext = getGS('Section "$1" ($2) added. (Publication: $3, Issue: $4)',
  73. $this->m_data['Name'],
  74. $this->m_data['Number'],
  75. $this->m_data['IdPublication'],
  76. $this->m_data['NrIssue']);
  77. Log::Message($logtext, null, 21);
  78. }
  79. return $success;
  80. } // fn create
  81. /**
  82. * Copy the section to the given issue. The issue can be the same as
  83. * the current issue. All articles will be copied to the new section.
  84. *
  85. * @param int $p_destPublicationId
  86. * The destination publication ID.
  87. * @param int $p_destIssueNumber
  88. * The destination issue ID.
  89. * @param int $p_destIssueLanguageId
  90. * (optional) The destination issue language ID. If not given,
  91. * it will use the language ID of this section.
  92. * @param int $p_destSectionNumber
  93. * (optional) The destination section ID. If not given, a new
  94. * section will be created.
  95. * @param boolean $p_copyArticles
  96. * (optional) If set to true, all articles will be copied to the
  97. * destination section.
  98. * @return Section
  99. * The new Section object.
  100. */
  101. public function copy($p_destPublicationId, $p_destIssueNumber,
  102. $p_destIssueLanguageId = null,$p_destSectionNumber = null,
  103. $p_copyArticles = true)
  104. {
  105. if (is_null($p_destIssueLanguageId)) {
  106. $p_destIssueLanguageId = $this->m_data['IdLanguage'];
  107. }
  108. if (is_null($p_destSectionNumber)) {
  109. $p_destSectionNumber = $this->m_data['Number'];
  110. }
  111. $dstSectionObj = new Section($p_destPublicationId,
  112. $p_destIssueNumber,
  113. $p_destIssueLanguageId,
  114. $p_destSectionNumber);
  115. // If source issue and destination issue are the same
  116. if ( ($this->m_data['IdPublication'] == $p_destPublicationId)
  117. && ($this->m_data['NrIssue'] == $p_destIssueNumber)
  118. && ($this->m_data['IdLanguage'] == $p_destIssueLanguageId) ) {
  119. $shortName = $p_destSectionNumber;
  120. $sectionName = $this->getName() . " (duplicate)";
  121. } else {
  122. $shortName = $this->getUrlName();
  123. $sectionName = $this->getName();
  124. }
  125. $dstSectionCols = array();
  126. $dstSectionCols['SectionTplId'] = $this->m_data['SectionTplId'];
  127. $dstSectionCols['ArticleTplId'] = $this->m_data['ArticleTplId'];
  128. // Create the section if it doesnt exist yet.
  129. if (!$dstSectionObj->exists()) {
  130. $dstSectionObj->create($sectionName, $shortName, $dstSectionCols);
  131. }
  132. // Copy all the articles.
  133. if ($p_copyArticles) {
  134. $srcSectionArticles = Article::GetArticles($this->m_data['IdPublication'],
  135. $this->m_data['NrIssue'],
  136. $this->m_data['Number']);
  137. $copiedArticles = array();
  138. foreach ($srcSectionArticles as $articleObj) {
  139. if (!in_array($articleObj->getArticleNumber(), $copiedArticles)) {
  140. $tmpCopiedArticles = $articleObj->copy($p_destPublicationId,
  141. $p_destIssueNumber, $p_destSectionNumber, null, true);
  142. $copiedArticles[] = $articleObj->getArticleNumber();
  143. }
  144. }
  145. }
  146. return $dstSectionObj;
  147. } // fn copy
  148. /**
  149. * Delete the section, and optionally the articles. If you are
  150. * deleting the articles, the default is to only delete the articles
  151. * with the same language as this section. If you want to delete
  152. * all article translations, set the second parameter to TRUE.
  153. *
  154. * @param boolean $p_deleteArticles
  155. * @param boolean $p_deleteArticleTranslations
  156. * @return int
  157. * Return the number of articles deleted.
  158. */
  159. public function delete($p_deleteArticles = false, $p_deleteArticleTranslations = false)
  160. {
  161. $numArticlesDeleted = 0;
  162. if ($p_deleteArticles) {
  163. $languageId = null;
  164. if (!$p_deleteArticleTranslations) {
  165. $languageId = $this->m_data['IdLanguage'];
  166. }
  167. $articles = Article::GetArticles($this->m_data['IdPublication'],
  168. $this->m_data['NrIssue'],
  169. $this->m_data['Number'],
  170. $languageId);
  171. $numArticlesDeleted = count($articles);
  172. foreach ($articles as $deleteMe) {
  173. $deleteMe->delete();
  174. }
  175. }
  176. $tmpData = $this->m_data;
  177. $success = parent::delete();
  178. if ($success) {
  179. if (function_exists("camp_load_translation_strings")) {
  180. camp_load_translation_strings("api");
  181. }
  182. $logtext = getGS('Section "$1" ($2) deleted. (Publication: $3, Issue: $4)',
  183. $tmpData['Name'], $tmpData['Number'],
  184. $tmpData['IdPublication'], $tmpData['NrIssue']);
  185. Log::Message($logtext, null, 22);
  186. }
  187. return $numArticlesDeleted;
  188. } // fn delete
  189. /**
  190. * @return int
  191. */
  192. public function getPublicationId()
  193. {
  194. return $this->m_data['IdPublication'];
  195. } // fn getPublicationId
  196. /**
  197. * @return int
  198. */
  199. public function getIssueNumber()
  200. {
  201. return $this->m_data['NrIssue'];
  202. } // fn getIssueNumber
  203. /**
  204. * @return int
  205. */
  206. public function getLanguageId()
  207. {
  208. return $this->m_data['IdLanguage'];
  209. } // fn getLanguageId
  210. /**
  211. * A simple way to get the name of the language the article is
  212. * written in. The value is cached in case there are multiple
  213. * calls to this function.
  214. *
  215. * @return string
  216. */
  217. public function getLanguageName()
  218. {
  219. if (is_null($this->m_languageName)) {
  220. $language = new Language($this->m_data['IdLanguage']);
  221. $this->m_languageName = $language->getNativeName();
  222. }
  223. return $this->m_languageName;
  224. } // fn getLanguageName
  225. /**
  226. * @return int
  227. */
  228. public function getSectionNumber()
  229. {
  230. return $this->m_data['Number'];
  231. } // fn getSectionNumber
  232. /**
  233. * @return string
  234. */
  235. public function getName()
  236. {
  237. return $this->m_data['Name'];
  238. } // fn getName
  239. /**
  240. * @param string $p_value
  241. * @return boolean
  242. */
  243. public function setName($p_value)
  244. {
  245. return $this->setProperty('Name', $p_value);
  246. } // fn setName
  247. /**
  248. * @return string
  249. */
  250. public function getDescription()
  251. {
  252. return $this->m_data['Description'];
  253. } // fn getDescription
  254. /**
  255. * @param string $p_value
  256. * @return boolean
  257. */
  258. public function setDescription($p_value)
  259. {
  260. return $this->setProperty('Description', $p_value);
  261. } // fn setDescription
  262. /**
  263. * @return string
  264. */
  265. public function getUrlName()
  266. {
  267. return $this->m_data['ShortName'];
  268. } // fn getUrlName
  269. /**
  270. * @param string $p_name
  271. */
  272. public function setUrlName($p_name)
  273. {
  274. return $this->setProperty('ShortName', $p_name);
  275. } // fn setUrlName
  276. /**
  277. * @return int
  278. */
  279. public function getArticleTemplateId()
  280. {
  281. return $this->m_data['ArticleTplId'];
  282. } // fn getArticleTemplateId
  283. /**
  284. * @param int $p_value
  285. * @return boolean
  286. */
  287. public function setArticleTemplateId($p_value)
  288. {
  289. return $this->setProperty('ArticleTplId', $p_value);
  290. } // fn setArticleTemplateId
  291. /**
  292. * @return int
  293. */
  294. public function getSectionTemplateId()
  295. {
  296. return $this->m_data['SectionTplId'];
  297. } // fn getSectionTemplateId
  298. /**
  299. * @param int $p_value
  300. * @return boolean
  301. */
  302. public function setSectionTemplateId($p_value)
  303. {
  304. return $this->setProperty('SectionTplId', $p_value);
  305. } // fn setSectionTemplateId
  306. /**
  307. * Return an array of sections in the given issue.
  308. * @param int $p_publicationId
  309. * (Optional) Only return sections in this publication.
  310. *
  311. * @param int $p_issueNumber
  312. * (Optional) Only return sections in this issue.
  313. *
  314. * @param int $p_languageId
  315. * (Optional) Only return sections that have this language ID.
  316. *
  317. * @param string $p_urlName
  318. * (Optional) Only return sections that have this URL name.
  319. *
  320. * @param string $p_sectionName
  321. * (Optional) Only return sections that have this name.
  322. *
  323. * @param array $p_sqlOptions
  324. * (Optional) Additional options. See DatabaseObject::ProcessOptions().
  325. *
  326. * @return array
  327. */
  328. public static function GetSections($p_publicationId = null, $p_issueNumber = null,
  329. $p_languageId = null, $p_urlName = null,
  330. $p_sectionName = null, $p_sqlOptions = null, $p_skipCache = false)
  331. {
  332. if (!$p_skipCache && CampCache::IsEnabled()) {
  333. $paramsArray['publication_id'] = (is_null($p_publicationId)) ? 'null' : $p_publicationId;
  334. $paramsArray['issue_number'] = (is_null($p_issueNumber)) ? 'null' : $p_issueNumber;
  335. $paramsArray['language_id'] = (is_null($p_languageId)) ? 'null' : $p_languageId;
  336. $paramsArray['url_name'] = (is_null($p_urlName)) ? 'null' : $p_urlName;
  337. $paramsArray['section_name'] = (is_null($p_sectionName)) ? 'null' : $p_sectionName;
  338. $paramsArray['sql_options'] = (is_null($p_sqlOptions)) ? 'null' : $p_sqlOptions;
  339. $cacheListObj = new CampCacheList($paramsArray, __METHOD__);
  340. $sectionsList = $cacheListObj->fetchFromCache();
  341. if ($sectionsList !== false && is_array($sectionsList)) {
  342. return $sectionsList;
  343. }
  344. }
  345. $constraints = array();
  346. if (!is_null($p_publicationId)) {
  347. $constraints[] = array("IdPublication", $p_publicationId);
  348. }
  349. if (!is_null($p_issueNumber)) {
  350. $constraints[] = array("NrIssue", $p_issueNumber);
  351. }
  352. if (!is_null($p_languageId)) {
  353. $constraints[] = array("IdLanguage", $p_languageId);
  354. }
  355. if (!is_null($p_urlName)) {
  356. $constraints[] = array("ShortName", $p_urlName);
  357. }
  358. if (!is_null($p_sectionName)) {
  359. $constraints[] = array("Name", $p_sectionName);
  360. }
  361. $sectionsList = DatabaseObject::Search('Section', $constraints, $p_sqlOptions);
  362. if (!$p_skipCache && CampCache::IsEnabled()) {
  363. $cacheListObj->storeInCache($sectionsList);
  364. }
  365. return $sectionsList;
  366. } // fn GetSections
  367. /**
  368. * Return an array of arrays indexed by "id" and "name".
  369. * @return array
  370. */
  371. public static function GetUniqueSections($p_publicationId, $p_byLanguage = false)
  372. {
  373. global $g_ado_db;
  374. $queryStr = "SELECT Number as id, s.Name as name, s.IdLanguage, l.Name as LangName "
  375. ." FROM Sections AS s LEFT JOIN Languages AS l ON s.IdLanguage = l.Id"
  376. ." WHERE s.IdPublication = $p_publicationId"
  377. ." GROUP BY s.Number";
  378. if ($p_byLanguage) {
  379. $queryStr .= ", s.IdLanguage";
  380. }
  381. return $g_ado_db->GetAll($queryStr);
  382. } // fn GetSectionNames
  383. /**
  384. * Return the total number of sections according to the given values.
  385. * @param int $p_publicationId
  386. * @param int $p_issueNumber
  387. * @param int $p_languageId
  388. * @return int
  389. */
  390. public static function GetTotalSections($p_publicationId = null,
  391. $p_issueNumber = null,
  392. $p_languageId = null)
  393. {
  394. global $g_ado_db;
  395. $queryStr = 'SELECT COUNT(*) FROM Sections';
  396. $whereClause = array();
  397. if (!is_null($p_publicationId)) {
  398. $whereClause[] = "IdPublication=$p_publicationId";
  399. }
  400. if (!is_null($p_issueNumber)) {
  401. $whereClause[] = "NrIssue=$p_issueNumber";
  402. }
  403. if (!is_null($p_languageId)) {
  404. $whereClause[] = "IdLanguage=$p_languageId";
  405. }
  406. if (count($whereClause) > 0) {
  407. $queryStr .= ' WHERE '.implode(' AND ', $whereClause);
  408. }
  409. $total = $g_ado_db->GetOne($queryStr);
  410. return $total;
  411. } // fn GetTotalSections
  412. public static function GetNumUniqueSections($p_publicationId, $p_byLanguage = true)
  413. {
  414. global $g_ado_db;
  415. $queryStr = "SELECT * FROM Sections WHERE IdPublication = $p_publicationId"
  416. ." GROUP BY Number";
  417. if ($p_byLanguage) {
  418. $queryStr .= ', IdLanguage';
  419. }
  420. $result = $g_ado_db->Execute($queryStr);
  421. return $result->RowCount();
  422. }
  423. /**
  424. * Return a section number that is not in use.
  425. * @param int $p_publicationId
  426. * @param int $p_issueNumber
  427. * @param int $p_languageId
  428. * @return int
  429. */
  430. public static function GetUnusedSectionNumber($p_publicationId, $p_issueNumber)
  431. {
  432. global $g_ado_db;
  433. $queryStr = "SELECT MAX(Number) + 1 FROM Sections "
  434. ." WHERE IdPublication=$p_publicationId "
  435. ." AND NrIssue=$p_issueNumber";
  436. $number = 0 + $g_ado_db->GetOne($queryStr);
  437. if ($number <= 0) {
  438. $number++;
  439. }
  440. return $number;
  441. } // fn GetUnusedSectionNumber
  442. /**
  443. * Returns a sections list based on the given parameters.
  444. *
  445. * @param array $p_parameters
  446. * An array of ComparisonOperation objects
  447. * @param string $p_order
  448. * An array of columns and directions to order by
  449. * @param integer $p_start
  450. * The record number to start the list
  451. * @param integer $p_limit
  452. * The offset. How many records from $p_start will be retrieved.
  453. *
  454. * @return array $sectionsList
  455. * An array of Section objects
  456. */
  457. public static function GetList(array $p_parameters, $p_order = null,
  458. $p_start = 0, $p_limit = 0, &$p_count, $p_skipCache = false)
  459. {
  460. global $g_ado_db;
  461. if (!$p_skipCache && CampCache::IsEnabled()) {
  462. $paramsArray['parameters'] = serialize($p_parameters);
  463. $paramsArray['order'] = (is_null($p_order)) ? 'null' : $p_order;
  464. $paramsArray['start'] = $p_start;
  465. $paramsArray['limit'] = $p_limit;
  466. $cacheListObj = new CampCacheList($paramsArray, __METHOD__);
  467. $sectionsList = $cacheListObj->fetchFromCache();
  468. if ($sectionsList !== false && is_array($sectionsList)) {
  469. return $sectionsList;
  470. }
  471. }
  472. $hasPublicationId = false;
  473. $selectClauseObj = new SQLSelectClause();
  474. $countClauseObj = new SQLSelectClause();
  475. // sets the where conditions
  476. foreach ($p_parameters as $param) {
  477. $comparisonOperation = self::ProcessListParameters($param);
  478. if (empty($comparisonOperation)) {
  479. break;
  480. }
  481. if (strpos($comparisonOperation['left'], 'IdPublication') !== false) {
  482. $hasPublicationId = true;
  483. }
  484. $whereCondition = $comparisonOperation['left'] . ' '
  485. . $comparisonOperation['symbol'] . " '"
  486. . $comparisonOperation['right'] . "' ";
  487. $selectClauseObj->addWhere($whereCondition);
  488. $countClauseObj->addWhere($whereCondition);
  489. }
  490. // validates whether publication identifier was given
  491. if ($hasPublicationId == false) {
  492. CampTemplate::singleton()->trigger_error('missed parameter Publication '
  493. .'Identifier in statement list_sections');
  494. return;
  495. }
  496. // sets the columns to be fetched
  497. $tmpSection = new Section();
  498. $columnNames = $tmpSection->getColumnNames(true);
  499. foreach ($columnNames as $columnName) {
  500. $selectClauseObj->addColumn($columnName);
  501. }
  502. $countClauseObj->addColumn('COUNT(*)');
  503. // sets the main table for the query
  504. $selectClauseObj->setTable($tmpSection->getDbTableName());
  505. $countClauseObj->setTable($tmpSection->getDbTableName());
  506. unset($tmpSection);
  507. if (!is_array($p_order)) {
  508. $p_order = array();
  509. }
  510. // sets the order condition if any
  511. foreach ($p_order as $orderColumn => $orderDirection) {
  512. $selectClauseObj->addOrderBy($orderColumn . ' ' . $orderDirection);
  513. }
  514. $selectClauseObj->addGroupField('Number');
  515. $selectClauseObj->addGroupField('IdLanguage');
  516. // sets the limit
  517. $selectClauseObj->setLimit($p_start, $p_limit);
  518. // builds the query and executes it
  519. $selectQuery = $selectClauseObj->buildQuery();
  520. $countQuery = $countClauseObj->buildQuery();
  521. $sections = $g_ado_db->GetAll($selectQuery);
  522. if (is_array($sections)) {
  523. $p_count = $g_ado_db->GetOne($countQuery);
  524. // builds the array of section objects
  525. $sectionsList = array();
  526. foreach ($sections as $section) {
  527. $secObj = new Section($section['IdPublication'],
  528. $section['NrIssue'],
  529. $section['IdLanguage'],
  530. $section['Number']);
  531. if ($secObj->exists()) {
  532. $sectionsList[] = $secObj;
  533. }
  534. }
  535. } else {
  536. $sectionsList = array();
  537. $p_count = 0;
  538. }
  539. if (!$p_skipCache && CampCache::IsEnabled()) {
  540. $cacheListObj->storeInCache($sectionsList);
  541. }
  542. return $sectionsList;
  543. } // fn GetList
  544. /**
  545. * Processes a paremeter (condition) coming from template tags.
  546. *
  547. * @param array $p_param
  548. * The array of parameters
  549. *
  550. * @return array $comparisonOperation
  551. * The array containing processed values of the condition
  552. */
  553. private static function ProcessListParameters($p_param)
  554. {
  555. $comparisonOperation = array();
  556. switch (strtolower($p_param->getLeftOperand())) {
  557. case 'name':
  558. $comparisonOperation['left'] = 'Name';
  559. break;
  560. case 'number':
  561. $comparisonOperation['left'] = 'Number';
  562. break;
  563. case 'idpublication':
  564. $comparisonOperation['left'] = 'IdPublication';
  565. break;
  566. case 'nrissue':
  567. $comparisonOperation['left'] = 'NrIssue';
  568. break;
  569. case 'idlanguage':
  570. $comparisonOperation['left'] = 'IdLanguage';
  571. break;
  572. }
  573. if (isset($comparisonOperation['left'])) {
  574. $operatorObj = $p_param->getOperator();
  575. $comparisonOperation['symbol'] = $operatorObj->getSymbol('sql');
  576. $comparisonOperation['right'] = $p_param->getRightOperand();
  577. }
  578. return $comparisonOperation;
  579. } // fn ProcessListParameters
  580. } // class Section
  581. ?>