PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/application/helpers/page.php

https://github.com/fb83/Project-Pier
PHP | 853 lines | 324 code | 119 blank | 410 comment | 48 complexity | 33b1ff771b7e8a3f4f0e17d85f7be791 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, AGPL-3.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * Open HTML tag
  4. *
  5. * @access public
  6. * @param string $name Tag name
  7. * @param array $attributes Array of tag attributes
  8. * @param boolean $empty If tag is empty it will be automaticly closed
  9. * @return string
  10. */
  11. function open_html_tag($name, $attributes = null, $empty = false) {
  12. $attribute_string = '';
  13. if (is_array($attributes) && count($attributes)) {
  14. $prepared_attributes = array();
  15. foreach ($attributes as $k => $v) {
  16. if (trim($k) <> '') {
  17. if (is_bool($v)) {
  18. if ($v) {
  19. $prepared_attributes[] = "$k=\"$k\"";
  20. }
  21. } else {
  22. $prepared_attributes[] = $k . '="' . clean($v) . '"';
  23. } // if
  24. } // if
  25. } // foreach
  26. $attribute_string = implode(' ', $prepared_attributes);
  27. } // if
  28. $empty_string = $empty ? ' /' : ''; // Close?
  29. return "<$name $attribute_string$empty_string>"; // And done...
  30. } // html_tag
  31. /**
  32. * Close specific HTML tag
  33. *
  34. * @access public
  35. * @param string $name Tag name
  36. * @return string
  37. */
  38. function close_html_tag($name) {
  39. return "</$name>";
  40. } // close_html_tag
  41. /**
  42. * Return title tag
  43. *
  44. * @access public
  45. * @param string $title
  46. * @return string
  47. */
  48. function title_tag($title) {
  49. return open_html_tag('title') . $title . close_html_tag('title');
  50. } // title_tag
  51. /**
  52. * Prepare link tag
  53. *
  54. * @access public
  55. * @param string $href
  56. * @param string $rel_or_rev Rel or rev
  57. * @param string $rel
  58. * @param array $attributes
  59. * @return string
  60. */
  61. function link_tag($href, $rel_or_rev = 'rel', $rel = 'alternate', $attributes = null) {
  62. // Prepare attributes
  63. $all_attributes = array(
  64. 'href' => $href,
  65. $rel_or_rev => $rel
  66. ); // array
  67. // Additional attributes
  68. if (is_array($attributes) && count($attributes)) {
  69. $all_attributes = array_merge($all_attributes, $attributes);
  70. } // if
  71. // And done!
  72. return open_html_tag('link', $all_attributes, true);
  73. } // link_tag
  74. /**
  75. * Rel link tag
  76. *
  77. * @access public
  78. * @param string $href
  79. * @param string $rel
  80. * @param string $attributes
  81. * @return string
  82. */
  83. function link_tag_rel($href, $rel, $attributes = null) {
  84. return link_tag($href, 'rel', $rel, $attributes);
  85. } // link_tag_rel
  86. /**
  87. * Rev link tag
  88. *
  89. * @access public
  90. * @param string $href
  91. * @param string $rel
  92. * @param string $attributes
  93. * @return string
  94. */
  95. function link_tag_rev($href, $rel, $attributes = null) {
  96. return link_tag($href, 'rev', $rel, $attributes);
  97. } // link_tag_rev
  98. /**
  99. * Return code of meta tag
  100. *
  101. * @access public
  102. * @param string $name Name of the meta property
  103. * @param string $content
  104. * @param boolean $http_equiv
  105. * @return string
  106. */
  107. function meta_tag($name, $content, $http_equiv = false) {
  108. // Name attribute
  109. $name_attribute = $http_equiv ? 'http-equiv' : 'name';
  110. // Prepare attributes
  111. $attributes = array(
  112. $name_attribute => $name,
  113. 'content' => $content
  114. ); // array
  115. // And done...
  116. return open_html_tag('meta', $attributes, true);
  117. } // meta_tag
  118. /**
  119. * Generate javascript tag
  120. *
  121. * @access public
  122. * @param string $src Path to external file
  123. * @param string $content Tag content
  124. * @return string
  125. */
  126. function javascript_tag($src = null, $content = null) {
  127. // Content formatting
  128. if ($content) {
  129. $content = "\n" . $content . "\n";
  130. }
  131. // Prepare attributes
  132. $attributes = array('type' => 'text/javascript');
  133. if (!is_null($src)) {
  134. $attributes['src'] = is_valid_url($src) ? $src : get_javascript_url($src);
  135. } // if
  136. // Generate
  137. return open_html_tag('script', $attributes) . $content . close_html_tag('script');
  138. } // javascript_tag
  139. /**
  140. * Render stylesheet tag
  141. *
  142. * @access public
  143. * @param string $href URL of external stylesheet
  144. * @Param array $attributes
  145. * @return string
  146. */
  147. function stylesheet_tag($href, $attributes = null) {
  148. if (!is_valid_url($href)) {
  149. $href = get_stylesheet_url($href);
  150. } // if
  151. $all_attributes = array(
  152. 'type' => 'text/css'
  153. ); // array
  154. if (is_array($attributes) && count($attributes)) {
  155. array_merge($all_attributes, $attributes);
  156. } // if
  157. return link_tag($href, 'rel', 'Stylesheet', $all_attributes);
  158. } // stylesheet_tag
  159. /**
  160. * Render style tag inside optional conditional comment
  161. *
  162. * @access public
  163. * @param string $content
  164. * @param string $condition Condition for conditional comment (IE, lte IE6...). If null
  165. * conditional comment will not be added
  166. * @return string
  167. */
  168. function style_tag($content, $condition = null) {
  169. // Open and close for conditional comment
  170. $open = '';
  171. $close = '';
  172. if ($condition) {
  173. $open = "<!--[if $condition]>\n";
  174. $close = '<![endif]-->';
  175. } // if
  176. // And return...
  177. return $open . open_html_tag('style', array('type' => 'text/css')) .
  178. "\n" . $content . "\n" .
  179. close_html_tag('style') . "\n" . $close;
  180. } // style_tag
  181. /**
  182. * Style class for sequence number
  183. *
  184. * @access public
  185. * @param integer $seq_num
  186. * @return string
  187. */
  188. function odd_even_class(&$seq_num) {
  189. (isset($seq_num)) ? $seq_num++ : $seq_num = 1;
  190. return (($seq_num % 2) == 0) ? 'even' : 'odd';
  191. }
  192. /**
  193. * Page description class
  194. *
  195. * Class that hold XHTML page properties. This class can be used from templates
  196. * to inform layouts that templates want some proprety changes. It is entirely
  197. * on layout to decide if it will use properties from instance of this class or
  198. * some other set of values.
  199. *
  200. * @version 1.0
  201. * @http://www.projectpier.org/
  202. */
  203. class PageDescription {
  204. /**
  205. * Page title
  206. *
  207. * @var string
  208. */
  209. private $title;
  210. /**
  211. * Array of page metadata
  212. *
  213. * @var array
  214. */
  215. private $meta = array();
  216. /**
  217. * Array of links
  218. *
  219. * @var array
  220. */
  221. private $links = array();
  222. /**
  223. * Array of inline CSS rules
  224. *
  225. * @var array
  226. */
  227. private $inline_css = array();
  228. /**
  229. * Array of links to external JS files
  230. *
  231. * @var array
  232. */
  233. private $javascript = array();
  234. /**
  235. * Array of inline JS code
  236. *
  237. * @var array
  238. */
  239. private $inline_js = array();
  240. /**
  241. * Get title
  242. *
  243. * @access public
  244. * @param null
  245. * @return string
  246. */
  247. function getTitle() {
  248. return $this->title;
  249. } // getTitle
  250. /**
  251. * Set title value
  252. *
  253. * @access public
  254. * @param string $value
  255. * @return null
  256. */
  257. function setTitle($value) {
  258. $this->title = $value;
  259. } // setTitle
  260. /**
  261. * Get meta
  262. *
  263. * @access public
  264. * @param null
  265. * @return type
  266. */
  267. function getMeta() {
  268. return $this->meta;
  269. } // getMeta
  270. /**
  271. * Add page meta entry
  272. *
  273. * @access public
  274. * @param string $name
  275. * @param string $content
  276. * @param boolean $http_equiv
  277. * @return null
  278. */
  279. function addMeta($name, $content, $http_equiv = false) {
  280. $this->meta[] = array(
  281. 'name' => $name,
  282. 'content' => $content,
  283. 'http_equiv' => $http_equiv
  284. ); // array
  285. } // addMeta
  286. /**
  287. * Get links
  288. *
  289. * @access public
  290. * @param null
  291. * @return array
  292. */
  293. function getLinks() {
  294. return $this->links;
  295. } // getLinks
  296. /**
  297. * Add rel link
  298. *
  299. * @param string $href Link locator
  300. * @param string $rel Rel value
  301. * @param string $attributes
  302. * @return mixed
  303. */
  304. function addRelLink($href, $rel = 'alternate', $attributes) {
  305. $this->addLink($href, 'rel', $rel, $attributes);
  306. } // end func addRelMedia
  307. /**
  308. * Add rev link
  309. *
  310. * @param string $href Link locator
  311. * @param string $rev Rev value
  312. * @param array $attributes
  313. * @return mixed
  314. */
  315. function addRevLink($href, $rev = 'alternate', $attributes = null) {
  316. $this->addLink($href, 'rev', $rev, $attributes);
  317. } // end func addRevLink
  318. /**
  319. * Add link
  320. *
  321. * @access public
  322. * @param string $href
  323. * @param string $rel_or_rev
  324. * @param string $rel
  325. * @param array $attributes
  326. * @return null
  327. */
  328. function addLink($href, $rel_or_rev = 'rel', $rel = 'alternate', $attributes = null) {
  329. // Do we have this link?
  330. foreach ($this->links as $link) {
  331. if (array_var($link, 'href') == $href) {
  332. return;
  333. }
  334. }
  335. // Prepare link attributes...
  336. $link = array(
  337. 'href' => $href,
  338. $rel_or_rev => $rel
  339. ); // array
  340. // Additional attributes
  341. if (is_array($attributes) && count($attributes)) {
  342. $link = array_merge($link, $attributes);
  343. }
  344. $this->links[] = $link;
  345. } // addLink
  346. /**
  347. * Get inline_css
  348. *
  349. * @access public
  350. * @param null
  351. * @return array
  352. */
  353. function getInlineCSS() {
  354. return $this->inline_css;
  355. } // getInlineCSS
  356. /**
  357. * Add inline CSS block
  358. *
  359. * @access public
  360. * @param string $content
  361. * @param string $condition
  362. * @return null
  363. */
  364. function addInlineCSS($content, $condition = null) {
  365. $this->inline_css[] = array(
  366. 'content' => $content,
  367. 'condition' => $condition
  368. ); // array
  369. } // addInlineCSS
  370. /**
  371. * Get javascription
  372. *
  373. * @access public
  374. * @param null
  375. * @return array
  376. */
  377. function getJavascript() {
  378. return $this->javascript;
  379. } // getJavascript
  380. /**
  381. * Add external JS link
  382. *
  383. * @access public
  384. * @param string $src JS file URL
  385. * @return null
  386. */
  387. function addJavascript($src) {
  388. $this->javascript[] = $src;
  389. } // addJavascript
  390. /**
  391. * Check if specific javascript file is included on page
  392. *
  393. * @access public
  394. * @param string $src
  395. * @return boolean
  396. */
  397. function isJavascriptIncluded($src) {
  398. return in_array($src, $this->inline_js);
  399. } // isJavascriptIncluded
  400. /**
  401. * Get inline_js
  402. *
  403. * @access public
  404. * @param null
  405. * @return array
  406. */
  407. function getInlineJS() {
  408. return $this->inline_js;
  409. } // getInlineJS
  410. /**
  411. * Add inline JS entry
  412. *
  413. * @access public
  414. * @param string
  415. * @return null
  416. */
  417. function addInlineJS($content) {
  418. $this->inline_js[] = $content;
  419. } // addInlineJS
  420. /**
  421. * Return single PageDescription instance
  422. *
  423. * @access public
  424. * @param void
  425. * @return PageDescription
  426. */
  427. static function instance() {
  428. static $instance;
  429. if (!instance_of($instance, 'PageDescription')) {
  430. $instance = new PageDescription();
  431. } // if
  432. return $instance;
  433. } // instance
  434. } // PageDescription
  435. /**
  436. * Return page title
  437. *
  438. * @access public
  439. * @param void
  440. * @return string
  441. */
  442. function get_page_title() {
  443. $page = PageDescription::instance();
  444. // If we dont have title use action
  445. if ($page->getTitle() == '') {
  446. $action = array_var($_GET, 'action');
  447. return $action ? ucfirst($action) : PRODUCT_NAME;
  448. } else {
  449. return $page->getTitle();
  450. } // if
  451. } // get_page_title
  452. /**
  453. * Set page title
  454. *
  455. * @access public
  456. * @param string $value
  457. * @return null
  458. */
  459. function set_page_title($value) {
  460. PageDescription::instance()->setTitle(clean($value));
  461. } // set_page_title
  462. /**
  463. * Add external stylesheet file to page
  464. *
  465. * @access public
  466. * @param string $href
  467. * @param string $title
  468. * @param string $media
  469. * @return null
  470. */
  471. function add_stylesheet_to_page($href, $title = null, $media = null) {
  472. if (!is_valid_url($href)) {
  473. $href = get_stylesheet_url($href);
  474. }
  475. $page = PageDescription::instance();
  476. $attributes = array('type' => 'text/css');
  477. if ($title) $attributes['title'] = $title;
  478. if ($media) $attributes['media'] = $media;
  479. $page->addRelLink($href, 'Stylesheet', $attributes); // addRelLink
  480. } // add_stylesheet_to_page
  481. /**
  482. * Add external JS to page
  483. *
  484. * @access public
  485. * @param string $src URL of external JS file
  486. * @return null
  487. */
  488. function add_javascript_to_page($src) {
  489. $page = PageDescription::instance();
  490. $page->addJavascript($src);
  491. } // add_javascript_to_page
  492. /**
  493. * Add inline JS to page
  494. *
  495. * @access public
  496. * @param string $content
  497. * @return null
  498. */
  499. function add_inline_javascript_to_page($content) {
  500. $page = PageDescription::instance();
  501. $page->addInlineJS($content);
  502. } // add_inline_javascript_to_page
  503. /**
  504. * Add inline CSS to page
  505. *
  506. * @access public
  507. * @param string $content
  508. * @param string $condition
  509. * @return null
  510. */
  511. function add_inline_css_to_page($content, $condition = null) {
  512. $page = PageDescription::instance();
  513. $page->addInlineCSS($content, $condition);
  514. } // add_inline_css_to_page
  515. /**
  516. * Return generated page meta code
  517. *
  518. * @access public
  519. * @param void
  520. * @return string
  521. */
  522. function render_page_meta() {
  523. // Get page instance...
  524. $page = PageDescription::instance();
  525. // Get meta...
  526. $meta = $page->getMeta();
  527. // Generated code...
  528. $generated_code = '';
  529. if (is_array($meta) && count($meta)) {
  530. $generated = array();
  531. foreach ($meta as $data) {
  532. $generated[] = meta_tag($data['name'], $data['content'], $data['http_equiv']);
  533. } // foreach
  534. $generated_code = implode("\n", $generated);
  535. } // if
  536. // Return...
  537. return $generated_code;
  538. } // render_page_meta
  539. /**
  540. * Render page links
  541. *
  542. * @access public
  543. * @param void
  544. * @return string
  545. */
  546. function render_page_links() {
  547. // Get page instance...
  548. $page = PageDescription::instance();
  549. // Getlinks...
  550. $links = $page->getLinks();
  551. // Prepare result
  552. $generated_code = '';
  553. // If we have links go...
  554. if (is_array($links) && count($links)) {
  555. $generated = array();
  556. foreach ($links as $data) {
  557. $href = array_var($data, 'href');
  558. $rel_or_rev = isset($data['rel']) ? 'rel' : 'rev';
  559. $rel = '';
  560. if (isset($data[$rel_or_rev])) {
  561. $rel = $data[$rel_or_rev];
  562. unset($data[$rel_or_rev]);
  563. } // if
  564. $generated[] = link_tag($href, $rel_or_rev, $rel, $data);
  565. } // if
  566. $generated_code = implode("\n", $generated);
  567. } // if
  568. return $generated_code;
  569. } // render_page_links
  570. /**
  571. * Render page inline CSS
  572. *
  573. * @access public
  574. * @param void
  575. * @return string
  576. */
  577. function render_page_inline_css() {
  578. // Get page instance...
  579. $page = PageDescription::instance();
  580. // Get inline CSS
  581. $css = $page->getInlineCSS();
  582. // Prepare result...
  583. $generated_code = '';
  584. // And get code...
  585. if (is_array($css) && count($css)) {
  586. $generated = array();
  587. foreach ($css as $data) {
  588. // Get...
  589. $content = array_var($data, 'content');
  590. $condition = array_var($data, 'condition');
  591. // If we have content generate tag...
  592. if ($content) {
  593. $generated[] = style_tag($content, $condition);
  594. }
  595. } // foreach
  596. $generated_code = implode("\n", $generated);
  597. } // if
  598. // And done!
  599. return $generated_code;
  600. } // render_page_inline_css
  601. /**
  602. * Render javascript tags
  603. *
  604. * @access public
  605. * @param void
  606. * @return string
  607. */
  608. function render_page_javascript() {
  609. // Get page instance...
  610. $page = PageDescription::instance();
  611. // Get page javascript...
  612. $javascript = $page->getJavascript();
  613. // Prepare result...
  614. $generated_code = '';
  615. // Loop...
  616. if (is_array($javascript) && count($javascript)) {
  617. $generated = array();
  618. foreach ($javascript as $data) {
  619. $generated[] = javascript_tag($data);
  620. } // foreach
  621. $generated_code = implode("\n", $generated);
  622. } // if
  623. // Done...
  624. return $generated_code;
  625. } // render_page_javascript
  626. /**
  627. * Render inline javascript
  628. *
  629. * @access public
  630. * @param void
  631. * @return string
  632. */
  633. function render_page_inline_js() {
  634. // Get page instance...
  635. $page = PageDescription::instance();
  636. // Get page javascript...
  637. $javascript = $page->getInlineJS();
  638. // Prepare result...
  639. $generated_code = '';
  640. // Loop...
  641. if (is_array($javascript) && count($javascript)) {
  642. $generated = array();
  643. foreach ($javascript as $data) {
  644. $generated[] = javascript_tag(null, $data);
  645. } // foreach
  646. $generated_code = implode("\n", $generated);
  647. } // if
  648. // Done...
  649. return $generated_code;
  650. } // render_page_inline_js
  651. /**
  652. * Render page head...
  653. *
  654. * @access public
  655. * @param string $title
  656. * @return string
  657. */
  658. function render_page_head() {
  659. $page = PageDescription::instance();
  660. $head = render_page_links() . "\n" .
  661. render_page_meta() . "\n" .
  662. render_page_javascript() . "\n" .
  663. render_page_inline_js() . "\n" .
  664. render_page_inline_css();
  665. return trim($head) . "\n";
  666. } // render_page_head
  667. /**
  668. * Return URL relative to public folder
  669. *
  670. * @param string $rel
  671. * @return string
  672. */
  673. function get_public_url($rel) {
  674. $base = trim(PUBLIC_FOLDER) == '' ? with_slash(ROOT_URL) : with_slash(with_slash(ROOT_URL) . PUBLIC_FOLDER);
  675. return $base . $rel;
  676. } // get_public_url
  677. /**
  678. * Return URL of specific file in /public/files
  679. *
  680. * @param string $file_name Name of the file or path relative to /public/files/
  681. * @return string
  682. */
  683. function get_file_url($file_name) {
  684. return get_public_url('files/' . $file_name);
  685. } // get_file_url
  686. /**
  687. * Return javascript URL
  688. *
  689. * @param string $file_name
  690. * @return string
  691. */
  692. function get_javascript_url($file_name) {
  693. if (file_exists(THEMES_DIR.'/'.config_option('theme')."/javascript/$file_name"))
  694. return get_public_url('assets/themes/'.config_option('theme')."/javascript/$file_name");
  695. else
  696. return get_public_url('assets/javascript/' . $file_name);
  697. } // get_javascript_url
  698. /**
  699. * Return URL of specific stylesheet
  700. *
  701. * @param string $file_name
  702. * @return string
  703. */
  704. function get_stylesheet_url($file_name) {
  705. static $theme = null;
  706. if (is_null($theme)) {
  707. if (function_exists('config_option')) {
  708. $theme = config_option('theme');
  709. } // if
  710. if (trim($theme) == '') {
  711. $theme = DEFAULT_THEME;
  712. } // if
  713. } // if
  714. return get_public_url("assets/themes/$theme/stylesheets/$file_name");
  715. } // get_stylesheet_url
  716. /**
  717. * Return image URl
  718. *
  719. * @param string $file_name
  720. * @return string
  721. */
  722. function get_image_url($file_name) {
  723. static $theme = null;
  724. if (is_null($theme)) {
  725. if (function_exists('config_option')) {
  726. $theme = config_option('theme');
  727. } // if
  728. if (trim($theme) == '') {
  729. $theme = DEFAULT_THEME;
  730. } // if
  731. } // if
  732. return get_public_url("assets/themes/$theme/images/$file_name");
  733. } // get_image_url
  734. ?>