writetothem /phplib/cobrand.php

Language PHP Lines 471
MD5 Hash adcdfc28c7bd352cdfa87ff7c87a86e7
Repository https://github.com/mysociety/writetothem.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?php
/*
 * Cobranding.
 * 
 * Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
 * Email: francis@mysociety.org; WWW: http://www.mysociety.org
 *
 * $Id: cobrand.php,v 1.28 2009-11-05 11:19:22 louise Exp $
 * 
 */

$handles = array();

// To change look and feel, make new files in templates/. Look at cheltenham
// for examples. Also need to make file like web/cobrands/cheltenham/cheltenham.css. To change the 
// behaviour of a cobranded site, add a function to phplib/cobrands/[cobrand name]/utils.php
// and add a hook function with default behaviour here. 

// Include any cobrand-specific code
function cobrand_handle($cobrand){
  global $handles;
  $dir = dirname(__FILE__);
  if (array_key_exists($cobrand, $handles)){
    return $handles[$cobrand];
  }
  if (file_exists("$dir/cobrands/$cobrand/utils.php")){
      include_once("$dir/cobrands/$cobrand/utils.php");
      $classname = ucwords($cobrand);
      $handle = call_user_func( array($classname, 'factory'));      
      $handles[$cobrand] = $handle;
  }else{
      $handles[$cobrand] = null;
  }
  return $handles[$cobrand];
}

// Return the message that asks the user to enter their postcode
function cobrand_enter_postcode_message($cobrand, $cocode) {
    if (!$cobrand) {
        return false;
    } 
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'enter_postcode_message')){
        return $cobrand_handle->enter_postcode_message($cocode);
    }
    return false;

} 

//Return the message for when an empty postcode is entered
function cobrand_empty_postcode_message($cobrand, $cocode) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'empty_postcode_message')){
            return $cobrand_handle->empty_postcode_message($cocode);
        }
    }
    return false;
}

// Return the message for when a bad postcode is entered
function cobrand_bad_postcode_message($cobrand, $cocode) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'bad_postcode_message')){
            return $cobrand_handle->bad_postcode_message($cocode);
        }
    }
    return false;
}

// Return the message for when a postcode can't be found
function cobrand_postcode_not_found_message($cobrand, $cocode) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'postcode_not_found_message')){
            return $cobrand_handle->postcode_not_found_message($cocode);
        }
    }
    return false;
}

// Return the message to be used for bad contact information
function cobrand_bad_contact_error_msg($cobrand, $eb_area_info) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'bad_contact_error_msg')){
            return $cobrand_handle->bad_contact_error_msg($eb_area_info);
        }
    }
    return false;
} 

// Return the message to be used for people who don't want to be contacted
// through the service
function cobrand_shame_error_msg($cobrand, $fyr_voting_area, $fyr_representative) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'shame_error_msg')){
            return $cobrand_handle->shame_error_msg($fyr_voting_area, $fyr_representative);
        }
    }
    return false;
}

// Return the message to be used for an unexpected error
function cobrand_generic_error_message($cobrand, $cocode, $page_name) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'generic_error_message')){
            return $cobrand_handle->generic_error_message($cocode, $page_name);
        }
    }
    return false;
}

// Return the short error strings to be used when errors are returned writing to multiple reps
function cobrand_message_sending_errors($cobrand) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'message_sending_errors')){
            return $cobrand_handle->message_sending_errors();
        }
    }
    return false;
}

// Return the link to write to all representatives of a given type
function cobrand_write_all_link($cobrand, $url, $rep_desc_plural, $cocode) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'write_all_link')){
            return $cobrand_handle->write_all_link($url, $rep_desc_plural, $cocode);
        }
    }
    return false;
}

// Return the text for the preview button
function cobrand_preview_button_text($cobrand) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'preview_button_text')){
            return $cobrand_handle->preview_button_text();
        }
    }
    return false;
}

// Return the text to display when a user accesses a token-based URL without a token
function cobrand_missing_token_message($cobrand) {
    if ($cobrand) {   
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'missing_token_message')){
            return $cobrand_handle->missing_token_message();
        }
    }
    return false;
}

// Return the text to display when a user accesses an answer URL without an answer
function cobrand_missing_answer_message($cobrand) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'missing_answer_message')){
            return $cobrand_handle->missing_answer_message();
        }
    }
    return false;

}

// Return the text to display when a message can't be found for a token
function cobrand_unfound_token_message($cobrand) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'unfound_token_message')){
            return $cobrand_handle->unfound_token_message();
        }
    }
    return false;

}

// Return the text for previewing your message
function cobrand_preview_text($cobrand) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'preview_text')){
            return $cobrand_handle->preview_text();
        }
    }
    return false;
}

// Return a boolean indicating whether the cocode is allowed
function cobrand_cocode_allowed($cobrand, $cocode) {
    if ($cobrand) {
         $cobrand_handle = cobrand_handle($cobrand);
         if ($cobrand_handle && method_exists($cobrand_handle, 'cocode_allowed')){
             return $cobrand_handle->cocode_allowed($cocode);
         } 
    }
    return true;
}

// Return the column blurb for a set of representatives
function cobrand_col_blurb($cobrand, $va_type, $va_info, $eb_info, $rep_count, $rep_counts, $representatives, $va_salaried){
    if ($cobrand) {
         $cobrand_handle = cobrand_handle($cobrand);
         if ($cobrand_handle && method_exists($cobrand_handle, 'col_blurb')){
             return $cobrand_handle->col_blurb($va_type, $va_info, $eb_info, $rep_count, $rep_counts, $representatives, $va_salaried);
         }
    }
    return false;

}

// Message to show when there's an election
function cobrand_election_error_message($cobrand) {
    if ($cobrand) {
         $cobrand_handle = cobrand_handle($cobrand);
         if ($cobrand_handle && method_exists($cobrand_handle, 'election_error_message')){
             return $cobrand_handle->election_error_message();
         }
    }
    return false;
}

// Message to show when the user enters an invalid email address
function cobrand_invalid_email_message($cobrand) {
    if ($cobrand) {
         $cobrand_handle = cobrand_handle($cobrand);
         if ($cobrand_handle && method_exists($cobrand_handle, 'invalid_email_message')){
             return $cobrand_handle->invalid_email_message();
         }
    }
    return false;
}

// Message for mismatched email addresses
function cobrand_mismatched_emails_message($cobrand) {
    if ($cobrand) {
         $cobrand_handle = cobrand_handle($cobrand);
         if ($cobrand_handle && method_exists($cobrand_handle, 'mismatched_emails_message')){
             return $cobrand_handle->mismatched_emails_message();
         }
    }
    return false;

}

// Bullet points / tips to put at the top of a letter.
function cobrand_get_letter_help($cobrand, $fyr_values) {
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'get_letter_help')){
        return $cobrand_handle->get_letter_help($fyr_values);
    }
    return false;
}

// Title for the (1-based) step of the process
function cobrand_step_title($cobrand, $step) {
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'step_title')){
        return $cobrand_handle->step_title($step);
    }
    return '';
}

// Action to perform after the letter has been sent.
// Return true if you've rendered a template so another doesn't need rendering. 
// Exit if you've done a redirect with header().
// Return false for default behaviour.
function cobrand_post_letter_send($values) {
    $cobrand = $values['cobrand']; 
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'post_letter_send')){
        return $cobrand_handle->post_letter_send($values);
    }
    return false;
}

// Action to perform if someone enters a special (mostly fictional) postcode
// Return true if you've rendered a template so another template doesn't need rendering
// Exit if you've done a redirect with header()
// Return false for default behaviour
function cobrand_special_postcodes($cobrand, $cocode, $pc, $a) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'special_postcodes')){
             return $cobrand_handle->special_postcodes($cocode, $pc, $a);
        }

    }
    return false; 
}

// Action to perform if someone enters one of the outside UK special postcodes
// Return true if you've rendered a template so another template doesn't need rendering
// Exit if you've done a redirect with header()
// Return false for default behaviour
function cobrand_other_postcodes($cobrand, $cocode, $pc, $a) {
    if ($cobrand) {
        $cobrand_handle = cobrand_handle($cobrand);
        if ($cobrand_handle && method_exists($cobrand_handle, 'other_postcodes')){
             return $cobrand_handle->other_postcodes($cocode, $pc, $a);
        }

    }
    return false; 
} 

// On front page, force a particular campaign code as default for site (either
// forced value, or if another code isn't set).
function cobrand_force_default_cocode($cobrand, $cocode) {
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'force_default_cocode')){
        return $cobrand_handle->force_default_cocode($cocode);
    }
    return $cocode;
}

// Force setting the representative type or types to a certain type, mainly for
// when they land on the front page. Can set per cobrand or cocode.
// $type, and your return value, are the options as described for the 'a'
// parameter in https://www.writetothem.com/about-linktous
// $type input is the value set with the 'a' URL parameter, if there is one.
function cobrand_force_representative_type($cobrand, $cocode, $type) {
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'force_representative_type')){
        return $cobrand_handle->force_representative_type($cocode, $type);
    }
    return $type;
}

// Perform any additional checks on the voting areas returned
// for a postcode.
// Return true if you've rendered a template so another doesn't need rendering.
// Exit if you've done a redirect with header().
function cobrand_check_areas($cobrand, $cocode, $voting_areas, $pc, $a) {
    if (! $cobrand) {
         return false;
    }
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'check_areas')){
        return $cobrand_handle->check_areas($cocode, $voting_areas, $pc, $a);
    }
    return false;
}

// Return any HTML headers to be used in the cobranded site
function cobrand_headers($cobrand, $template) {
  if ($cobrand) {
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'headers')) {
      return $cobrand_handle->headers($template);
    }
    return '';
  }
}

// Return a boolean indicating whether the survey should be displayed
function cobrand_display_survey($cobrand) {
  if ($cobrand) {
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'display_survey')) {
      return $cobrand_handle->display_survey();
    }
  }
  return true;
}

// Return a boolean indicating whether the link allowing users to submit
// councillor corrections should be displayed
function cobrand_display_councillor_correction_link($cobrand) {
  if ($cobrand) {
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'display_councillor_correction_link')) {
      return $cobrand_handle->display_councillor_correction_link();
    }
  }
  return true;
}

// Generate a url for writing to all reps in the main app
function cobrand_main_write_url($cobrand, $fyr_postcode, $cocode, $fyr_extref) {
  if ($cobrand) {
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'main_write_url')) {
      return $cobrand_handle->main_write_url($fyr_postcode, $cocode, $fyr_extref);
    }
  }
  return '';
}

// Give cobrands a hook to rewrite URLs
function cobrand_url($cobrand, $url, $cocode) {
  if ($cobrand) {
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'url')) {
      return $cobrand_handle->url($url, $cocode);
    }
  }
  return $url;
}

// Return the rendering options for the postcode entry form
function cobrand_postcode_form_options($cobrand) {
  if ($cobrand){
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'postcode_form_options')){
        return $cobrand_handle->postcode_form_options();
    }
  }
  return array(
               'inner_div'   => true, 
               'extra_space' => true, 
               'bold_labels' => true, 
               'show_errors' => false);
}

// Return the rendering options for lists of representatives
function cobrand_rep_list_options($cobrand) { 
  if ($cobrand){
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'rep_list_options')){
        return $cobrand_handle->rep_list_options();
    }
  }
  return array('include_write_all' => false);
}

// Return the rendering options for message-writing form
function cobrand_write_form_options($cobrand) {
  if ($cobrand){
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'write_form_options')){
        return $cobrand_handle->write_form_options();
    }
  }
  return array('table_layout'         => true, 
               'include_write_header' => true,
               'include_fao'          => false,  
               'renderer'             => new HTML_QuickForm_Renderer_mySociety());
}

// Return the rendering options for the message preview form
function cobrand_preview_form_options($cobrand) {
  if ($cobrand){
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'preview_form_options')){
        return $cobrand_handle->preview_form_options();
    }
  }
  return array('inner_div' => true);

}

// Return the HTML for a cobrand page
function cobrand_page($cobrand, $page) {
  if ($cobrand){
    $cobrand_handle = cobrand_handle($cobrand);
    if ($cobrand_handle && method_exists($cobrand_handle, 'page')){
        return $cobrand_handle->page($page);
    }
  }
 return '';

}
Back to Top