/** * * retrieves all tags used by blog posts along with a count of number of posts for each tag. */ public function all_in_use($opts = array()) { $blogSQL = false; // SQL for refining by blog if (isset($opts['blog']) && PERCH_RUNWAY) { $Blogs = new PerchBlog_Blogs($this->api); $Blog = $Blogs->get_one_by('blogSlug', $opts['blog']); if ($Blog) { $blogSQL = ' AND p.blogID=' . $this->db->pdb((int) $Blog->id()) . ' '; } } if (isset($opts['section']) && $opts['section']) { $Sections = new PerchBlog_Sections($this->api); $Section = $Sections->get_one_by('sectionSlug', $opts['section']); if (!is_object($Section)) { return false; } $sql = 'SELECT t.tagTitle, t.tagSlug, COUNT(p2t.postID) AS qty FROM ' . PERCH_DB_PREFIX . 'blog_tags t, ' . PERCH_DB_PREFIX . 'blog_posts_to_tags p2t, ' . PERCH_DB_PREFIX . 'blog_posts p WHERE p2t.tagID=t.tagID AND p2t.postID=p.postID AND p.postStatus=\'Published\' AND p.postDateTime<=' . $this->db->pdb(date('Y-m-d H:i:00')) . ' AND p.sectionID=' . $this->db->pdb((int) $Section->id()) . $blogSQL . ' GROUP BY t.tagID ORDER BY t.tagTitle ASC '; } else { $sql = 'SELECT t.tagTitle, t.tagSlug, COUNT(p2t.postID) AS qty FROM ' . PERCH_DB_PREFIX . 'blog_tags t, ' . PERCH_DB_PREFIX . 'blog_posts_to_tags p2t, ' . PERCH_DB_PREFIX . 'blog_posts p WHERE p2t.tagID=t.tagID AND p2t.postID=p.postID AND p.postStatus=\'Published\' AND p.postDateTime<=' . $this->db->pdb(date('Y-m-d H:i:00')) . $blogSQL . ' GROUP BY t.tagID ORDER BY t.tagTitle ASC'; } $rows = $this->db->get_rows($sql); $r = $this->return_instances($rows); return $r; }
public function get_custom($opts) { $API = new PerchAPI(1.0, 'perch_blog'); $blogSQL = ''; if ($opts['blog']) { $BlogPosts = new PerchBlog_Posts($API); $Blogs = new PerchBlog_Blogs($API); $Blog = $Blogs->get_one_by('blogSlug', $opts['blog']); if ($Blog) { $blogID = (int) $Blog->id(); $blogSQL = ' AND blogID=' . $this->db->pdb($blogID) . ' '; } } if ($opts['include-empty']) { $sql = 'SELECT *, sectionID AS _id, sectionPostCount as qty FROM ' . $this->table . ' WHERE 1=1 ' . $blogSQL . ' ORDER BY sectionTitle ASC'; } else { $sql = 'SELECT *, sectionID AS _id, sectionPostCount as qty FROM ' . $this->table . ' WHERE sectionPostCount>0 ' . $blogSQL . ' ORDER BY sectionTitle ASC'; } $rows = $this->db->get_rows($sql); $sections = $this->return_instances($rows); $content = array(); if (PerchUtil::count($sections)) { foreach ($sections as $Section) { $content[] = $Section->to_array(); } } if (isset($opts['filter']) && (isset($opts['value']) || is_array($opts['filter']))) { if (PerchUtil::count($content)) { $out = array(); // if it's not a multi-filter, make it look like one to unify what we're working with if (!is_array($opts['filter']) && isset($opts['value'])) { $filters = array(array('filter' => $opts['filter'], 'value' => $opts['value'], 'match' => isset($opts['match']) ? $opts['match'] : 'eq')); $filter_mode = 'AND'; } else { $filters = $opts['filter']; $filter_mode = 'AND'; if (isset($opts['match']) && strtolower($opts['match']) == 'or') { $filter_mode = 'OR'; } } $filter_content = $content; foreach ($filters as $filter) { $key = $filter['filter']; $val = $filter['value']; $match = isset($filter['match']) ? $filter['match'] : 'eq'; foreach ($filter_content as $item) { // If 'AND' mode, remove the item, as we only want it if it's added by this filter too. // ninja code. if ($filter_mode == 'AND' && isset($out[$item['_id']])) { unset($out[$item['_id']]); } if (!isset($item[$key])) { $item[$key] = false; } if (isset($item[$key])) { $this_item = $this->_resolve_to_value($item[$key]); switch ($match) { case 'eq': case 'is': case 'exact': if ($this_item == $val) { $out[$item['_id']] = $item; } break; case 'neq': case 'ne': case 'not': if ($this_item != $val) { $out[$item['_id']] = $item; } break; case 'gt': if ($this_item > $val) { $out[$item['_id']] = $item; } break; case 'gte': if ($this_item >= $val) { $out[$item['_id']] = $item; } break; case 'lt': if ($this_item < $val) { $out[$item['_id']] = $item; } break; case 'lte': if ($this_item <= $val) { $out[$item['_id']] = $item; } break; case 'contains': $value = str_replace('/', '\\/', $val); if (preg_match('/\\b' . $value . '\\b/i', $this_item)) { $out[$item['_id']] = $item; } break; case 'regex': case 'regexp': if (preg_match($val, $this_item)) { $out[$item['_id']] = $item; } break; case 'between': case 'betwixt': $vals = explode(',', $val); if (PerchUtil::count($vals) == 2) { if ($this_item > trim($vals[0]) && $this_item < trim($vals[1])) { $out[$item['_id']] = $item; } } break; case 'eqbetween': case 'eqbetwixt': $vals = explode(',', $val); if (PerchUtil::count($vals) == 2) { if ($this_item >= trim($vals[0]) && $this_item <= trim($vals[1])) { $out[$item['_id']] = $item; } } break; case 'in': case 'within': $vals = explode(',', $val); if (PerchUtil::count($vals)) { foreach ($vals as $value) { if ($this_item == trim($value)) { $out[$item['_id']] = $item; break; } } } break; } } } // if 'AND' mode, run the next filter against the already filtered list if ($filter_mode == 'AND') { $filter_content = $out; } else { $filter_content = $content; } } $content = $out; } } // reindex array $new_content = array(); foreach ($content as $c) { $new_content[] = $c; } $content = $new_content; // sort if (isset($opts['sort'])) { if (isset($opts['sort-order']) && $opts['sort-order'] == 'DESC') { $desc = true; } else { $desc = false; } $content = PerchUtil::array_sort($content, $opts['sort'], $desc); } if (isset($opts['sort-order']) && $opts['sort-order'] == 'RAND') { shuffle($content); } // Pagination if (isset($opts['paginate'])) { $Paging = $API->get('Paging'); if (isset($opts['pagination-var'])) { $Paging->set_qs_param($opts['pagination-var']); } $Paging->set_per_page(isset($opts['count']) ? (int) $opts['count'] : 10); $opts['count'] = $Paging->per_page(); $opts['start'] = $Paging->lower_bound() + 1; $Paging->set_total(PerchUtil::count($content)); } else { $Paging = false; } // limit if (isset($opts['count']) || isset($opts['start'])) { // count if (isset($opts['count'])) { $count = (int) $opts['count']; } else { $count = PerchUtil::count($content); } // start if (isset($opts['start'])) { if ($opts['start'] === 'RAND') { $start = rand(0, PerchUtil::count($content) - 1); } else { $start = (int) $opts['start'] - 1; } } else { $start = 0; } // loop through $out = array(); for ($i = $start; $i < $start + $count; $i++) { if (isset($content[$i])) { $out[] = $content[$i]; } else { break; } } $content = $out; } if (isset($opts['skip-template']) && $opts['skip-template'] == true) { if (isset($opts['raw']) && $opts['raw'] == true) { if (PerchUtil::count($content)) { foreach ($content as &$item) { if (PerchUtil::count($item)) { foreach ($item as &$field) { if (is_array($field) && isset($field['raw'])) { $field = $field['raw']; } } } } } return $content; } } // template $Template = $API->get('Template'); $Template->set('blog/' . $opts['template'], 'blog'); // post process $tags = $Template->find_all_tags_and_repeaters('blog'); $processed_vars = array(); $used_items = array(); foreach ($content as $item) { $tmp = $item; if (PerchUtil::count($tags)) { foreach ($tags as $Tag) { if (isset($item[$Tag->id()])) { $used_items[] = $item; } } } if ($tmp) { $processed_vars[] = $tmp; } } // Paging to template if (is_object($Paging) && $Paging->enabled()) { $paging_array = $Paging->to_array($opts); // merge in paging vars foreach ($processed_vars as &$item) { foreach ($paging_array as $key => $val) { $item[$key] = $val; } } } if (PerchUtil::count($processed_vars)) { $html = $Template->render_group($processed_vars, true); } else { $Template->use_noresults(); $html = $Template->render(array()); } if (isset($opts['skip-template']) && $opts['skip-template'] == true) { $out = array(); if (PerchUtil::count($processed_vars)) { foreach ($processed_vars as &$item) { if (PerchUtil::count($item)) { foreach ($item as &$field) { if (is_array($field) && isset($field['processed'])) { $field = $field['processed']; } if (is_array($field) && isset($field['_default'])) { $field = $field['_default']; } } } } } for ($i = 0; $i < PerchUtil::count($content); $i++) { $out[] = array_merge($content[$i], $processed_vars[$i]); } if (isset($opts['return-html']) && $opts['return-html'] == true) { $out['html'] = $html; } return $out; } return $html; }
/** * * Builds an archive listing looping through years. Echoes out the resulting mark-up and content * @param string $template * @param bool $return if set to true returns the output rather than echoing it */ function perch_blog_date_archive_years($opts = 'year_link.html', $return = false) { $default_opts = array('template' => 'year_link.html', 'skip-template' => false, 'split-items' => false, 'cache' => true, 'section' => false, 'blog' => false); if (!is_array($opts)) { $opts = array('template' => $opts); } if (is_array($opts)) { $opts = array_merge($default_opts, $opts); } else { $opts = $default_opts; } if ($opts['skip-template'] || $opts['split-items']) { $return = true; } $cache = false; $template = $opts['template']; if ($opts['cache']) { $cache_key = 'perch_blog_date_archive_years' . md5(serialize($opts)); $cache = PerchBlog_Cache::get_static($cache_key, 10); if ($opts['skip-template'] || $opts['split-items']) { $cache = unserialize($cache); } } if ($cache) { if ($return) { return $cache; } echo $cache; return ''; } $API = new PerchAPI(1.0, 'perch_blog'); $BlogPosts = new PerchBlog_Posts($API); $blogID = null; if ($opts['blog']) { $Blogs = new PerchBlog_Blogs($API); $Blog = $Blogs->get_one_by('blogSlug', $opts['blog']); if ($Blog) { $blogID = (int) $Blog->id(); } } $sectionID = null; if ($opts['section']) { $Sections = new PerchBlog_Sections($API); $Section = $Sections->find_by_given($opts['section']); if (is_object($Section)) { $sectionID = (int) $Section->id(); } } $years = $BlogPosts->get_years($sectionID, $blogID); if ($opts['skip-template'] || $opts['split-items']) { if ($opts['cache']) { PerchBlog_Cache::save_static($cache_key, serialize($years)); } return $years; } $Template = $API->get('Template'); $Template->set('blog/' . $template, 'blog'); $r = $Template->render_group($years, true); if ($r != '') { PerchBlog_Cache::save_static($cache_key, $r); } if ($return) { return $r; } echo $r; return false; }
if ($Settings->get('perch_blog_update')->val() != '5.0.1') { PerchUtil::redirect($API->app_path() . '/update/'); } $Posts = new PerchBlog_Posts($API); $Blogs = new PerchBlog_Blogs($API); $blogs = $Blogs->all(); if (!PerchUtil::count($blogs)) { $Posts->attempt_install(); $blogs = $Blogs->all(); } $Paging = $API->get('Paging'); $Paging->set_per_page(15); $Blog = false; if (PERCH_RUNWAY) { if (PerchUtil::get('blog')) { $Blog = $Blogs->get_one_by('blogSlug', PerchUtil::get('blog')); } } if (!$Blog) { $Blog = $Blogs->find(1); } $Categories = new PerchCategories_Categories(); $categories = $Categories->get_for_set($Blog->setSlug()); $Sections = new PerchBlog_Sections($API); $sections = $Sections->get_by('blogID', (int) $Blog->id()); $Lang = $API->get('Lang'); $posts = array(); $filter = 'all'; if (isset($_GET['category']) && $_GET['category'] != '') { $filter = 'category'; $category = $_GET['category'];