PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/phplib/cobrand.php

https://github.com/mysociety/writetothem
PHP | 470 lines | 351 code | 49 blank | 70 comment | 108 complexity | adcdfc28c7bd352cdfa87ff7c87a86e7 MD5 | raw file
  1. <?php
  2. /*
  3. * Cobranding.
  4. *
  5. * Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
  6. * Email: francis@mysociety.org; WWW: http://www.mysociety.org
  7. *
  8. * $Id: cobrand.php,v 1.28 2009-11-05 11:19:22 louise Exp $
  9. *
  10. */
  11. $handles = array();
  12. // To change look and feel, make new files in templates/. Look at cheltenham
  13. // for examples. Also need to make file like web/cobrands/cheltenham/cheltenham.css. To change the
  14. // behaviour of a cobranded site, add a function to phplib/cobrands/[cobrand name]/utils.php
  15. // and add a hook function with default behaviour here.
  16. // Include any cobrand-specific code
  17. function cobrand_handle($cobrand){
  18. global $handles;
  19. $dir = dirname(__FILE__);
  20. if (array_key_exists($cobrand, $handles)){
  21. return $handles[$cobrand];
  22. }
  23. if (file_exists("$dir/cobrands/$cobrand/utils.php")){
  24. include_once("$dir/cobrands/$cobrand/utils.php");
  25. $classname = ucwords($cobrand);
  26. $handle = call_user_func( array($classname, 'factory'));
  27. $handles[$cobrand] = $handle;
  28. }else{
  29. $handles[$cobrand] = null;
  30. }
  31. return $handles[$cobrand];
  32. }
  33. // Return the message that asks the user to enter their postcode
  34. function cobrand_enter_postcode_message($cobrand, $cocode) {
  35. if (!$cobrand) {
  36. return false;
  37. }
  38. $cobrand_handle = cobrand_handle($cobrand);
  39. if ($cobrand_handle && method_exists($cobrand_handle, 'enter_postcode_message')){
  40. return $cobrand_handle->enter_postcode_message($cocode);
  41. }
  42. return false;
  43. }
  44. //Return the message for when an empty postcode is entered
  45. function cobrand_empty_postcode_message($cobrand, $cocode) {
  46. if ($cobrand) {
  47. $cobrand_handle = cobrand_handle($cobrand);
  48. if ($cobrand_handle && method_exists($cobrand_handle, 'empty_postcode_message')){
  49. return $cobrand_handle->empty_postcode_message($cocode);
  50. }
  51. }
  52. return false;
  53. }
  54. // Return the message for when a bad postcode is entered
  55. function cobrand_bad_postcode_message($cobrand, $cocode) {
  56. if ($cobrand) {
  57. $cobrand_handle = cobrand_handle($cobrand);
  58. if ($cobrand_handle && method_exists($cobrand_handle, 'bad_postcode_message')){
  59. return $cobrand_handle->bad_postcode_message($cocode);
  60. }
  61. }
  62. return false;
  63. }
  64. // Return the message for when a postcode can't be found
  65. function cobrand_postcode_not_found_message($cobrand, $cocode) {
  66. if ($cobrand) {
  67. $cobrand_handle = cobrand_handle($cobrand);
  68. if ($cobrand_handle && method_exists($cobrand_handle, 'postcode_not_found_message')){
  69. return $cobrand_handle->postcode_not_found_message($cocode);
  70. }
  71. }
  72. return false;
  73. }
  74. // Return the message to be used for bad contact information
  75. function cobrand_bad_contact_error_msg($cobrand, $eb_area_info) {
  76. if ($cobrand) {
  77. $cobrand_handle = cobrand_handle($cobrand);
  78. if ($cobrand_handle && method_exists($cobrand_handle, 'bad_contact_error_msg')){
  79. return $cobrand_handle->bad_contact_error_msg($eb_area_info);
  80. }
  81. }
  82. return false;
  83. }
  84. // Return the message to be used for people who don't want to be contacted
  85. // through the service
  86. function cobrand_shame_error_msg($cobrand, $fyr_voting_area, $fyr_representative) {
  87. if ($cobrand) {
  88. $cobrand_handle = cobrand_handle($cobrand);
  89. if ($cobrand_handle && method_exists($cobrand_handle, 'shame_error_msg')){
  90. return $cobrand_handle->shame_error_msg($fyr_voting_area, $fyr_representative);
  91. }
  92. }
  93. return false;
  94. }
  95. // Return the message to be used for an unexpected error
  96. function cobrand_generic_error_message($cobrand, $cocode, $page_name) {
  97. if ($cobrand) {
  98. $cobrand_handle = cobrand_handle($cobrand);
  99. if ($cobrand_handle && method_exists($cobrand_handle, 'generic_error_message')){
  100. return $cobrand_handle->generic_error_message($cocode, $page_name);
  101. }
  102. }
  103. return false;
  104. }
  105. // Return the short error strings to be used when errors are returned writing to multiple reps
  106. function cobrand_message_sending_errors($cobrand) {
  107. if ($cobrand) {
  108. $cobrand_handle = cobrand_handle($cobrand);
  109. if ($cobrand_handle && method_exists($cobrand_handle, 'message_sending_errors')){
  110. return $cobrand_handle->message_sending_errors();
  111. }
  112. }
  113. return false;
  114. }
  115. // Return the link to write to all representatives of a given type
  116. function cobrand_write_all_link($cobrand, $url, $rep_desc_plural, $cocode) {
  117. if ($cobrand) {
  118. $cobrand_handle = cobrand_handle($cobrand);
  119. if ($cobrand_handle && method_exists($cobrand_handle, 'write_all_link')){
  120. return $cobrand_handle->write_all_link($url, $rep_desc_plural, $cocode);
  121. }
  122. }
  123. return false;
  124. }
  125. // Return the text for the preview button
  126. function cobrand_preview_button_text($cobrand) {
  127. if ($cobrand) {
  128. $cobrand_handle = cobrand_handle($cobrand);
  129. if ($cobrand_handle && method_exists($cobrand_handle, 'preview_button_text')){
  130. return $cobrand_handle->preview_button_text();
  131. }
  132. }
  133. return false;
  134. }
  135. // Return the text to display when a user accesses a token-based URL without a token
  136. function cobrand_missing_token_message($cobrand) {
  137. if ($cobrand) {
  138. $cobrand_handle = cobrand_handle($cobrand);
  139. if ($cobrand_handle && method_exists($cobrand_handle, 'missing_token_message')){
  140. return $cobrand_handle->missing_token_message();
  141. }
  142. }
  143. return false;
  144. }
  145. // Return the text to display when a user accesses an answer URL without an answer
  146. function cobrand_missing_answer_message($cobrand) {
  147. if ($cobrand) {
  148. $cobrand_handle = cobrand_handle($cobrand);
  149. if ($cobrand_handle && method_exists($cobrand_handle, 'missing_answer_message')){
  150. return $cobrand_handle->missing_answer_message();
  151. }
  152. }
  153. return false;
  154. }
  155. // Return the text to display when a message can't be found for a token
  156. function cobrand_unfound_token_message($cobrand) {
  157. if ($cobrand) {
  158. $cobrand_handle = cobrand_handle($cobrand);
  159. if ($cobrand_handle && method_exists($cobrand_handle, 'unfound_token_message')){
  160. return $cobrand_handle->unfound_token_message();
  161. }
  162. }
  163. return false;
  164. }
  165. // Return the text for previewing your message
  166. function cobrand_preview_text($cobrand) {
  167. if ($cobrand) {
  168. $cobrand_handle = cobrand_handle($cobrand);
  169. if ($cobrand_handle && method_exists($cobrand_handle, 'preview_text')){
  170. return $cobrand_handle->preview_text();
  171. }
  172. }
  173. return false;
  174. }
  175. // Return a boolean indicating whether the cocode is allowed
  176. function cobrand_cocode_allowed($cobrand, $cocode) {
  177. if ($cobrand) {
  178. $cobrand_handle = cobrand_handle($cobrand);
  179. if ($cobrand_handle && method_exists($cobrand_handle, 'cocode_allowed')){
  180. return $cobrand_handle->cocode_allowed($cocode);
  181. }
  182. }
  183. return true;
  184. }
  185. // Return the column blurb for a set of representatives
  186. function cobrand_col_blurb($cobrand, $va_type, $va_info, $eb_info, $rep_count, $rep_counts, $representatives, $va_salaried){
  187. if ($cobrand) {
  188. $cobrand_handle = cobrand_handle($cobrand);
  189. if ($cobrand_handle && method_exists($cobrand_handle, 'col_blurb')){
  190. return $cobrand_handle->col_blurb($va_type, $va_info, $eb_info, $rep_count, $rep_counts, $representatives, $va_salaried);
  191. }
  192. }
  193. return false;
  194. }
  195. // Message to show when there's an election
  196. function cobrand_election_error_message($cobrand) {
  197. if ($cobrand) {
  198. $cobrand_handle = cobrand_handle($cobrand);
  199. if ($cobrand_handle && method_exists($cobrand_handle, 'election_error_message')){
  200. return $cobrand_handle->election_error_message();
  201. }
  202. }
  203. return false;
  204. }
  205. // Message to show when the user enters an invalid email address
  206. function cobrand_invalid_email_message($cobrand) {
  207. if ($cobrand) {
  208. $cobrand_handle = cobrand_handle($cobrand);
  209. if ($cobrand_handle && method_exists($cobrand_handle, 'invalid_email_message')){
  210. return $cobrand_handle->invalid_email_message();
  211. }
  212. }
  213. return false;
  214. }
  215. // Message for mismatched email addresses
  216. function cobrand_mismatched_emails_message($cobrand) {
  217. if ($cobrand) {
  218. $cobrand_handle = cobrand_handle($cobrand);
  219. if ($cobrand_handle && method_exists($cobrand_handle, 'mismatched_emails_message')){
  220. return $cobrand_handle->mismatched_emails_message();
  221. }
  222. }
  223. return false;
  224. }
  225. // Bullet points / tips to put at the top of a letter.
  226. function cobrand_get_letter_help($cobrand, $fyr_values) {
  227. $cobrand_handle = cobrand_handle($cobrand);
  228. if ($cobrand_handle && method_exists($cobrand_handle, 'get_letter_help')){
  229. return $cobrand_handle->get_letter_help($fyr_values);
  230. }
  231. return false;
  232. }
  233. // Title for the (1-based) step of the process
  234. function cobrand_step_title($cobrand, $step) {
  235. $cobrand_handle = cobrand_handle($cobrand);
  236. if ($cobrand_handle && method_exists($cobrand_handle, 'step_title')){
  237. return $cobrand_handle->step_title($step);
  238. }
  239. return '';
  240. }
  241. // Action to perform after the letter has been sent.
  242. // Return true if you've rendered a template so another doesn't need rendering.
  243. // Exit if you've done a redirect with header().
  244. // Return false for default behaviour.
  245. function cobrand_post_letter_send($values) {
  246. $cobrand = $values['cobrand'];
  247. $cobrand_handle = cobrand_handle($cobrand);
  248. if ($cobrand_handle && method_exists($cobrand_handle, 'post_letter_send')){
  249. return $cobrand_handle->post_letter_send($values);
  250. }
  251. return false;
  252. }
  253. // Action to perform if someone enters a special (mostly fictional) postcode
  254. // Return true if you've rendered a template so another template doesn't need rendering
  255. // Exit if you've done a redirect with header()
  256. // Return false for default behaviour
  257. function cobrand_special_postcodes($cobrand, $cocode, $pc, $a) {
  258. if ($cobrand) {
  259. $cobrand_handle = cobrand_handle($cobrand);
  260. if ($cobrand_handle && method_exists($cobrand_handle, 'special_postcodes')){
  261. return $cobrand_handle->special_postcodes($cocode, $pc, $a);
  262. }
  263. }
  264. return false;
  265. }
  266. // Action to perform if someone enters one of the outside UK special postcodes
  267. // Return true if you've rendered a template so another template doesn't need rendering
  268. // Exit if you've done a redirect with header()
  269. // Return false for default behaviour
  270. function cobrand_other_postcodes($cobrand, $cocode, $pc, $a) {
  271. if ($cobrand) {
  272. $cobrand_handle = cobrand_handle($cobrand);
  273. if ($cobrand_handle && method_exists($cobrand_handle, 'other_postcodes')){
  274. return $cobrand_handle->other_postcodes($cocode, $pc, $a);
  275. }
  276. }
  277. return false;
  278. }
  279. // On front page, force a particular campaign code as default for site (either
  280. // forced value, or if another code isn't set).
  281. function cobrand_force_default_cocode($cobrand, $cocode) {
  282. $cobrand_handle = cobrand_handle($cobrand);
  283. if ($cobrand_handle && method_exists($cobrand_handle, 'force_default_cocode')){
  284. return $cobrand_handle->force_default_cocode($cocode);
  285. }
  286. return $cocode;
  287. }
  288. // Force setting the representative type or types to a certain type, mainly for
  289. // when they land on the front page. Can set per cobrand or cocode.
  290. // $type, and your return value, are the options as described for the 'a'
  291. // parameter in https://www.writetothem.com/about-linktous
  292. // $type input is the value set with the 'a' URL parameter, if there is one.
  293. function cobrand_force_representative_type($cobrand, $cocode, $type) {
  294. $cobrand_handle = cobrand_handle($cobrand);
  295. if ($cobrand_handle && method_exists($cobrand_handle, 'force_representative_type')){
  296. return $cobrand_handle->force_representative_type($cocode, $type);
  297. }
  298. return $type;
  299. }
  300. // Perform any additional checks on the voting areas returned
  301. // for a postcode.
  302. // Return true if you've rendered a template so another doesn't need rendering.
  303. // Exit if you've done a redirect with header().
  304. function cobrand_check_areas($cobrand, $cocode, $voting_areas, $pc, $a) {
  305. if (! $cobrand) {
  306. return false;
  307. }
  308. $cobrand_handle = cobrand_handle($cobrand);
  309. if ($cobrand_handle && method_exists($cobrand_handle, 'check_areas')){
  310. return $cobrand_handle->check_areas($cocode, $voting_areas, $pc, $a);
  311. }
  312. return false;
  313. }
  314. // Return any HTML headers to be used in the cobranded site
  315. function cobrand_headers($cobrand, $template) {
  316. if ($cobrand) {
  317. $cobrand_handle = cobrand_handle($cobrand);
  318. if ($cobrand_handle && method_exists($cobrand_handle, 'headers')) {
  319. return $cobrand_handle->headers($template);
  320. }
  321. return '';
  322. }
  323. }
  324. // Return a boolean indicating whether the survey should be displayed
  325. function cobrand_display_survey($cobrand) {
  326. if ($cobrand) {
  327. $cobrand_handle = cobrand_handle($cobrand);
  328. if ($cobrand_handle && method_exists($cobrand_handle, 'display_survey')) {
  329. return $cobrand_handle->display_survey();
  330. }
  331. }
  332. return true;
  333. }
  334. // Return a boolean indicating whether the link allowing users to submit
  335. // councillor corrections should be displayed
  336. function cobrand_display_councillor_correction_link($cobrand) {
  337. if ($cobrand) {
  338. $cobrand_handle = cobrand_handle($cobrand);
  339. if ($cobrand_handle && method_exists($cobrand_handle, 'display_councillor_correction_link')) {
  340. return $cobrand_handle->display_councillor_correction_link();
  341. }
  342. }
  343. return true;
  344. }
  345. // Generate a url for writing to all reps in the main app
  346. function cobrand_main_write_url($cobrand, $fyr_postcode, $cocode, $fyr_extref) {
  347. if ($cobrand) {
  348. $cobrand_handle = cobrand_handle($cobrand);
  349. if ($cobrand_handle && method_exists($cobrand_handle, 'main_write_url')) {
  350. return $cobrand_handle->main_write_url($fyr_postcode, $cocode, $fyr_extref);
  351. }
  352. }
  353. return '';
  354. }
  355. // Give cobrands a hook to rewrite URLs
  356. function cobrand_url($cobrand, $url, $cocode) {
  357. if ($cobrand) {
  358. $cobrand_handle = cobrand_handle($cobrand);
  359. if ($cobrand_handle && method_exists($cobrand_handle, 'url')) {
  360. return $cobrand_handle->url($url, $cocode);
  361. }
  362. }
  363. return $url;
  364. }
  365. // Return the rendering options for the postcode entry form
  366. function cobrand_postcode_form_options($cobrand) {
  367. if ($cobrand){
  368. $cobrand_handle = cobrand_handle($cobrand);
  369. if ($cobrand_handle && method_exists($cobrand_handle, 'postcode_form_options')){
  370. return $cobrand_handle->postcode_form_options();
  371. }
  372. }
  373. return array(
  374. 'inner_div' => true,
  375. 'extra_space' => true,
  376. 'bold_labels' => true,
  377. 'show_errors' => false);
  378. }
  379. // Return the rendering options for lists of representatives
  380. function cobrand_rep_list_options($cobrand) {
  381. if ($cobrand){
  382. $cobrand_handle = cobrand_handle($cobrand);
  383. if ($cobrand_handle && method_exists($cobrand_handle, 'rep_list_options')){
  384. return $cobrand_handle->rep_list_options();
  385. }
  386. }
  387. return array('include_write_all' => false);
  388. }
  389. // Return the rendering options for message-writing form
  390. function cobrand_write_form_options($cobrand) {
  391. if ($cobrand){
  392. $cobrand_handle = cobrand_handle($cobrand);
  393. if ($cobrand_handle && method_exists($cobrand_handle, 'write_form_options')){
  394. return $cobrand_handle->write_form_options();
  395. }
  396. }
  397. return array('table_layout' => true,
  398. 'include_write_header' => true,
  399. 'include_fao' => false,
  400. 'renderer' => new HTML_QuickForm_Renderer_mySociety());
  401. }
  402. // Return the rendering options for the message preview form
  403. function cobrand_preview_form_options($cobrand) {
  404. if ($cobrand){
  405. $cobrand_handle = cobrand_handle($cobrand);
  406. if ($cobrand_handle && method_exists($cobrand_handle, 'preview_form_options')){
  407. return $cobrand_handle->preview_form_options();
  408. }
  409. }
  410. return array('inner_div' => true);
  411. }
  412. // Return the HTML for a cobrand page
  413. function cobrand_page($cobrand, $page) {
  414. if ($cobrand){
  415. $cobrand_handle = cobrand_handle($cobrand);
  416. if ($cobrand_handle && method_exists($cobrand_handle, 'page')){
  417. return $cobrand_handle->page($page);
  418. }
  419. }
  420. return '';
  421. }