View Source

/hsphere/local/home/c251266/sunsetvines.com/www.sunsetvines.com/sunsetvines/current/vendor/creovel/helpers/ajax.php (4.464 KB)

#0001  
#0002  
#0003  
#0004  
#0005  
#0006  
#0007  
#0008  
#0009  
#0010  
#0011  
#0012  
#0013  
#0014  
#0015  
#0016  
#0017  
#0018  
#0019  
#0020  
#0021  
#0022  
#0023  
#0024  
#0025  
#0026  
#0027  
#0028  
#0029  
#0030  
#0031  
#0032  
#0033  
#0034  
#0035  
#0036  
#0037  
#0038  
#0039  
#0040  
#0041  
#0042  
#0043  
#0044  
#0045  
#0046  
#0047  
#0048  
#0049  
#0050  
#0051  
#0052  
#0053  
#0054  
#0055  
#0056  
#0057  
#0058  
#0059  
#0060  
#0061  
#0062  
#0063  
#0064  
#0065  
#0066  
#0067  
#0068  
#0069  
#0070  
#0071  
#0072  
#0073  
#0074  
#0075  
#0076  
#0077  
#0078  
#0079  
#0080  
#0081  
#0082  
#0083  
#0084  
#0085  
#0086  
#0087  
#0088  
#0089  
#0090  
#0091  
#0092  
#0093  
#0094  
#0095  
#0096  
#0097  
#0098  
#0099  
#0100  
#0101  
#0102  
#0103  
#0104  
#0105  
#0106  
#0107  
#0108  
#0109  
#0110  
#0111  
#0112  
#0113  
#0114  
#0115  
#0116  
#0117  
#0118  
#0119  
#0120  
#0121  
#0122  
#0123  
#0124  
#0125  
#0126  
#0127  
#0128  
#0129  
#0130  
#0131  
#0132  
#0133  
#0134  
#0135  
#0136  
#0137  
#0138  
#0139  
#0140  
#0141  
#0142  
#0143  
#0144  
#0145  
#0146  
#0147  
#0148  
#0149  
#0150  
#0151  
#0152  
#0153  
#0154  
#0155  
#0156  
#0157  
#0158  
#0159  
<?php

/**
 * AJAX methods go here. The following files should be included: prototype.js,
 * dragdrop.js, controls.js, effects.js. 
 */
 
/**
 * Creates an AJAX link tag. An AJAX call will be executed on every click.
 * Example:
 *
 * <code>
 *     <?=link_to_remote('Go AJAK Go!!!', 'results', url_for('company', 'ajax_search') )?>
 *
 *    <div id="results"></div>
 * </code>
 *
 * @param string $link_title optional defaults to "Goto"
 * @param string $id_to_update required
 * @param string $url required
 * @param array $html_options optional
 */
 
function link_to_remote($link_title 'Goto'$id_to_update$url$html_options null) {

    if ( 
is_array($html_options) ){
    
        foreach (
$html_options as $attribute => $value ) {
            if ( 
strstr($attribute'onclick') ) { continue; }
            
$extra    .= ' '.$attribute.'="'.$value.'"';
        }
    
    }

    return 
"<a href=\"#\" onclick=\"new Ajax.Updater('".$id_to_update."', '".$url."', {asynchronous:true}); return false;\">".$link_title."</a>\n";
    
}

/**
 * Creates an AJAX form starter tag. This form will submited using AJAX
 * via post. Example:
 *
 * <code>
 *     <?=form_remote_tag('results', '/company/ajax_search/')?>
 *        First Name: <input type="text" name="agent[name_first]" value=""  /><br />
 *        Last Name: <input type="text" name="agent[name_last]" value=""  /><br />
 *        Email: <input type="text" name="agent[email]" value=""  /><br />
 *        <input type="submit" value="Go AJAX go!!!" />
 *    <?=end_form_tag()?>
 *
 *     <h1>Search Results</h1>
 *    <div id="results"></div>
 * </code>
 *
 * @param string $id_to_update required
 * @param string $url required
 */
 
function form_remote_tag($id_to_update$url) {

    return 
"<form action=\"".$url."\" method=\"post\" onsubmit=\"new Ajax.Updater('".$id_to_update."', '".$url."', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">\n";

}

/**
 * Creates an observer for a form field. Any time the field being observed is 
 * modified it does an ajax call. Example:
 *
 * <code>
 *     <input type="text" id="search" name"search" value="" />
 *    <?= observe_field('search', 0.5, 'results', '/search/ajax_search/') ?>
 *
 *     <h1>Search Results</h1>
 *    <div id="results"></div>
 * </code>
 *
 * @param string $id_of_field required
 * @param int $frequency optional default set to 0
 * @param string $id_to_update required
 * @param string $url required
 */
 
function observe_field($id_of_field$frequency 0$id_to_update$url) {
 
     if ( !
is_numeric($frequency) ) {
        
$frequency 0;
    }
    
     return 
"<script type=\"text/javascript\">new Form.Element.Observer('".$id_of_field."', ".$frequency.", function(element, value) {new Ajax.Updater('".$id_to_update."', '".$url."', {asynchronous:true, evalScripts:true, parameters:value})})</script>\n";
 
}

/**
 * Creates an AJAX object that does period calls depending on the value of the frequency.
 * The bottom example will call the controller "company" and action "ajax_search" every
 * 5 seconds and update "results" div. 
 *
 * <code>
 *     <?=periodically_call_remote('results', url_for('company', 'ajax_search'), 5)?>
 *
 *    <div id="results"></div>
 * </code>
 *
 * @param string $id_to_update required
 * @param string $url required
 * @param int $frequency optional default set to 3
 */

function periodically_call_remote($id_to_update$url$frequency 3) {

    return 
"<script type=\"text/javascript\">new PeriodicalExecuter(function() {new Ajax.Updater('".$id_to_update."', '".$url."', {asynchronous:true, evalScripts:true})}, ".$frequency.")</script>";
    
}

/**
 * Creates an sortable list using dragdrop.js.
 *
 * <code>
 *    <ul id="shopping-list">
 *        <li id="li_1">Triscuit</li>
 *        <li id="li_2">Milk</li>
 *        <li id="li_3">Cake</li>
 *        <li id="li_4">Biscuit</li>
 *    </ul>
 *
 *    <?=create_sortable_list('shopping-list', 'li')?>
 * </code>
 *
 * Note:
 * To get the sortable list value you can use the function
 * Sortable.serialize. Example:
 *
 *    var list_order = Sortable.serialize('shopping-list');
 *
 * @param string/array $id_of_container required
 * @param int $list_tag optional but is required if not using an array
 */

function create_sortable_list($id_of_container$list_tag null) {

    
$str .= "<script type=\"text/javascript\">\n";
    
$str .= "// <![CDATA[\n";
    
    if ( 
is_array($id_of_container) ) {
        foreach ( 
$id_of_container as $id => $tag ) {
            
$str .= "    Sortable.create('".$id."',{tag:'".$tag."'});\n";
        }
    } else {
        
$str .= "    Sortable.create('".$id_of_container."',{tag:'".$list_tag."'});\n";
    }
    
    
$str .= "// ]]>\n";
    
$str .= "</script>\n";
    
    return 
$str;
    
}

?>