Source for file wml-defs.php

Documentation is available at wml-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: wml-defs.php */
  22. /* Author: Paul Waite */
  23. /* Description: Definitions for WAP WML generation */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package wml */
  27. include_once("button-defs.php");
  28. /** Form elements */
  29. ("form-defs.php");
  30.  
  31. //-----------------------------------------------------------------------
  32. /**
  33. * WMLelement class
  34. * Manage WML elements. This is a virtual class used for all WML elements.
  35. * @package wml
  36. */
  37. class WMLelement extends RenderableObject {
  38. /** The content of the element */
  39.  
  40. var $body = "";
  41. // ....................................................................
  42. /**
  43. * Constructor
  44. * Create a new element object.
  45. * @param string $body Initial element body content
  46. */
  47. function WMLelement($body="") {
  48. $this->body = $body;
  49. } // WMLelement
  50. // ....................................................................
  51. /**
  52. * Insert content
  53. * Append more WML content to the body. This just concatenates
  54. * the given string onto the existing content.
  55. * @param string $wml WML string to append
  56. */
  57. function insert($content) {
  58. $this->body .= $content;
  59. return $this;
  60. } // insert
  61. // ....................................................................
  62. /**
  63. * Insert paragraph
  64. * Append content to the body inside a paragraph.
  65. * @param string $wml WML string to append inside paragraph tags
  66. */
  67. function insert_paragraph($content) {
  68. $this->insert("<p>$content</p>");
  69. return $this;
  70. } // insert_paragraph
  71. // ....................................................................
  72. /**
  73. * Insert paragraph small text
  74. * Append content to the body inside a paragraph and with small text
  75. * tags around it. Refinement of the standard insert_paragraph() method.
  76. * @param string $wml WML string to append inside paragraph tags as small text
  77. */
  78. function insert_para($content) {
  79. $this->insert_paragraph("<small>$content</small>");
  80. return $this;
  81. } // insert_para
  82. // ....................................................................
  83. /**
  84. * Insert paragraph small text scrubbed
  85. * Append content to the body inside a paragraph and with small text
  86. * tags around it. Just like insert_para() except we also scrub the
  87. * content to make sure it is clean.
  88. * @param string $wml WML string to append inside paragraph tags as small text and scrubbed
  89. */
  90. function insert_para_scrubbed($content) {
  91. $this->insert_para( scrub($content) );
  92. return $this;
  93. } // insert_para_scrubbed
  94.  
  95. } // WMLelement
  96. //-----------------------------------------------------------------------
  97.  
  98. /**
  99. * WMLfieldset class
  100. * Manage a set of fields.
  101. * @package wml
  102. */
  103. class WMLfieldset extends WMLelement {
  104. /** Title of the set of fields */
  105.  
  106. var $title = "";
  107. /** Array of fields */
  108.  
  109. var $fields;
  110. /** Total number of fields */
  111.  
  112. var $fields_total = 0;
  113. // ....................................................................
  114. /**
  115. * Constructor
  116. * Create a new fieldset object.
  117. * @param string $title Title of this set of fields
  118. */
  119. function WMLfieldset($title="") {
  120. $this->title = $title;
  121. } // WMLfieldset
  122. // ....................................................................
  123. /**
  124. * Add field
  125. * Add a field object to the fields in this fieldset.
  126. * @param object $field Field to add to the fieldset
  127. */
  128. function add_field($field) {
  129. $this->fields[$field->name] = $field;
  130. $this->fields_total += 1;
  131. return $this;
  132. } // add_field
  133. // ....................................................................
  134. /**
  135. * Return WML content
  136. * Use render() to render this element in your page.
  137. * Returns a string which is the WML for the fieldset. We render the
  138. * fieldset tags, and we iterate over the fields to fill in the
  139. * content of the fieldset.
  140. * NOTE: The field which is added is a field of a standard type such
  141. * as form_textfield, form_combofield etc. @see form-defs.php.
  142. * @return string The WML for the element.
  143. */
  144. function wml() {
  145. $wml = "<fieldset";
  146. if ($this->title != "") $wml .= " title=\"" . $this->title . "\"";
  147. $wml .= ">";
  148. reset($this->fields);
  149. while (list($fieldname, $fieldobj) = each($this->fields)) {
  150. $wml .= $fieldobj->wml() . "<br/>";
  151. }
  152. $wml .= "</fieldset>";
  153. return $wml;
  154. } // wml
  155. // ....................................................................
  156. /**
  157. * Return HTML content
  158. * Use render() to render this element in your page.
  159. * NOTE; For html this is meaningless. Just use the BACK button.
  160. * @return string The HTML for the element.
  161. */
  162. function html() {
  163. $form = new form($this->title);
  164. reset($this->fields);
  165. while (list($fieldname, $fieldobj) = each($this->fields)) {
  166. $form->add($fieldobj);
  167. }
  168. $submit = new submit_button("Submit");
  169. $form->add_button($submit);
  170. return $form->render();
  171. } // html
  172.  
  173. } // WMLfieldset
  174. //-----------------------------------------------------------------------
  175.  
  176. /**
  177. * WMLdo_element class
  178. * Do Element Class: Extends element to make a virtual class which
  179. * has descendants which are contained by a 'do' construct.
  180. * A Do element is essentially a kind of clickable link which does
  181. * something when the user clicks on it. What it does depends on
  182. * "type" of the DO. This virtual class is inherited by child
  183. * classes which implement the various different types of DO.
  184. * @package wml
  185. */
  186. class WMLdo_element extends WMLelement {
  187. /** Type of DO element: 'accept', 'prev', 'help', 'reset', 'options', or 'delete' */
  188.  
  189. var $type;
  190. /** Name of the DO element */
  191.  
  192. var $name = "";
  193. /** Label for the Do element */
  194.  
  195. var $label = "";
  196. /** True if optional */
  197.  
  198. var $optional = false;
  199. // ....................................................................
  200. /**
  201. * Constructor
  202. * Create a new do element object.
  203. * @param string $type Type of do element.
  204. * @param string $name Name of this Do element
  205. * @param string $label Label for this DO element
  206. * @param bool $optional True if DO element is optional
  207. * @param string $ontimerhref URL for ontimer event to redirect to
  208. */
  209. function WMLdo_element($type, $name, $label="", $optional=false, $ontimerhref="") {
  210. $this->WMLelement("");
  211. $this->type = $type;
  212. $this->name = $name;
  213. $this->label = chop($label);
  214. $this->optional = $optional;
  215. $this->ontimerhref = $ontimerhref;
  216. } // WMLdo_element
  217. // ....................................................................
  218. /**
  219. * Produce main WML for DO element
  220. * This is a common utility method to render the DO WML once the
  221. * details (body) has been filled in by the child class of this one.
  222. * @return string WML for the DO element
  223. */
  224. function wml() {
  225. $wml = "";
  226. $wml .= "<do type=\"" . $this->type . "\" name=\"" . $this->name . "\"";
  227. if ($this->label != "") $wml .= " label=\"" . $this->label . "\"";
  228. if ($this->optional) $wml .= " optional=\"true\"";
  229. $timerhref = href_rewrite($this->ontimerhref);
  230. if ($this->ontimerhref != "") $wml .= " ontimer=\"$timerhref\"";
  231. $wml .= ">";
  232. $wml .= $this->body;
  233. $wml .= "</do>";
  234. return $wml;
  235. } // wml
  236. // ....................................................................
  237. /**
  238. * Produce main HTML for DO element
  239. * @return string HTML for the DO element
  240. */
  241. function html() {
  242. return $this->body;
  243. } // html
  244.  
  245. } // WMLdo_element
  246. //-----------------------------------------------------------------------
  247.  
  248. /**
  249. * WMLprev class
  250. * Extends the Do Element Class. Provide the Do element of type = 'prev'.
  251. * @package wml
  252. */
  253. class WMLprev extends WMLdo_element {
  254. /**
  255. * Constructor
  256. * Create a new prev element object.
  257. * @param string $label Label for this PREV element
  258. * @param string $body Body content
  259. */
  260. function WMLprev($label, $body="") {
  261. $this->WMLdo_element("prev", "prev", $label);
  262. if ($body != "") $this-insert($body);
  263. } // WMLprev
  264. // ....................................................................
  265. /**
  266. * Return WML content
  267. * Use render() to render this element in your page.
  268. * @return string The WML for the element.
  269. */
  270. function wml() {
  271. global $RESPONSE;
  272. $wml = "";
  273. $wml .= "<prev";
  274. if ($this->body != "" || isset($RESPONSE->session_id)) {
  275. $wml .= ">" . $this->body;
  276. if (isset($RESPONSE->session_id)) {
  277. $cookie = $RESPONSE->cookiename;
  278. $sessid = $RESPONSE->session_id;
  279. $wml .= "<setvar name=\"$cookie\" value=\"$sessid\"/>";
  280. }
  281. $wml .= "</prev>";
  282. }
  283. else $wml .= "/>";
  284.  
  285. // Make this our body, and render using the
  286. // parent 'do' wml rendering routine..
  287. $this->body = $wml;
  288. return WMLdo_element::wml();
  289. } // wml
  290. // ....................................................................
  291. /**
  292. * Return HTML content
  293. * Use render() to render this element in your page.
  294. * NOTE; For html this is meaningless. Just use the BACK button.
  295. * @return string The HTML for the element.
  296. */
  297. function html() {
  298. $html = "";
  299. return $html;
  300. } // html
  301.  
  302. } // WMLprev
  303. //-----------------------------------------------------------------------
  304.  
  305. /**
  306. * WMLrefresh class
  307. * Extends the Do Element Class. Provide the Do element of type = 'refresh'.
  308. * @package wml
  309. */
  310. class WMLrefresh extends WMLdo_element {
  311. /**
  312. * Constructor
  313. * Create a new refresh DO element object.
  314. * @param string $label Label for this DO element
  315. * @param string $body Body content
  316. */
  317. function WMLrefresh($label, $body="") {
  318. $this->WMLdo_element("refresh", "refresh", $label);
  319. if ($body != "") $this-insert($body);
  320. } // WMLrefresh
  321. // ....................................................................
  322. /**
  323. * Return WML content
  324. * Use render() to render this element in your page.
  325. * @return string The WML for the element.
  326. */
  327. function wml() {
  328. global $RESPONSE;
  329. $wml = "";
  330. $wml .= "<refresh";
  331. if ($this->body != "" || isset($RESPONSE->session_id)) {
  332. $wml .= ">" . $this->body;
  333. if (isset($RESPONSE->session_id)) {
  334. $cookie = $RESPONSE->cookiename;
  335. $sessid = $RESPONSE->session_id;
  336. $wml .= "<setvar name=\"$cookie\" value=\"$sessid\"/>";
  337. }
  338. $wml .= "</refresh>";
  339. }
  340. else $wml .= "/>";
  341.  
  342. // Make this our body, and render using the
  343. // parent 'do' wml rendering routine..
  344. $this->body = $wml;
  345. return WMLdo_element::wml();
  346. } // wml
  347. // ....................................................................
  348. /**
  349. * Return HTML content
  350. * Use render() to render this element in your page.
  351. * NOTE; For html this is meaningless. Just use the REFRESH button.
  352. * @return string The HTML for the element.
  353. */
  354. function html() {
  355. $html = "";
  356. return $html;
  357. } // html
  358.  
  359. } // WMLrefresh
  360. //-----------------------------------------------------------------------
  361.  
  362. /**
  363. * WMLgo class
  364. * Extends the Do Element Class. Provide the Do element of type = 'accept'.
  365. * This is like the HTML submit button which delivers form content back
  366. * to the server after the user has entered details.
  367. * @package wml
  368. */
  369. class WMLgo extends WMLdo_element {
  370. /** URL to GO to */
  371.  
  372. var $href = "";
  373. /** Method, eg. "post" */
  374.  
  375. var $method;
  376. /** Array of fields to post */
  377.  
  378. var $postfields;
  379. // ....................................................................
  380. /**
  381. * Constructor
  382. * Create a new go DO element object.
  383. * @param string $name Name for this GO element
  384. * @param string $label Label for this GO element
  385. * @param string $href URL to go to when clicked by user
  386. * @param string $method Method, eg. "post"
  387. */
  388. function WMLgo($name, $label, $href, $method="") {
  389. $this->WMLdo_element("accept", $name, $label);
  390. $this->href = $href;
  391. $this->method = chop($method);
  392. } // WMLgo
  393. // ....................................................................
  394. /**
  395. * Define a field to post with this GO element. When fields are
  396. * defined like this the method is automatically switched to
  397. * being "post" for the GO element.
  398. * @param string $name Name of the field to post
  399. * @param string $value Value in the field to post
  400. */
  401. function postfield($name, $value="") {
  402. if ($value == "field_value") $value = "\$" . $name;
  403. $this->body .= "<postfield name=\"$name\" value=\"$value\"/>";
  404. $this->postfields[$name] = $value;
  405. $this->method = "post";
  406. return $this;
  407. } // postfield
  408. // ....................................................................
  409. /**
  410. * Return WML content
  411. * Use render() to render this element in your page.
  412. * @return string The WML for the element.
  413. */
  414. function wml() {
  415. $wml = "";
  416. $href = href_rewrite($this->href);
  417. $wml .= "<go href=\"$href\"";
  418. if ($this->method != "") $wml .= " method=\"" . $this->method . "\"";
  419. $wml .= ">\n";
  420. $wml .= $this->body;
  421. $wml .= "</go>";
  422.  
  423. // Make this our body, and render using the
  424. // parent 'do' wml rendering routine..
  425. $this->body = $wml;
  426. return WMLdo_element::wml();
  427. } // wml
  428. // ....................................................................
  429. /**
  430. * Return HTML content
  431. * Use render() to render this element in your page.
  432. * @return string The HTML for the element.
  433. */
  434. function html() {
  435. $html = "";
  436. $href = $this->href;
  437. if (isset($this->postfields) && count($this->postfields) > 0) {
  438. if (!strstr($href, "?")) {
  439. $href .= "?";
  440. }
  441. $qstr = "";
  442. while (list($fieldname, $fieldval) = each($this->postfields)) {
  443. if ($qstr != "") $qstr .= "&";
  444. $qstr .= "$fieldname=$fieldval";
  445. }
  446. $qstr = urlencode($qstr);
  447. $href .= $qstr;
  448. }
  449. $golink = new Link($href, $this->label);
  450. $hml .= "<p>" . $golink->render() . "</p>";
  451. return $html;
  452. } // html
  453.  
  454. } // WMLgo
  455. //-----------------------------------------------------------------------
  456.  
  457. /**
  458. * WMLanchor class
  459. * Provides a standard link on a WML page. It is usually an underlined
  460. * bit of text which the user can click on to go to the given URL.
  461. * This object contains a GO element to do the work of jumping to
  462. * the given URL.
  463. * @package wml
  464. */
  465. class WMLanchor extends WMLelement {
  466. /** Text to display for the anchor/link */
  467.  
  468. var $text;
  469. /** Title of the anchor */
  470.  
  471. var $title;
  472. /** Go object associated with this link */
  473.  
  474. var $go;
  475. // ....................................................................
  476. /**
  477. * Constructor
  478. * Create a new anchor (clickable link)
  479. * @param string $href URL to go to when clicked by user
  480. * @param string $text Text to display to the user
  481. * @param string $title Title for the anchor
  482. */
  483. function WMLanchor($href="", $text="Go", $title="") {
  484. $this->WMLelement("");
  485. $go = new WMLgo($href);
  486. $this->text = $text;
  487. $this->title = $title;
  488. } // WMLanchor
  489. // ....................................................................
  490. /**
  491. * Insert content into the anchor
  492. * @param string $wml Content to append to the anchor
  493. */
  494. function insert($wml) {
  495. $this->go = $this->go->insert($wml);
  496. return $this;
  497. } // insert
  498. // ....................................................................
  499. /**
  500. * Define a variable
  501. * This variable will be sent to the server when the user clicks
  502. * the link.
  503. * @param string $varname Name of the variable
  504. * @param string $value Value of the variable
  505. */
  506. function set_variable($varname, $value) {
  507. $this->insert("<setvar name=\"$varname\" value=\"$value\"/>");
  508. return $this;
  509. } // set_variable
  510. // ....................................................................
  511. /**
  512. * Return WML content
  513. * Use render() to render this element in your page.
  514. * @return string The WML for the element.
  515. */
  516. function wml() {
  517. $wml = "";
  518. $wml .= "<anchor ";
  519. if ($this->title != "") $wml .= " title=\"" . $this->title . "\"";
  520. $wml .= ">";
  521. $wml .= $this->go->wml();
  522. $wml .= "</anchor>";
  523. return $wml;
  524. } // wml
  525. // ....................................................................
  526. /**
  527. * Return HTML content
  528. * Use render() to render this element in your page.
  529. * @return string The HTML for the element.
  530. */
  531. function html() {
  532. $html = $this->go->html();
  533. return $html;
  534. } // html
  535.  
  536. } // WMLanchor
  537. //-----------------------------------------------------------------------
  538.  
  539. /**
  540. * WMLcontainer class
  541. * Container Class: Extends element class to make a virtual class for
  542. * descendants which are container of basic elements. Examples of these
  543. * would be a WMLtemplate, or a WMLcard.
  544. * @package wml
  545. */
  546. class WMLcontainer extends WMLelement {
  547. /** URL to go to when timer expires */
  548.  
  549. var $ontimerhref;
  550. /** Timer expiry interval (in 10ths of seconds) */
  551.  
  552. var $timerval;
  553. // ....................................................................
  554. /**
  555. * Constructor
  556. * Create a new container object.
  557. * @param string $body Initial body content
  558. */
  559. function WMLcontainer($body="") {
  560. $this->WMLelement($body);
  561. $this->ontimerhref = "";
  562. } // WMLcontainer
  563. // ....................................................................
  564. /**
  565. * Activate a timer
  566. * Sets up the parameters for a timer.
  567. * @param string $href URL to go to on timer expiry
  568. * @param integer $secs Timer expiry time, in seconds
  569. */
  570. function set_ontimer($href, $secs=1) {
  571. $this->ontimerhref = $href;
  572. $this->timerval = $secs * 10;
  573. } // set_ontimer
  574. // ....................................................................
  575. /**
  576. * Insert GO element into container
  577. * @param string $name Name for this GO element
  578. * @param string $label Label for this GO element
  579. * @param string $href URL to go to when clicked by user
  580. */
  581. function insert_go($name, $label, $href) {
  582. $d = new WMLgo($name, $label, $href);
  583. $this->insert($d->render());
  584. } // insert_go
  585. // ....................................................................
  586. /**
  587. * Insert PREV element into container
  588. * @param string $label Label for this PREV element
  589. */
  590. function insert_prev($label) {
  591. $d = new WMLprev($label);
  592. $this->insert($d->render());
  593. } // insert_prev
  594. // ....................................................................
  595. /**
  596. * Insert REFRESH element into container
  597. * @param string $label Label for this REFRESH element
  598. */
  599. function insert_refresh($label) {
  600. $d = new WMLrefresh($label);
  601. $this->insert($d->render());
  602. } // insert_refresh
  603.  
  604. } // WMLcontainer
  605. //-----------------------------------------------------------------------
  606.  
  607. /**
  608. * WMLtemplate class
  609. * Extends container class to make a template. These are useful when
  610. * you have a bunch of cards which you want to all have a common
  611. * set of options.
  612. * @package wml
  613. */
  614. class WMLtemplate extends WMLcontainer {
  615. /**
  616. * Constructor
  617. * Create a new template object.
  618. * @param string $body Initial body content
  619. */
  620. function WMLtemplate($body="") {
  621. $this->WMLcontainer($body);
  622. } // WMLtemplate
  623. // ....................................................................
  624. /**
  625. * Return WML content
  626. * Use render() to render this element in your page.
  627. * Returns a string which is the WML for the template.
  628. * @return string The WML for the element.
  629. */
  630. function wml() {
  631. $wml = "";
  632. $wml .= "<template";
  633. $timerhref = href_rewrite($this->ontimerhref);
  634. if ($timerhref != "") $wml .= " ontimer=\"$timerhref\"";
  635. $wml .= ">";
  636. if ($timerhref != "") $wml .= "<timer value=\"$this->timerval\"/>";
  637. $wml .= $this->body;
  638. $wml .= "</template>";
  639. return $wml;
  640. } // wml
  641. // ....................................................................
  642. /**
  643. * Return HTML content
  644. * Use render() to render this element in your page.
  645. * @return string The HTML for the element.
  646. */
  647. function html() {
  648. return $this->body;
  649. } // html
  650.  
  651. } // WMLtemplate
  652. //-----------------------------------------------------------------------
  653.  
  654. /**
  655. * WMLcard class
  656. * Manage WML cards.
  657. * These are effectively the "webpages" of the WAP world.
  658. * @package wml
  659. */
  660. class WMLcard extends WMLcontainer {
  661. /** Unique card identifier string */
  662.  
  663. var $card_id = "";
  664. /** Card title/banner */
  665.  
  666. var $title = "";
  667. /** True if card has its own context */
  668.  
  669. var $newcontext = false;
  670. /** True if the card is "ordered" */
  671.  
  672. var $ordered = false;
  673. // ....................................................................
  674. /**
  675. * Constructor
  676. * Create a new WML card.
  677. * @param string $card_id The unique card identifier string
  678. * @param string $title The card title string
  679. * @param string $newcontext True if card has a new context when shown
  680. * @param string $ordered True if card is ordered
  681. */
  682. function WMLcard($card_id="main", $title="", $newcontext=false, $ordered=false) {
  683. $this->WMLcontainer("");
  684. $this->card_id = $card_id;
  685. $this->title = chop($title);
  686. $this->newcontext = $newcontext;
  687. $this->ordered = $ordered;
  688. $this->ontimerhref = "";
  689. } // WMLcard
  690. // ....................................................................
  691. /**
  692. * Return WML content
  693. * Use render() to render this element in your page.
  694. * Returns a string which is the WML for the card. This is basically
  695. * just a case of rendering the various properties like card_id etc.
  696. * as they were defined for the card.
  697. * @return string The WML for the element.
  698. */
  699. function wml() {
  700. global $RESPONSE;
  701. // Deal with OpenWave phones not showing a card title..
  702. if (isset($RESPONSE)
  703. && $RESPONSE->browser_type == BROWSER_TYPE_WMLUP
  704. && $this->title != "") {
  705. $s = "<p><small><b>" . $this->title . "</b></small></p>";
  706. $this->body = $s . $this->body;
  707. }
  708. $wml = "";
  709. $timerhref = href_rewrite($this->ontimerhref);
  710. $wml .= "<card";
  711. if ($this->title != "") $wml .= " title=\"$this->title\"";
  712. if ($this->card_id != "") $wml .= " id=\"$this->card_id\"";
  713. if ($timerhref != "") $wml .= " ontimer=\"$timerhref\"";
  714. if ($this->newcontext) $wml .= " newcontext=\"true\"";
  715. if ($this->ordered) $wml .= " ordered=\"true\"";
  716. $wml .= ">";
  717. if ($timerhref != "") $wml .= "<timer value=\"$this->timerval\"/>";
  718. $wml .= $this->body;
  719. $wml .= "</card>";
  720. return $wml;
  721. } // wml
  722. // ....................................................................
  723. /**
  724. * Return HTML content
  725. * Use render() to render this element in your page.
  726. * @return string The HTML for the element.
  727. */
  728. function html() {
  729. $html = "";
  730. if ($this->title != "") {
  731. $html .= "<h3>$this->title</h3>";
  732. }
  733. if ($this->ontimerhref != "") {
  734. $timerlink = new Link($this->ontimerhref, "$this->ontimerhref (" . $this->timerval/10 . " secs)");
  735. $hml .= "<p>" . $timerlink->render() . "</p>";
  736. }
  737. $html .= $this->body;
  738. return $html;
  739. } // html
  740.  
  741. } // WMLcard
  742. // ----------------------------------------------------------------------
  743.  
  744. /**
  745. * WMLDeck class
  746. * Manage WML Decks of cards.
  747. * The main element in WML content - the deck which
  748. * holds all the cards.
  749. * @package wml
  750. */
  751. class WMLdeck extends RenderableObject {
  752. /** Optional template */
  753.  
  754. var $template;
  755. /** Array of card objects */
  756.  
  757. var $cards;
  758. /** Length of content in bytes */
  759.  
  760. var $wml_len = 0;
  761. // ....................................................................
  762. /**
  763. * Constructor
  764. * Create a new WML deck.
  765. * @param object $card A card to put in the deck
  766. */
  767. function WMLdeck($card="") {
  768. if ($card != "") {
  769. $this->insert_card($card);
  770. }
  771. } // WMLdeck
  772. // ....................................................................
  773. /**
  774. * Length (size) of deck
  775. * Return the length of the WML. NOTE: this should only be called
  776. * *after* the wml() function has been called, or it will return zero.
  777. * @return integer The size of the deck of WML
  778. */
  779. function length() {
  780. return $this->wml_len;
  781. } // length
  782. // ....................................................................
  783. /**
  784. * Calculate the length (size) of deck
  785. * Calculates the WML length from scratch.
  786. * @return integer The calculated size of the deck of WML
  787. */
  788. function calculate_length() {
  789. $this->wml_len = strlen($this->wml());
  790. return $this->wml_len;
  791. } // calculate_length
  792. // ....................................................................
  793. /**
  794. * Define template
  795. * defines the template for the deck.
  796. * @param object $template The template object for the WML deck
  797. */
  798. function insert_template($template) {
  799. if (is_object($template)) {
  800. $this->template = $template;
  801. }
  802. return $this;
  803. } // insert_template
  804. // ....................................................................
  805. /**
  806. * Add card
  807. * Inserts a ready-made card object into the deck.
  808. * @param object $card The card object to add to the WML deck
  809. */
  810. function insert_card($card) {
  811. $this->cards[$card->card_id] = $card;
  812. return $this;
  813. } // insert_card
  814. // ....................................................................
  815. /**
  816. * New card
  817. * Creates a card and inserts the new card object into the deck.
  818. * @param string $card_id The unique card identifier string
  819. * @param string $title The card title string
  820. * @param string $newcontext True if card has a new context when shown
  821. * @param string $ordered True if card is ordered
  822. */
  823. function new_card($card_id, $title="", $newcontext=false, $ordered=false) {
  824. $this->cards[$card_id] = new WMLcard($card_id, $title, $newcontext, $ordered);
  825. return $this;
  826. } // new_card
  827. // ....................................................................
  828. /**
  829. * Return complete WML response content including DOCTYPE, <wml> tags,
  830. * and the content-length header.
  831. * @return string The complete WML for this deck
  832. */
  833. function response() {
  834. $s = "";
  835. $s .= "<?xml version=\"1.0\"?>\n";
  836. $s .= "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" ";
  837. $s .= "\"http://www.wapforum.org/DTD/wml_1.1.xml\">\n";
  838. $s .= "<wml>";
  839. $s .= $this->wml();
  840. $s .= "</wml>";
  841.  
  842. // Set the length..
  843. $this->wml_len = strlen($this->wml);
  844.  
  845. // Output the header for wml length..
  846. Header("Content-Length: " . $this->wml_len);
  847. return $s;
  848. } // response
  849. // ....................................................................
  850. /**
  851. * Return WML content for this deck.
  852. * Use render() to render this element in your page.
  853. * Returns a string which is the WML for the deck. First we render any
  854. * template and then we iterate over all the cards defined for the deck.
  855. * @return string The WML for the deck of cards complete.
  856. */
  857. function wml() {
  858. $s = "";
  859. if (isset($this->template)) {
  860. $s .= $this->template->wml();
  861. }
  862. foreach($this->cards as $card) {
  863. $s .= $card->wml();
  864. }
  865. return $s;
  866. } // wml
  867. // ....................................................................
  868. /**
  869. * Return HTML content
  870. * Use render() to render this element in your page.
  871. * Returns a string which is the HTML for the deck.
  872. * @return string The HTML for the element.
  873. */
  874. function html() {
  875. // Define a webpage..
  876. $page = new webpage("WML Viewer");
  877. // Render all cards..
  878. foreach ($this->cards as $card) {
  879. $page->add( $card->html() );
  880. }
  881. // Return the content of the whole page..
  882. return $page->render();
  883. } // html
  884.  
  885. } // WMLdeck
  886. // ----------------------------------------------------------------------
  887.  
  888. /**
  889. * Scrubs the given string to make it acceptable to touchy WML parsers
  890. * in cellphones.
  891. * @param string $s String to scrub
  892. * @return string Scrubbed version of given string
  893. */
  894. function scrub($s) {
  895. global $RESPONSE;
  896. $res = strip_tags($s);
  897. $res = ereg_replace( "[\r\n ]+", " ", $res);
  898. $res = ereg_replace( "&([#0-9a-z]+);", "aMpErSaNd-\\1;", $res);
  899. $res = ereg_replace( "&", "&amp;", $res);
  900. $res = ereg_replace( "aMpErSaNd-", "&", $res);
  901. if ($RESPONSE->browser == BROWSER_PHONE) {
  902. $res = ereg_replace( "&copy;", "(c)", $res);
  903. $res = ereg_replace( "[$]", "$$", $res);
  904. }
  905. return $res;
  906. } // scrub
  907. // ----------------------------------------------------------------------
  908.  
  909. /**
  910. * Re-writes the given HREF, appending session ID if found. Appends the
  911. * session properly when pre-existing query parameters are already on
  912. * the href string. Omits the session id if the string contains the
  913. * pattern "logoff", since this is assumed to mean that the session is
  914. * being terminated. Also omits it if already present.
  915. * @param string $href HREF to rewrite
  916. * @return string Re-written HREF
  917. */
  918. function href_rewrite($href) {
  919. global $RESPONSE;
  920. if ($href != "" && isset($RESPONSE) && isset($RESPONSE->session_id)) {
  921. $cookie = $RESPONSE->cookiename;
  922. $sessid = $RESPONSE->session_id;
  923. if (($cookie != "")
  924. && !stristr($href, $cookie)
  925. && !stristr($href, "logoff")) {
  926. if ((strtolower(substr($href,0,4)) != "http") && (substr($href,0,1) != "#")) {
  927. if (strstr($href, "?")) $href .= "&amp;$cookie=$sessid";
  928. else $href .= "?$cookie=$sessid";
  929. }
  930. }
  931. }
  932. return $href;
  933. } // href_rewrite
  934. // ----------------------------------------------------------------------
  935.  
  936. ?>

Documentation generated by phpDocumentor 1.3.0RC3