Example #1
0
 /**
  * blcLinkQuery::get_links()
  *
  * @see blc_get_links()
  *
  * @param array $params
  * @return array|int
  */
 function get_links($params = null)
 {
     global $wpdb;
     /** @var wpdb $wpdb */
     if (!is_array($params)) {
         $params = array();
     }
     $defaults = array('offset' => 0, 'max_results' => 0, 'load_instances' => false, 'load_containers' => false, 'load_wrapped_objects' => false, 'count_only' => false, 'purpose' => '', 'include_invalid' => false, 'orderby' => '', 'order' => '');
     $params = array_merge($defaults, $params);
     //Compile the search-related params into search expressions usable in a WHERE clause
     $criteria = $this->compile_search_params($params);
     //Build the WHERE clause
     if (!empty($criteria['where_exprs'])) {
         $where_expr = "\t( " . implode(" ) AND\n\t( ", $criteria['where_exprs']) . ' ) ';
     } else {
         $where_expr = '1';
     }
     //Join the blc_instances table if it's required to perform the search.
     $joins = "";
     if ($criteria['join_instances']) {
         $joins = "JOIN {$wpdb->prefix}blc_instances AS instances ON links.link_id = instances.link_id";
     }
     //Optional sorting
     if (!empty($criteria['order_exprs'])) {
         $order_clause = 'ORDER BY ' . implode(', ', $criteria['order_exprs']);
     } else {
         $order_clause = '';
     }
     if ($params['count_only']) {
         //Only get the number of matching links.
         $q = "\r\r\n\t\t\t\tSELECT COUNT(*)\r\r\n\t\t\t\tFROM (\t\r\r\n\t\t\t\t\tSELECT 0\r\r\n\t\t\t\t\t\r\r\n\t\t\t\t\tFROM \r\r\n\t\t\t\t\t\t{$wpdb->prefix}blc_links AS links \r\r\n\t\t\t\t\t\t{$joins}\r\r\n\t\t\t\t\t\r\r\n\t\t\t\t\tWHERE\r\r\n\t\t\t\t\t\t{$where_expr}\r\r\n\t\t\t\t\t\r\r\n\t\t\t\t   GROUP BY links.link_id) AS foo";
         return $wpdb->get_var($q);
     }
     //Select the required links.
     $q = "SELECT \r\r\n\t\t\t\t links.*\r\r\n\t\t\t\t\r\r\n\t\t\t  FROM \r\r\n\t\t\t\t {$wpdb->prefix}blc_links AS links\r\r\n\t\t\t\t {$joins}\r\r\n\t\t\t\t\r\r\n\t\t\t  WHERE\r\r\n\t\t\t\t {$where_expr}\r\r\n\t\t\t\t \r\r\n\t\t\t   GROUP BY links.link_id\r\r\n\r\r\n\t\t\t   {$order_clause}";
     //Note: would be a lot faster without GROUP BY
     //Add the LIMIT clause
     if ($params['max_results'] || $params['offset']) {
         $q .= sprintf("\nLIMIT %d, %d", $params['offset'], $params['max_results']);
     }
     $results = $wpdb->get_results($q, ARRAY_A);
     if (empty($results)) {
         return array();
     }
     //Create the link objects
     $links = array();
     foreach ($results as $result) {
         $link = new blcLink($result);
         $links[$link->link_id] = $link;
     }
     $purpose = $params['purpose'];
     /*
     Preload instances if :
     	* It has been requested via the 'load_instances' argument. 
     	* The links are going to be displayed or edited, which involves instances. 
     */
     $load_instances = $params['load_instances'] || in_array($purpose, array(BLC_FOR_DISPLAY, BLC_FOR_EDITING));
     if ($load_instances) {
         $link_ids = array_keys($links);
         $all_instances = blc_get_instances($link_ids, $purpose, $params['load_containers'], $params['load_wrapped_objects']);
         //Assign each batch of instances to the right link
         foreach ($all_instances as $link_id => $instances) {
             foreach ($instances as $instance) {
                 /** @var blcLinkInstance $instance */
                 $instance->_link = $links[$link_id];
             }
             $links[$link_id]->_instances = $instances;
         }
     }
     return $links;
 }
Example #2
0
 /**
  * Get a list of the link's instances
  *
  * @param bool $ignore_cache Don't use the internally cached instance list.
  * @param string $purpose 
  * @return blcLinkInstance[] An array of instance objects or FALSE on failure.
  */
 function get_instances($ignore_cache = false, $purpose = '')
 {
     if (!$this->valid() || empty($this->link_id)) {
         return false;
     }
     if ($ignore_cache || is_null($this->_instances)) {
         $instances = blc_get_instances(array($this->link_id), $purpose);
         if (!empty($instances)) {
             $this->_instances = $instances[$this->link_id];
         }
     }
     return $this->_instances;
 }