/php/render.inc.php

https://github.com/yoyar/tprsp · PHP · 408 lines · 276 code · 125 blank · 7 comment · 11 complexity · a807bdf1e9c83792be1c328af0308891 MD5 · raw file

  1. <?php
  2. function tprsp_twitterlink($str) {
  3. $regex = '/(@\w+)/';
  4. $result = preg_replace(
  5. $regex,
  6. '<a href="https://twitter.com/$1" rel="nofollow">$1</a>',
  7. $str
  8. );
  9. return $result;
  10. }
  11. interface SectionRenderer {
  12. public function render();
  13. }
  14. abstract class BaseRenderer implements SectionRenderer {
  15. protected $section;
  16. function __construct($section) {
  17. $this->section = $section;
  18. }
  19. private function preRender() {
  20. $title = $this->getSectionTitle();
  21. $class = strtolower($this->getSectionTitle());
  22. $pre = sprintf(
  23. '<div class="section %s"> <div>%s:</div> <div class="section-content">',
  24. $class,
  25. $title
  26. );
  27. return $pre;
  28. }
  29. private function postRender() {
  30. return '</div></div>';
  31. }
  32. public function render() {
  33. $content = $this->getContentArray();
  34. array_walk(
  35. $content,
  36. function (&$content) {
  37. $content = trim($content);
  38. $content = htmlentities($content);
  39. $content = tprsp_twitterlink($content);
  40. }
  41. );
  42. return $this->preRender() .
  43. vsprintf($this->getFormat(), $content) .
  44. $this->postRender();
  45. }
  46. abstract protected function getSectionTitle();
  47. abstract protected function getFormat();
  48. abstract protected function getContentArray();
  49. }
  50. class WeekRenderer extends BaseRenderer {
  51. function __construct($section) {
  52. parent::__construct($section);
  53. }
  54. function getFormat() {
  55. return '<div>%s</div> <div>%s</div>';
  56. }
  57. function getContentArray() {
  58. $args = array(
  59. $this->section->week->title,
  60. $this->section->week->number
  61. );
  62. return $args;
  63. }
  64. function getSectionTitle() {
  65. return 'Week';
  66. }
  67. }
  68. class FeatureRenderer extends BaseRenderer {
  69. function __construct($section) {
  70. parent::__construct($section);
  71. }
  72. function getContentArray() {
  73. return array( $this->section->feature->title);
  74. }
  75. function getFormat() {
  76. return '<div>%s</div> ';
  77. }
  78. function getSectionTitle() {
  79. return 'Feature';
  80. }
  81. }
  82. class AirdateRenderer extends BaseRenderer {
  83. function __construct($section) {
  84. parent::__construct($section);
  85. }
  86. function getContentArray() {
  87. return array(
  88. $this->section->airdate->month,
  89. $this->section->airdate->date,
  90. $this->section->airdate->year
  91. );
  92. }
  93. function getFormat() {
  94. return '<div>%s %s, %s</div> ';
  95. }
  96. function getSectionTitle() {
  97. return 'Airdate';
  98. }
  99. }
  100. class TeaseRenderer extends BaseRenderer {
  101. function __construct($section) {
  102. parent::__construct($section);
  103. }
  104. function getFormat() {
  105. return '<div>%s </div> ';
  106. }
  107. function getContentArray() {
  108. return array($this->findTease($this->section->tease->text));
  109. }
  110. function getSectionTitle() {
  111. return 'Tease';
  112. }
  113. private function findTease($line) {
  114. $teaser = trim($line);
  115. // remove the last period, and any other periods at the end of the string
  116. // because later we're going to look for a period in the middle of the string
  117. // and we don't want to find the last one by mistake.
  118. while( substr($teaser, -1 ) == '.' ) {
  119. $teaser = substr($teaser, 0, -1);
  120. $teaser = trim($teaser);
  121. }
  122. if( FALSE !== ($pos = strpos($teaser, '?')) ) {
  123. // first sentence ends in a question mark, cut it there.
  124. return substr($teaser, 0, $pos) . '?';
  125. } elseif ( preg_match('/([,])[^,]*([Pp]arent\s+[Rr]eport).*?$/', $teaser, $m) ) {
  126. $punct = $m[1];
  127. ',' == $punct && $punct = '.';
  128. return substr(trim($teaser), 0, - (strlen($m[0]))) . $punct;
  129. } elseif( FALSE !== ($pos = strpos(substr($teaser, 0, -1), '.'))) {
  130. return substr($teaser, 0, $pos) . '.';
  131. }
  132. return $line;
  133. }
  134. }
  135. class IntroRenderer extends BaseRenderer {
  136. function __construct($section) {
  137. parent::__construct($section);
  138. }
  139. function getFormat() {
  140. return '<div>%s </div> ';
  141. }
  142. function getContentArray() {
  143. return array( $this->findIntro($this->section->intro->text));
  144. }
  145. function getSectionTitle() {
  146. return 'Intro';
  147. }
  148. function findIntro($line) {
  149. // lose the first two sentences. They are usually
  150. // "Hi. I'm Joanne Wilson with The Parent Report."
  151. $intro = $line;
  152. if( ! preg_match(
  153. '/^([^.]+\.)([^.]+\.)\s+(.*)$/i',
  154. $line,
  155. $matches
  156. ) ) {
  157. return $intro;
  158. }
  159. // a few sanity checks.
  160. if( trim($matches[1]) != 'Hi.' ) return $intro;
  161. if( ! strstr($matches[2], 'Joanne' ) ) return $intro;
  162. if( ! strstr($matches[2], 'Wilson' ) ) return $intro;
  163. return $matches[3];
  164. }
  165. }
  166. class BridgeRenderer extends BaseRenderer {
  167. function __construct($section) {
  168. parent::__construct($section);
  169. }
  170. function getFormat() {
  171. return '<div>%s </div> ';
  172. }
  173. function getContentArray() {
  174. return array( $this->section->bridge->text);
  175. }
  176. function getSectionTitle() {
  177. return 'Bridge';
  178. }
  179. }
  180. class ClipRenderer extends BaseRenderer {
  181. function __construct($section) {
  182. parent::__construct($section);
  183. }
  184. function getFormat() {
  185. return '<div>%s </div> ';
  186. }
  187. function getContentArray() {
  188. return array( $this->section->clip->text);
  189. }
  190. function getSectionTitle() {
  191. return 'Clip';
  192. }
  193. }
  194. class WrapRenderer extends BaseRenderer {
  195. function __construct($section) {
  196. parent::__construct($section);
  197. }
  198. function getFormat() {
  199. return '<div>%s </div> ';
  200. }
  201. function getContentArray() {
  202. return array( $this->section->wrap->text);
  203. }
  204. function getSectionTitle() {
  205. return 'Wrap';
  206. }
  207. }
  208. class AgesRenderer extends BaseRenderer {
  209. function __construct($section) {
  210. parent::__construct($section);
  211. }
  212. function getFormat() {
  213. return '<div>%s </div> ';
  214. }
  215. function getContentArray() {
  216. return array( join(', ', $this->section->ages->items));
  217. }
  218. function getSectionTitle() {
  219. return 'Ages';
  220. }
  221. }
  222. class CategoriesRenderer extends BaseRenderer {
  223. function __construct($section) {
  224. parent::__construct($section);
  225. }
  226. function getFormat() {
  227. return '<div>%s </div> ';
  228. }
  229. function getContentArray() {
  230. return array( join(', ', $this->section->categories->items));
  231. }
  232. function getSectionTitle() {
  233. return 'Categories';
  234. }
  235. }
  236. class ErrorRenderer extends BaseRenderer {
  237. function __construct($section) {
  238. parent::__construct($section);
  239. }
  240. function getFormat() {
  241. if( ! @$this->section->lineno ) {
  242. return '<div>%s</div>';
  243. }
  244. return '<div>%s on line: %s</div> ';
  245. }
  246. function getContentArray() {
  247. $error = array();
  248. array_push($error, $this->section->error);
  249. if( @$this->section->lineno ) {
  250. array_push($error, $this->section->lineno);
  251. }
  252. return $error;
  253. }
  254. function getSectionTitle() {
  255. return 'Error';
  256. }
  257. }
  258. function tprsp_categoriesLookupHash() {
  259. return array(
  260. 'B' => 'Behaviour',
  261. 'D' => 'Development',
  262. 'SF' =>'Safety',
  263. 'E' => 'Education',
  264. 'N' => 'Nutrition',
  265. 'F' => 'Family Life',
  266. 'H' => 'Health',
  267. 'LS' => 'Limit Setting',
  268. 'KC' => 'Kids Culture',
  269. 'SL' => 'Sleep',
  270. 'F' => 'Family',
  271. );
  272. }
  273. function tprsp_agesLookupHash() {
  274. return array(
  275. 'N' => 'Newborn',
  276. 'I' => 'Infant',
  277. 'IT' => 'Toddler',
  278. 'PS' => 'Preschool',
  279. 'ES' => 'Early School',
  280. 'PT' => 'Preteen',
  281. 'T' => 'Teen',
  282. );
  283. }