Base class for the report and data entry helpers.

Provides several generally useful methods and also includes resource management.

package Client

 Methods

Method to link up the external css or js files associated with a set of code.

add_resource(string $resource) 
Static

This is normally called internally by the control methods to ensure the required files are linked into the page so does not need to be called directly. However it can be useful when writing custom code that uses one of these standard libraries such as jQuery. Ensures each file is only linked once.

Parameters

$resource

string

Name of resource to link. The following options are available:

  • jquery
  • openlayers
  • graticule
  • clearLayer
  • addrowtogrid
  • indiciaMapPanel
  • indiciaMapEdit
  • georeference_google_search_api
  • google_search
  • locationFinder
  • createPersonalSites
  • autocomplete
  • indicia_locks
  • jquery_cookie
  • jquery_ui
  • jquery_ui_fr
  • jquery_form
  • json
  • reportPicker
  • treeview
  • treeview_async
  • googlemaps
  • multimap
  • virtualearth
  • google_search
  • fancybox
  • flickr
  • treeBrowser
  • defaultStylesheet
  • validation
  • plupload
  • jqplot
  • jqplot_bar
  • jqplot_pie
  • jqplot_category_axis_renderer
  • jqplot_trendline
  • reportgrid
  • tabs
  • wizardprogress
  • spatialReports
  • jsonwidget
  • timeentry
  • verification

Internal method to build a control from its options array and its template.

apply_template(string $template, array $options) 
Static

Outputs the prefix template, a label (if in the options), a control, the control's errors and a suffix template.

Parameters

$template

string

Name of the control template, from the global $indicia_templates variable.

$options

array

Options array containing the control replacement values for the templates. Options can contain a setting for prefixTemplate or suffixTemplate to override the standard templates.

Takes an associative array and converts it to a list of params for a query string.

array_to_query_string(array $array, boolean $encodeValues) : string
Static

This is like http_build_query but it does not url encode the & separator, and gives control over urlencoding the array values.

Parameters

$array

array

Associative array to convert.

$encodeValues

boolean

Default false. Set to true to URL encode the values being added to the string.

Returns

stringThe query string.

Parameters forms are a quick way of specifying a simple form used to specify the input parameters for a process.

build_params_form(array $options, boolean $hasVisibleContent) 
Static

Returns the HTML required for a parameters form, e.g. the form defined for input of report parameters or the default values for a csv import.

link used for report output, id of the report instance on the page if relevant, so that controls can be given unique ids. </li> <li><b>form</b><br/> Associative array defining the form content. </li> <li><b>readAuth</b><br/> Read authorisation array. </li> <li><b>fieldNamePrefix</b><br/> Optional prefix for form field names. </li> <li><b>defaults</b><br/> Associative array of default values for each form element. </li> <li><b>paramsToHide</b><br/> An optional array of parameter names for parameters that should be added to the form output as hidden inputs rather than visible controls. <li><b>paramsToExclude</b><br/> An optional array of parameter names for parameters that should be skipped in the form output despite being in the form definition. </li> <li><b>presetParams</b><br/> Optional array of param names and values that have a fixed value and are therefore output only as a hidden control. </li> <li><b>inlineMapTools</b><br/> Defaults to false. If true, then map drawing parameter tools are embedded into the report parameters form. If false, then the map drawing tools are added to a toolbar at the top of the map. </li> <li><b>helpText</b><br/> Defaults to true. Set to false to disable helpText being displayed alongside controls, useful for building compact versions of simple parameter forms. </li> <li><b>nocache</b><br/> Set to true to disable caching of lookups. </li> </ul>

Parameters

$options

array

Options array with the following possibilities:

<

ul>

  • form
    Associative array defining the form structure. The structure is the same as described for fixed_values_form in a Warehouse model.

  • $hasVisibleContent

    boolean

    On completion, this is set to true if there are visible controls in the params form. If not, then it may be appropriate to skip the displaying of this params form since it is not necessary.

    Returns a span containing any validation errors active on the form for the control with the supplied ID.

    check_errors(string $fieldname, boolean $plaintext) 
    Static

    Parameters

    $fieldname

    string

    Fieldname of the control to retrieve errors for.

    $plaintext

    boolean

    Set to true to return just the error text, otherwise it is wrapped in a span.

    Returns the client helper folder path, relative to the root folder.

    client_helper_path() 
    Static

    This method allows JavaScript and CSS links to be created and placed in the <head> of the HTML file rather than using dump_javascript which must be called after the form is built.

    dump_header(\$resources $resources) : string
    Static

    The advantage of dump_javascript is that it intelligently builds the required links depending on what is on your form. dump_header is not intelligent because the form is not built yet, but placing links in the header leads to cleaner code which validates better.

    Parameters

    $resources

    \$resources

    List of resources to include in the header. The available options are described in the documentation for the add_resource method. The default for this is jquery_ui and defaultStylesheet.

    Returns

    stringText to place in the head section of the html file.

    Helper function to collect javascript code in a single location.

    dump_javascript() : string
    Static

    Should be called at the end of each HTML page which uses the data entry helper so output all JavaScript required by previous calls.

    link http://code.google.com/p/indicia/wiki/TutorialBuildingBasicPage#Build_a_data_entry_page

    Returns

    stringJavaScript to insert into the page for all the controls added to the page so far.

    Call the enable_validation method to turn on client-side validation for any controls with validation rules defined.

    enable_validation(string $form_id) 
    Static

    To specify validation on each control, set the control's options array to contain a 'validation' entry. This must be set to an array of validation rules in Indicia validation format. For example, 'validation' => array('required', 'email').

    Parameters

    $form_id

    string

    @form_id Id of the form the validation is being attached to.

    Explodes a value on several lines into an array split on the lines.

    explode_lines(string $value) : array
    Static

    Tolerates any line ending.

    Parameters

    $value

    string

    A multi-line string to be split.

    Returns

    arrayAn array with one entry per line in $value.

    Explodes a value with key=value several lines into an array split on the lines.

    explode_lines_key_value_pairs(string $value) : array
    Static

    Tolerates any line ending.

    Parameters

    $value

    string

    A multi-line string to be split.

    Returns

    arrayAn associative array with one entry per line in $value. Array keys are the items before the = on each line, and values are the data after the = on each line.

    Internal function to find the path to the root of the site, including the trailing slash.

    getRootFolder() 
    Static

    Retrieves a token and inserts it into a data entry form which authenticates that the form was submitted by this website.

    get_auth(string $website_id, string $password) 
    Static

    Parameters

    $website_id

    string

    Indicia ID for the website.

    $password

    string

    Indicia password for the website.

    Retrieves a read token and passes it back as an array suitable to drop into the 'extraParams' options for an Ajax call.

    get_read_auth(string $website_id, string $password) 
    Static

    Parameters

    $website_id

    string

    Indicia ID for the website.

    $password

    string

    Indicia password for the website.

    Retrieves read and write nonce tokens from the warehouse.

    get_read_write_auth(string $website_id, string $password) : \Returns
    Static

    Parameters

    $website_id

    string

    Indicia ID for the website.

    $password

    string

    Indicia password for the website.

    Returns

    \Returnsan array containing: 'read' => the read authorisation array, 'write' => the write authorisation input controls to insert into your form. 'write_tokens' => the write authorisation array, if needed as separate tokens rather than just placing in form.

    List of external resources including stylesheets and js files used by the data entry helper class.

    get_resources() 
    Static

    A utility function for building the inline script content which should be inserted into a page from the javaascript, late javascript and onload javascript.

    get_scripts(string $javascript, string $late_javascript, string $onload_javascript, bool $includeWrapper) 
    Static

    Can optionally include the script tags wrapper around the script generated.

    Parameters

    $javascript

    string

    JavaScript to run when the page is ready, i.e. in $(document).ready.

    $late_javascript

    string

    JavaScript to run at the end of $(document).ready.

    $onload_javascript

    string

    JavaScript to run in the window.onLoad handler which comes later in the page load process.

    $includeWrapper

    bool

    If true then includes script tags around the script.

    Utility function to load a list of terms from a termlist.

    get_termlist_terms(array $auth, mixed $termlist, array $filter) : array
    Static

    Parameters

    $auth

    array

    Read authorisation array.

    $termlist

    mixed

    Either the id or external_key of the termlist to load.

    $filter

    array

    List of the terms that are required, or null for all terms.

    Returns

    arrayOutput of the Warehouse data services request for the terms.

    Calculates the folder that submitted images end up in according to the helper_config.

    get_uploaded_image_folder() 
    Static

    Sends a POST using the cUrl library.

    http_post(string $url, string $postargs, boolean $output_errors) : array
    Static

    Parameters

    $url

    string

    The URL the POST request is sent to.

    $postargs

    string

    Arguments to include in the POST data.

    $output_errors

    boolean

    Set to false to prevent echoing of errors. Defaults to true.

    Returns

    arrayAn array with a result element set to true or false for successful or failed posts respectively. The output is returned in an output element in the array. If there is an error, then an errorno element gives the cUrl error number (as generated by the cUrl library used for the post).

    Applies a output template to an array.

    mergeParamsIntoTemplate(array $params, string $template, boolean $useTemplateAsIs, boolean $allowHtml, boolean $allowEscapeQuotes) : string
    Static

    This is used to build the output for each item in a list, such as a species checklist grid or a radio group.

    Parameters

    $params

    array

    Array holding the parameters to merge into the template.

    $template

    string

    Name of the template to use, or actual template text if $useTemplateAsIs is set to true.

    $useTemplateAsIs

    boolean

    If true then the template parameter contains the actual template text, otherwise it is the name of a template in the $indicia_templates array. Default false.

    $allowHtml

    boolean

    If true then HTML is emitted as is from the parameter values inserted into the template, otherwise they are escaped.

    $allowEscapeQuotes

    boolean

    If true then parameter names can be suffixes -esape-quote, -escape-dblquote, -escape-htmlquote or -escape-htmldblquote to insert backslashes or html entities into the replacements for string escaping.

    Returns

    stringHTML for the item label

    Calculates the relative path to the client_helpers folder from wherever the current PHP script is.

    relative_client_helper_path() 
    Static

    If required, setup jQuery validation.

    setup_jquery_validation_js() 
    Static

    This JavaScript must be added at the end of form preparation otherwise we would not know all the control messages. It will normally be called by dump_javascript automatically, but is exposed here as a public method since the iform Drupal module does not call dump_javascript, but is responsible for adding JavaScript to the page via drupal_add_js.

    Protected function to create a cache file provided it does not already exist.

    _cacheResponse(string $file, array $response, array $options) 
    Static

    Parameters

    $file

    string

    Cache file to be removed, includes path - will be false if no caching to take place

    $response

    array

    http_post return value

    $options

    array

    Options array : contents used to tag what this data is.

    Protected function to generate a filename to be used as the cache file for this data

    _getCacheFileName(string $path, array $options, \number $timeout) : string
    Static

    Parameters

    $path

    string

    directory path for file

    $options

    array

    Options array : contents are used along with md5 to generate the filename.

    $timeout

    \number
    • will be false if no caching to take place

    Returns

    stringfilename, else FALSE if data is not to be cached.

    Protected function to fetch a validated timeout value from passed in options array

    _getCacheTimeOut(array $options) : \Timeout
    Static

    Parameters

    $options

    array

    Options array with the following possibilities:

    • cachetimeout
      Optional. The length in seconds before the cache times out and is refetched.

    Returns

    \Timeoutin number of seconds, else FALSE if data is not to be cached.

    Protected function to return the cached data stored in the specified local file.

    _getCachedResponse(string $file, \number $timeout, array $options) : array
    Static

    Parameters

    $file

    string

    Cache file to be used, includes path

    $timeout

    \number
    • will be false if no caching to take place

    $options

    array

    Options array : contents used to confirm what this data is.

    Returns

    arrayequivalent of call to http_post, else FALSE if data is not to be cached.

    Protected function to remove a cache file if it has timed out.

    _timeOutCacheFile(string $file, \number $timeout) 
    Static

    Parameters

    $file

    string

    Cache file to be removed, includes path

    $timeout

    \number
    • will be false if no caching to take place

    Takes a template string (e.g.

    apply_replacements_to_template(string $template, string $options) 
    Static

    <

    div id="{id}">) and replaces the tokens with the equivalent values looked up from the $options array.

    Parameters

    $template

    string

    The templatable string.

    $options

    string

    The array of items which can be merged into the template.

    Returns a static template which is either a default template or one specified in the options

    apply_static_template(string $name, array $options) : string
    Static

    Parameters

    $name

    string

    The static template type. e.g. prefix or suffix.

    $options

    array

    Array of options which may contain a template name.

    Returns

    stringTemplate value.

    Converts the validation rules in an options array into a string that can be used as the control class, to trigger the jQuery validation plugin.

    build_validation_class(\$options. $options) : string
    Static

    Parameters

    $options

    \$options.

    Control options array. For validation to be applied should contain a validation entry, containing a single validation string or an array of strings.

    Returns

    stringThe validation rules formatted as a class.

    Returns templated help text for a control, but only if the position matches the $helpTextPos value, and the $options array contains a helpText entry.

    get_help_text(array $options, string $pos) : string
    Static

    Parameters

    $options

    array

    Control's options array. Can specify the class for the help text item using option helpTextClass.

    $pos

    string

    Either before or after. Defines the position that is being requested.

    Returns

    stringTemplated help text, or nothing.

    Internal implementation of the dump_javascript method which takes the javascript and resources list as flexible parameters, rather that using the globals.

    internal_dump_resources(array $resources) 
    Static
    access private

    Parameters

    $resources

    array

    List of resources to include.

    Returns a string where characters have been escaped for use in jQuery selectors

    jq_esc(string $name) : string
    Static

    Parameters

    $name

    string

    The string to be escaped.

    Returns

    stringescaped name.

    Takes a file that has been uploaded to the client website upload folder, and moves it to the warehouse upload folder using the data services.

    send_file_to_warehouse(string $path, boolean $persist_auth, array $readAuth, string $service) : string
    Static

    Parameters

    $path

    string

    Path to the file to upload, relative to the interim image path folder (normally the client_helpers/upload folder.

    $persist_auth

    boolean

    Allows the write nonce to be preserved after sending the file, useful when several files are being uploaded.

    $readAuth

    array

    readAuth Read authorisation tokens, if not supplied then the $_POST array should contain them.

    $service

    string

    Path to the service URL used. Default is data/handle_media, but could be import/upload_csv.

    Returns

    stringError message, or true if successful.

    Method to format a control error message inside a templated span.

    apply_error_template(string $error, string $fieldname) 
    Static

    Parameters

    $error

    string

    The error message.

    $fieldname

    string

    The name of the field which the error is being attached to.

    Returns a control to insert onto a parameters form.

    get_params_form_control(string $key, array $info, array $options, array $tools) : string
    Static

    Parameters

    $key

    string

    The unique identifier of this control.

    $info

    array

    Configuration options for the parameter as defined in the report, including the description, display (label), default and datatype.

    $options

    array

    Control options array

    $tools

    array

    Any tools to be embedded in the map toolbar are returned in this parameter rather than as the return result of the function.

    Returns

    stringThe HTML for the form parameter.

    Internal method to safely find the value of a preset parameter.

    get_preset_param(array $options, string $name) 
    Static

    Returns empty string if not defined.

    Parameters

    $options

    array

    The options array, containing a presetParams entry that the parameter should be found in.

    $name

    string

    The key identifying the preset parameter to look for.

     Properties

     

    <p>Number of recent files allowed in the cache which the cache will not bother clearing during a deletion operation. They will be refreshed occasionally when requested anyway.</p>

    $cache_allowed_file_count : integer
     

    <p>On average, every 1 in $cache_chance_purge times the Warehouse is called for data, all files older than 5 times the cache_timeout will be purged, apart from the most recent $cache_allowed_file_count files.</p>

    $cache_chance_purge : integer
     

    <p>On average, every 1 in $cache_chance_expire times the Warehouse is called for data which is cached but older than the cache timeout, the cached data will be refreshed. This introduces a random element to cache refreshes so that no single form load event is responsible for refreshing all cached content.</p>

    $cache_chance_refresh_file : integer
     

    <p>Length of time in seconds after which cached Warehouse responses will start to expire.</p>

    $cache_timeout : integer
     

    <p>Path to Indicia CSS folder. If not specified, then it is calculated from the Warehouse $base_url. This path should be a full path on the server (starting with '/' exluding the domain).</p>

    $css_path : string
     

    <p>of default validation rules to apply to the controls on the form if the built in client side validation is used (with the jQuery validation plugin). This array can be replaced if required.</p>

    $default_validation_rules : Array
    todo This array could be auto-populated with validation rules for a survey's fields from the Warehouse.
     

    <p>List of resources that have already been dumped out, so we don't duplicate them. For example, if the site template includes JQuery set $dumped_resources[]='jquery'.</p>

    $dumped_resources : array
     

    <p>Form Mode. Initially unset indicating new input, but can be set to ERRORS or RELOAD.</p>

    $form_mode : string
     

    <p>Helptext positioning. Determines where the information is displayed when helpText is defined for a control. Options are before, after.</p>

    $helpTextPos : string
     

    <p>Path to Indicia Images folder.</p>

    $images_path : string
     

    <p>Flag set to true if returning content for an AJAX request. This allows the javascript to be returned direct rather than embedding in document.ready and window.onload handlers.</p>

    $is_ajax : boolean
     

    <p>JavaScript text to be emitted after the data entry form. Each control that needs custom JavaScript can append the script to this variable.</p>

    $javascript : string
     

    <p>Path to Indicia JavaScript folder. If not specified, then it is calculated from the Warehouse $base_url. This path should be a full path on the server (starting with '/' exluding the domain).</p>

    $js_path : string
     

    <p>JavaScript text to be emitted after the data entry form and all other JavaScript.</p>

    $late_javascript : string
     

    <p>Setting to completely disable loading from the cache</p>

    $nocache : boolean
     

    <p>JavaScript text to be emitted during window.onload.</p>

    $onload_javascript : string
     

    <p>List of resources that have been identified as required by the controls used. This defines the JavaScript and stylesheets that must be added to the page. Each entry is an array containing stylesheets and javascript sub-arrays. This has public access so the Drupal module can perform Drupal specific resource output.</p>

    $required_resources : Array
     

    <p>List of all available resources known. Each resource is named, and contains a sub array of deps (dependencies), stylesheets and javascripts.</p>

    $resource_list : Array
     

    <p>Name of the form which has been set up for jQuery validation, if any.</p>

    $validated_form_id : array
     

    <p>List of all error messages returned from an attempt to save.</p>

    $validation_errors : array
     

    <p>List of messages defined to pass to the validation plugin.</p>

    $validation_messages : array
     

    List of methods used to report a validation failure.

    $validation_mode : array

    Options are message, message, hint, icon, colour, inline. The inline option specifies that the message should appear on the same line as the control. Otherwise it goes on the next line, indented by the label width. Because in many cases, controls on an Indicia form occupy the full available width, it is often more appropriate to place error messages on the next line so this is the default behaviour.

     

    <p>Path to proxy script for calls to the warehouse (optional, allows the warehouse to sit behind a firewall only accessible from the server).</p>

    $warehouse_proxy : string
     

    <p>Are we linking in the default stylesheet? Handled sligtly different to the others so it can be added to the end of the list, allowing our CSS to override other stuff.</p>

    $default_styles : Boolean
     

    <p>List of error messages that have been displayed, so we don't duplicate them when dumping any remaining ones at the end.</p>

    $displayed_errors : array
     

    Array of html attributes.

    $html_attributes 

    When replacing items in a template, these get automatically wrapped. E.g. a template replacement for the class will be converted to class="value". The key is the parameter name, and the value is the html attribute it will be wrapped into.

     

    <p>indicates if any form controls have specified the lockable option. If so, we will need to output some javascript.</p>

    $using_locking : Boolean
     

    <p>Website ID, stored here to assist with caching.</p>

    $website_id : integer