Exemple #1
0
 /**
  * catch all methods for the autocomplete and pass them to this controller
  *
  * @param string $method 
  * @param array $params 
  * @return void
  * @author Craig Ulliott
  */
 public function catch_all($method, array $params)
 {
     $query = array_val($params, 'q');
     $results = WIB::getClient()->call_method('autocomplete.' . $method, array('q' => $query));
     // normalize to minute
     $data = array();
     foreach ($results['results'] as $result) {
         $data[] = array($result['type'] . ':' . $result['id'] => $result['name']);
     }
     v('json/data', $data);
 }
Exemple #2
0
/**
 * call the WIB API and convert urls into trackable urls
 *
 * @param mixed $urls 
 * @return void
 * @author Craig Ulliott
 */
function track_urls($urls, $additional_params = null)
{
    // to make the code easier, and accept one or an array of urls
    if (!is_array($urls)) {
        $urls = array($urls);
        $return_single = true;
    } else {
        $return_single = false;
    }
    // save on bandwidth
    $unique_urls = array_unique($urls);
    // here we add any new params we want to the url
    if ($additional_params) {
        foreach ($unique_urls as $k => $url) {
            // we cant add params to some urls
            if (!stristr($url, 'ad.doubleclick.net')) {
                $unique_urls[$k] = http_add_params($url, $additional_params);
            }
        }
    }
    // build an assoc array to return the new urls
    $new_urls = array();
    // build cache keys
    $cache_keys = array();
    foreach ($unique_urls as $url) {
        $cache_keys[] = 'track_urls_' . $url;
    }
    // try the cache first
    foreach (Cache::get($cache_keys) as $cache_key => $lilurl) {
        $url = substr($cache_key, 11);
        // add to the new url list
        $new_urls[$url] = $lilurl;
        // remove from the $unique_urls queue
        $k = array_search($url, $unique_urls);
        if ($k !== false) {
            unset($unique_urls[$k]);
        }
    }
    // we want to get all the remaining lilurls in one call
    WIB::getClient()->start_batch();
    // loop through each
    foreach ($unique_urls as $url) {
        // if we didnt get it from the cache
        if (!array_key_exists($url, $new_urls)) {
            $response = WIB::getClient()->call_method('lilurl.create', array('url' => $url));
        }
    }
    // get the results
    $results = WIB::getClient()->run_batch();
    // get only the parts we want from each URL
    foreach ($results as $result) {
        //the url we passed to the wib api
        $tracked_url = array_val($result, array('results', 0, 'url'));
        // we now go back to the very original $urls array, as we dont want to pass back the "additional params"
        $k = array_search($tracked_url, $unique_urls);
        $original_url = $urls[$k];
        $tracking_url = array_val($result, array('results', 0, 'lilurl'));
        if ($original_url && $tracking_url) {
            // add it to the result
            $new_urls[$original_url] = $tracking_url;
            // cache it for next time
            $from_cache = Cache::add('track_urls_' . $original_url, $tracking_url);
        }
    }
    if ($return_single) {
        return reset($new_urls);
    }
    return $new_urls;
}