/**
  * Function to display a random (or not so random) quote with (or without)
  * ajax refresh functionality. Used by the 'quotescollection_quote' template
  * function and the 'Random Quote' widget.
  */
 public function quote($args = array())
 {
     global $quotescollection_db;
     $args_default = array('instance' => '', 'show_author' => 1, 'show_source' => 0, 'ajax_refresh' => 1, 'random' => 1, 'auto_refresh' => 0, 'tags' => '', 'char_limit' => 500, 'echo' => 1);
     // Merge with default values
     $args = array_merge($args_default, $args);
     $instance = is_string($args['instance']) ? $args['instance'] : '';
     $show_author = false == $args['show_author'] ? 0 : 1;
     $show_source = true == $args['show_source'] ? 1 : 0;
     $ajax_refresh = false == $args['ajax_refresh'] ? 0 : 1;
     $auto_refresh = 0;
     if ($args['auto_refresh']) {
         if (is_numeric($args['auto_refresh'])) {
             $auto_refresh = $args['auto_refresh'];
         } else {
             if (true === $args['auto_refresh']) {
                 $auto_refresh = 5;
             }
         }
     }
     $random = false == $args['random'] ? 0 : 1;
     // Tags can only be passed as a string, comma separated
     $tags = is_string($args['tags']) ? $args['tags'] : '';
     // Only a numeral can be passed as char_limit
     $char_limit = is_numeric($args['char_limit']) ? $args['char_limit'] : 500;
     // By default, we fetch a random quote
     $orderby = 'random';
     if (!$random) {
         $orderby = 'quote_id';
     }
     // Frame the conditions to fetch the quote
     $condition = array('orderby' => $orderby, 'order' => 'DESC', 'tags' => $tags, 'char_limit' => $char_limit, 'public' => 'yes');
     if (!($count = $quotescollection_db->count($condition))) {
         return "";
     }
     $dynamic_fetch = 0;
     if ('random' == $orderby && ($options = get_option('quotescollection')) && isset($options['dynamic_fetch']) && $options['dynamic_fetch'] == 'on' && $count > 1) {
         $dynamic_fetch = 1;
     }
     $display = "";
     // And fetch the quote, only if dynamic fetch is off
     if (!$dynamic_fetch) {
         if ($quote = Quotes_Collection_Quote::with_condition($condition)) {
             $curr_quote_id = $quote->quote_id;
             $display = $quote->output_format(array('show_author' => $show_author, 'show_source' => $show_source, 'before' => '', 'after' => ''));
             if (!$display) {
                 return "";
             }
         } else {
             return "";
         }
     } else {
         $curr_quote_id = 0;
     }
     // Refresh functionality is included only when there is more than one quote for the condition
     // And instance ID is required for the refresh functionality
     if ($count > 1 && $instance) {
         if ($dynamic_fetch || $ajax_refresh) {
             $display .= "<script type=\"text/javascript\">\n";
             $display .= 'var args_' . $instance . ' = {' . '"instanceID":"' . $instance . '", ' . '"currQuoteID":' . $curr_quote_id . ', ' . '"showAuthor":' . $show_author . ', ' . '"showSource":' . $show_source . ', ' . '"tags":"' . $tags . '", ' . '"charLimit":' . $char_limit . ', ' . '"orderBy":"' . $orderby . '", ' . '"ajaxRefresh":' . $ajax_refresh . ', ' . '"autoRefresh":' . $auto_refresh . ', ' . '"dynamicFetch":' . $dynamic_fetch . '};';
             if ($dynamic_fetch) {
                 $display .= 'quotescollectionRefresh(args_' . $instance . ');';
             } else {
                 if ($ajax_refresh && $auto_refresh) {
                     $display .= 'quotescollectionTimer(args_' . $instance . ');';
                 } else {
                     if ($ajax_refresh && !$auto_refresh) {
                         $display .= "\n<!--\ndocument.write(\"" . '<footer class=\\"navigation\\">' . '<div class=\\"nav-next\\">' . '<a class=\\"next-quote-link\\" style=\\"cursor:pointer;\\" onclick=\\"quotescollectionRefresh(args_' . $instance . ')\\">' . html_entity_decode($this->refresh_link_text) . '</a>' . '</div>' . '</footer>' . "\")\n//-->\n";
                     }
                 }
             }
             $display .= "</script>\n";
         }
     }
     $instance_id = ' id="' . $args['instance'] . '"';
     $display = '<div class="quotescollection-quote"' . $instance_id . '>' . $display . '</div>';
     // If 'echo' argument is false, we return the display.
     if (isset($args['echo']) && !$args['echo']) {
         return $display;
     } else {
         // Else we simply output the display
         echo $display;
     }
     return true;
 }
 public function do_shortcode($atts = array())
 {
     $db = new Quotes_Collection_DB();
     extract(shortcode_atts(array('limit' => 0, 'id' => 0, 'author' => '', 'source' => '', 'tags' => '', 'orderby' => 'quote_id', 'order' => 'ASC', 'paging' => false, 'limit_per_page' => 10), $atts));
     // Initialize the variable that holds args to be passed to the DB function to get the quotes
     // And set 'public' argument to 'yes', because we don't want to display private quotes in public
     $db_args = array('public' => 'yes');
     // 'quote_id' is also a valid shortcode parameter synonymous to 'id'
     if (isset($quote_id) && is_numeric($quote_id)) {
         $id = $quote_id;
     }
     // If an id is specified, no need to process other attributes. Simply get the quote and return.
     if ($id && is_numeric($id)) {
         $db_args['quote_id'] = $id;
         if ($quote = Quotes_Collection_Quote::with_condition($db_args)) {
             return $quote->output_format();
         } else {
             return "";
         }
     }
     if ($author) {
         $db_args['author'] = $author;
     }
     if ($source) {
         $db_args['source'] = $source;
     }
     if ($tags) {
         $db_args['tags'] = $tags;
     }
     switch ($orderby) {
         case 'quote_id':
         case 'author':
         case 'source':
         case 'time_added':
         case 'random':
             $db_args['orderby'] = $orderby;
             break;
         case 'date_added':
             $db_args['orderby'] = 'time_added';
             break;
         case 'rand':
             $db_args['orderby'] = 'random';
             break;
         default:
             $db_args['orderby'] = 'quote_id';
     }
     if ($order == 'DESC' || $order == 'desc' || $order == 'Desc') {
         $db_args['order'] = 'DESC';
     } else {
         $db_args['order'] = 'ASC';
     }
     $page_nav = "";
     if ($paging == true || $paging == 1) {
         $num_quotes = $db->count($db_args);
         $total_pages = ceil($num_quotes / $limit_per_page);
         if (!isset($_GET['quotes_page']) || !$_GET['quotes_page'] || !is_numeric($_GET['quotes_page'])) {
             $page = 1;
         } else {
             $page = $_GET['quotes_page'];
         }
         if ($page > $total_pages) {
             $page = $total_pages;
         }
         if ($page_nav = $this->pagenav($total_pages, $page, 0, 'quotes_page')) {
             $page_nav = '<div class="quotescollection_pagenav">' . $page_nav . '</div>';
         }
         $start = ($page - 1) * $limit_per_page;
         $db_args['num_quotes'] = $limit_per_page;
         $db_args['start'] = $start;
     } else {
         if ($limit && is_numeric($limit)) {
             $db_args['num_quotes'] = $limit;
         }
     }
     if ($quotes_data = $db->get_quotes($db_args)) {
         return $page_nav . $this->output_format($quotes_data) . $page_nav;
     } else {
         return "";
     }
 }