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