function autocomplete_search_tags($term) { if (!PLUGIN_AUTOCOMPLETE_SEARCH_TAGS) { die; } // Get cache from registry $cache = Zend_Registry::get('cache'); if (($tags = $cache->load('addon_autocomplete')) === false) { // cache missed, we need to build this again $limit = (int) PLUGIN_AUTOCOMPLETE_TAGS_FETCH_LIMIT; $sql = "\n\t\tSELECT content\n\t\tFROM posts\n\t\tWHERE content like '%#%'\n\t\tORDER BY created_on DESC\n\t\tLIMIT {$limit}\n\t\t"; $Model = new Application_Model_Addons(); $rows = $Model->getAdapter()->fetchCol($sql); if (empty($rows)) { return; } // merge all content to a single string $full_content = ''; $raw_tags = null; $tags = array(); foreach ($rows as $key => $val) { // remove dupes from a single post to avoid abuse $val = implode(' ', array_unique(explode(' ', $val))); $val = implode("\n", array_unique(explode("\n", $val))); // concate to one big string $full_content .= $val . ' '; } // match tags //preg_match_all("/(^|[\t\r\n\s])#(\w+)/u", $full_content, $raw_tags); // not uft8 safe preg_match_all("/#([\\p{L}\\p{N}\\-_]+)(?=\\s|\\Z)/u", $full_content, $raw_tags); if (!$raw_tags || empty($raw_tags[1])) { return; } // take 2nd match $raw_tags = $raw_tags[1]; // transform and count foreach ($raw_tags as $key => $value) { $tags[$value] = isset($tags[$value]) ? ++$tags[$value] : 1; } // sort by count arsort($tags); // save to cache $cache->save($tags); } $result = array(); $total = 0; foreach ($tags as $tag => $count) { if (strpos(strtolower($tag), strtolower($term)) !== false) { $result[] = array('value' => $tag, 'label' => $tag); $total++; } if ($total >= 5) { break; } } echo json_encode($result); die; }
if ($controller == 'index' && ($action = 'index')) { $show = true; } if ($controller == 'search') { $show = true; } if (!$show) { return; } $this->attach('hook_view_sidebar', 20, function ($view) { // Get cache from registry $cache = Zend_Registry::get('cache'); if (($out = $cache->load('addon_trendingtags')) === false) { // cache missed, we need to build this again $sql = "\n\t\tSELECT content \n\t\tFROM posts\n\t\tWHERE content like '%#%'\n\t\tORDER BY created_on DESC \n\t\tLIMIT 300\n\t\t"; $Model = new Application_Model_Addons(); $rows = $Model->getAdapter()->fetchCol($sql); if (empty($rows)) { return; } // merge all content to a single string $full_content = ''; $raw_tags = null; $tags = array(); foreach ($rows as $key => $val) { // remove dupes from a single post to avoid abuse $val = implode(' ', array_unique(explode(' ', $val))); $val = implode("\n", array_unique(explode("\n", $val))); // concate to one big string $full_content .= $val . ' '; }