PageRenderTime 97ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/campsite/src/template_engine/classes/CampURIShortNames.php

https://github.com/joechrysler/Campsite
PHP | 440 lines | 292 code | 45 blank | 103 comment | 76 complexity | a13ac97058f748db420e2b1884bc4ed3 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. * @author Holman Romero <holman.romero@gmail.com>
  6. * @author Mugur Rus <mugur.rus@gmail.com>
  7. * @copyright 2007 MDLF, Inc.
  8. * @license http://www.gnu.org/licenses/gpl.txt
  9. * @version $Revision$
  10. * @link http://www.sourcefabric.org
  11. */
  12. /**
  13. * Includes
  14. */
  15. require_once($GLOBALS['g_campsiteDir'].'/classes/Language.php');
  16. require_once($GLOBALS['g_campsiteDir'].'/classes/Publication.php');
  17. require_once($GLOBALS['g_campsiteDir'].'/classes/Issue.php');
  18. require_once($GLOBALS['g_campsiteDir'].'/classes/Section.php');
  19. require_once($GLOBALS['g_campsiteDir'].'/classes/Article.php');
  20. require_once($GLOBALS['g_campsiteDir'].'/classes/Alias.php');
  21. require_once($GLOBALS['g_campsiteDir'].'/template_engine/classes/CampRequest.php');
  22. require_once($GLOBALS['g_campsiteDir'].'/template_engine/classes/CampURI.php');
  23. require_once($GLOBALS['g_campsiteDir'].'/template_engine/classes/CampTemplate.php');
  24. /**
  25. * Class CampURIShortNames
  26. */
  27. class CampURIShortNames extends CampURI
  28. {
  29. /**
  30. * Parameters that are restricted to CampURIShortNames object use.
  31. *
  32. * @var array
  33. */
  34. static private $m_restrictedParameters = array(
  35. );
  36. /**
  37. * Class constructor
  38. *
  39. * @param string $p_uri
  40. * The full URI string
  41. */
  42. public function __construct($p_uri = null)
  43. {
  44. parent::__construct($p_uri);
  45. $this->setURLType(URLTYPE_SHORT_NAMES);
  46. $res = $this->setURL();
  47. if (PEAR::isError($res)) {
  48. $this->m_validURI = false;
  49. $this->m_errorCode = $res->getCode();
  50. if (!is_null($this->m_publication)) {
  51. $tplId = CampSystem::GetInvalidURLTemplate($this->m_publication->identifier);
  52. $template = new MetaTemplate($tplId);
  53. if ($template->defined()) {
  54. $this->m_template = $template;
  55. }
  56. }
  57. CampTemplate::singleton()->trigger_error($res->getMessage());
  58. } else {
  59. $this->m_validURI = true;
  60. }
  61. $this->validateCache(false);
  62. } // fn __construct
  63. /**
  64. * Gets the language URI path.
  65. *
  66. * @return string
  67. * The language URI path
  68. */
  69. private function getURILanguage()
  70. {
  71. $uriString = null;
  72. if (!is_null($this->m_language) && $this->m_language->defined()) {
  73. $uriString = $this->m_config->getSetting('SUBDIR') . '/' . $this->m_language->code . '/';
  74. }
  75. return $uriString;
  76. } // fn getURILanguage
  77. /**
  78. * Gets the issue URI path.
  79. * It fetches the issue URL name from URI or current issue list if any.
  80. *
  81. * @return string
  82. * The issue URI path
  83. */
  84. private function getURIIssue()
  85. {
  86. $uriString = $this->getURILanguage();
  87. if (is_null($uriString)) {
  88. return null;
  89. }
  90. if (!is_null($this->m_issue) && $this->m_issue->defined()) {
  91. $uriString .= $this->m_issue->url_name . '/';
  92. } else {
  93. $uriString = null;
  94. }
  95. return $uriString;
  96. } // fn getURIIssue
  97. /**
  98. * Gets the section URI path.
  99. * It fetches the section URL name from URI or current section list if any.
  100. *
  101. * @return string
  102. * The section URI path
  103. */
  104. private function getURISection()
  105. {
  106. $uriString = $this->getURIIssue();
  107. if (is_null($uriString)) {
  108. return null;
  109. }
  110. if (!is_null($this->m_section) && $this->m_section->defined()) {
  111. $uriString .= $this->m_section->url_name . '/';
  112. } else {
  113. $uriString = null;
  114. }
  115. return $uriString;
  116. } // fn getURISection
  117. /**
  118. * Gets the article URI path.
  119. * It fetches the article URL name from URI or current article list if any.
  120. *
  121. * @return string
  122. * The article URI path
  123. */
  124. private function getURIArticle()
  125. {
  126. if (!is_null($this->m_article) && $this->m_article->defined()) {
  127. $uriString = $this->getURISection();
  128. $uriString .= $this->m_article->url_name . '/';
  129. if ($seo = $this->m_publication->seo) {
  130. $article = null;
  131. foreach ($seo as $field => $value) {
  132. switch ($field) {
  133. case 'name':
  134. $article .= trim($this->m_article->name) . ' ';
  135. break;
  136. case 'keywords':
  137. $article .= trim($this->m_article->keywords) . ' ';
  138. break;
  139. case 'topics':
  140. $article .= implode('-', $this->m_article->topics) . ' ';
  141. break;
  142. }
  143. }
  144. $article = preg_replace('/[,\/\.\?"\+&]/', '', trim($article));
  145. $article = str_replace(' ', '-', $article) . '.htm';
  146. $uriString .= $article;
  147. }
  148. } else {
  149. $uriString = null;
  150. }
  151. return $uriString;
  152. } // fn getURIArticle
  153. /**
  154. * @return array
  155. * An array containing all the form parameters to print out
  156. */
  157. public function getFormParameters()
  158. {
  159. $baseParameters = array('IdLanguage','IdPublication',
  160. 'NrIssue','NrSection','NrArticle');
  161. $parameters = array();
  162. $queryParameters = $this->getQueryArray();
  163. foreach ($queryParameters as $paramName => $paramValue) {
  164. if (in_array($paramName, $baseParameters)) {
  165. continue;
  166. }
  167. $parameters[] = array('name' => $paramName, 'value' => $paramValue);
  168. }
  169. return $parameters;
  170. } // fn getFormParameters
  171. /**
  172. * Returns true if the given parameter is restricted and can not
  173. * be set from outside the URL object.
  174. *
  175. * @param string $p_parameterName
  176. * @return bool
  177. */
  178. public function isRestrictedParameter($p_parameterName)
  179. {
  180. return in_array($p_parameterName, CampURIShortNames::$m_restrictedParameters);
  181. }
  182. /**
  183. * Sets the URL values.
  184. *
  185. * Algorithm:
  186. * - identify object (e.g.: publication, language, issue, section, article)
  187. * - object defined
  188. * - valid object?
  189. * - yes: set
  190. * - no: return error
  191. * - object undefined
  192. * - has default value?
  193. * - yes: set
  194. * - no:
  195. * - object mandatory?
  196. * - yes: return error
  197. * - no: continue
  198. *
  199. * @return PEAR_Error
  200. *
  201. */
  202. private function setURL()
  203. {
  204. $this->setQueryVar('acid', null);
  205. $this->m_publication = null;
  206. $this->m_language = null;
  207. $this->m_issue = null;
  208. $this->m_section = null;
  209. $this->m_article = null;
  210. // gets the publication object based on site name (URI host)
  211. $alias = preg_replace('/^'.$this->getScheme().':\/\//', '', $this->getBase());
  212. $aliasObj = new Alias($alias);
  213. if ($aliasObj->exists()) {
  214. $this->m_publication = new MetaPublication($aliasObj->getPublicationId());
  215. }
  216. if (is_null($this->m_publication) || !$this->m_publication->defined()) {
  217. return new PEAR_Error("Invalid site name '$alias' in URL.", self::INVALID_SITE_NAME);
  218. }
  219. // reads parameters values if any
  220. $params = str_replace($this->m_config->getSetting('SUBDIR'), '', $this->getPath());
  221. $cParams = explode('/', trim($params, '/'));
  222. $cParamsSize = sizeof($cParams);
  223. if ($cParamsSize >= 1) {
  224. $cLangCode = $cParams[0];
  225. }
  226. if ($cParamsSize >= 2) {
  227. $cIssueSName = $cParams[1];
  228. }
  229. if ($cParamsSize >= 3) {
  230. $cSectionSName = $cParams[2];
  231. }
  232. if ($cParamsSize >= 4) {
  233. $cArticleSName = $cParams[3];
  234. }
  235. // gets the language identifier and sets the language code
  236. if (!empty($cLangCode)) {
  237. $langArray = Language::GetLanguages(null, $cLangCode);
  238. if (is_array($langArray) && sizeof($langArray) == 1) {
  239. $this->m_language = new MetaLanguage($langArray[0]->getLanguageId());
  240. }
  241. } else {
  242. $this->m_language = new MetaLanguage($this->m_publication->default_language->number);
  243. }
  244. if (is_null($this->m_language) || !$this->m_language->defined()) {
  245. return new PEAR_Error("Invalid language identifier in URL.", self::INVALID_LANGUAGE);
  246. }
  247. // gets the issue number and sets the issue short name
  248. if (!empty($cIssueSName)) {
  249. $publishedOnly = !$this->m_preview;
  250. $issueArray = Issue::GetIssues($this->m_publication->identifier,
  251. $this->m_language->number, null, $cIssueSName, null, $publishedOnly);
  252. if (is_array($issueArray) && sizeof($issueArray) == 1) {
  253. $this->m_issue = new MetaIssue($this->m_publication->identifier,
  254. $this->m_language->number,
  255. $issueArray[0]->getIssueNumber());
  256. } else {
  257. return new PEAR_Error("Invalid issue identifier in URL.", self::INVALID_ISSUE);
  258. }
  259. } else {
  260. $issueObj = Issue::GetCurrentIssue($this->m_publication->identifier,
  261. $this->m_language->number);
  262. $this->m_issue = new MetaIssue($this->m_publication->identifier,
  263. $this->m_language->number, $issueObj->getIssueNumber());
  264. if (!$this->m_issue->defined()) {
  265. return new PEAR_Error("No published issue was found.", self::INVALID_ISSUE);
  266. }
  267. }
  268. // gets the section number and sets the section short name
  269. if (!empty($cSectionSName)) {
  270. $sectionArray = Section::GetSections($this->m_publication->identifier,
  271. $this->m_issue->number,
  272. $this->m_language->number,
  273. $cSectionSName);
  274. if (is_array($sectionArray) && sizeof($sectionArray) == 1) {
  275. $this->m_section = new MetaSection($this->m_publication->identifier,
  276. $this->m_issue->number,
  277. $this->m_language->number,
  278. $sectionArray[0]->getSectionNumber());
  279. } else {
  280. return new PEAR_Error("Invalid section identifier in URL.", self::INVALID_SECTION);
  281. }
  282. }
  283. // gets the article number and sets the article short name
  284. if (!empty($cArticleSName)) {
  285. // we pass article short name as article identifier as they are
  286. // the same for Campsite, we will have to change this in the future
  287. $articleObj = new Article($this->m_language->number, $cArticleSName);
  288. if (!$articleObj->exists() || (!$this->m_preview && !$articleObj->isPublished())) {
  289. return new PEAR_Error("Invalid article identifier in URL.", self::INVALID_ARTICLE);
  290. }
  291. $this->m_article = new MetaArticle($this->m_language->number,
  292. $articleObj->getArticleNumber());
  293. }
  294. $templateId = CampRequest::GetVar(CampRequest::TEMPLATE_ID);
  295. $this->m_template = new MetaTemplate($this->getTemplate($templateId));
  296. if (!$this->m_template->defined()) {
  297. return new PEAR_Error("Invalid template in URL or no default template specified.",
  298. self::INVALID_TEMPLATE);
  299. }
  300. $this->m_validURI = true;
  301. $this->validateCache(false);
  302. } // fn setURL
  303. /**
  304. * Sets the URI path and query values based on given parameters.
  305. *
  306. * @param array $p_params
  307. * An array of valid URL parameters
  308. * @param boolean $p_preview
  309. * If true, will keep the preview parameters in the URL
  310. *
  311. * @return void
  312. */
  313. protected function buildURI(array &$p_params = array(), $p_preview = false)
  314. {
  315. if ($this->isValidCache()) {
  316. return;
  317. }
  318. $parameter = count($p_params) > 0 ? strtolower(array_shift($p_params)) : null;
  319. switch($parameter) {
  320. case 'language':
  321. case 'publication':
  322. $this->m_buildPath = $this->getURILanguage();
  323. if ($p_preview) {
  324. $this->m_buildQueryArray = $this->getQueryArray(CampURI::$m_previewParameters);
  325. } else {
  326. $this->m_buildQueryArray = array();
  327. }
  328. $p_params = array();
  329. break;
  330. case 'issue':
  331. $this->m_buildPath = $this->getURIIssue();
  332. if ($p_preview) {
  333. $this->m_buildQueryArray = $this->getQueryArray(CampURI::$m_previewParameters);
  334. } else {
  335. $this->m_buildQueryArray = array();
  336. }
  337. $p_params = array();
  338. break;
  339. case 'section':
  340. $this->m_buildPath = $this->getURISection();
  341. if ($p_preview) {
  342. $this->m_buildQueryArray = $this->getQueryArray(CampURI::$m_previewParameters);
  343. } else {
  344. $this->m_buildQueryArray = array();
  345. }
  346. $p_params = array();
  347. break;
  348. case 'article':
  349. $this->m_buildPath = $this->getURIArticle();
  350. if ($p_preview) {
  351. $this->m_buildQueryArray = $this->getQueryArray(CampURI::$m_previewParameters);
  352. } else {
  353. $this->m_buildQueryArray = array();
  354. }
  355. $p_params = array();
  356. break;
  357. case 'template':
  358. $option = isset($p_params[0]) ? array_shift($p_params) : null;
  359. $template = new Template($option);
  360. if (!is_null($option) && $template->exists()) {
  361. $this->m_buildQueryArray[CampRequest::TEMPLATE_ID] = $template->getTemplateId();
  362. }
  363. break;
  364. default:
  365. if (!empty($parameter)) {
  366. array_unshift($p_params, $parameter);
  367. $count = count($p_params);
  368. parent::buildURI($p_params, $p_preview);
  369. if (count($p_params) == $count) {
  370. array_shift($p_params);
  371. }
  372. }
  373. }
  374. if (count($p_params) > 0) {
  375. $this->buildURI($p_params);
  376. }
  377. if (!is_null($this->m_language) && $this->m_language->defined() && is_null($this->m_buildPath)) {
  378. $this->m_buildPath = $this->m_config->getSetting('SUBDIR') . '/' . $this->m_language->code . '/';
  379. if (!is_null($this->m_issue) && $this->m_issue->defined()) {
  380. $this->m_buildPath .= $this->m_issue->url_name . '/';
  381. if (!is_null($this->m_section) && $this->m_section->defined()) {
  382. $this->m_buildPath .= $this->m_section->url_name . '/';
  383. if (!is_null($this->m_article) && $this->m_article->defined()) {
  384. $this->m_buildPath = $this->getURIArticle();
  385. }
  386. }
  387. }
  388. }
  389. if (is_null($this->m_buildQuery)) {
  390. $this->m_buildQuery = CampURI::QueryArrayToString($this->m_buildQueryArray);
  391. }
  392. $this->validateCache(true);
  393. } // fn buildURI
  394. } // class CampURIShortNames
  395. ?>