';
$html .= '';
$html .= '';
$html .= ''.$L->get('Amount of items to show on the feed').'';
$html .= '
';
return $html;
}
// HTML for sidebar
public function siteSidebar()
{
$html = '
';
$html .= '
'.$this->getValue('label').'
';
$html .= '
';
$html .= '';
$html .= '';
$html .= '
';
$html .= '
';
return $html;
}
public function install($position=0)
{
parent::install($position);
return $this->createCache();
}
public function post()
{
// Call the method
parent::post();
// After POST request
$this->createCache();
}
public function afterPageCreate()
{
$this->createCache();
}
public function afterPageModify()
{
$this->createCache();
}
public function afterPageDelete()
{
$this->createCache();
}
public function beforeAll()
{
// Check if the URL match with the webhook
$webhook = 'search';
if ($this->webhook($webhook, false, false)) {
global $site;
global $url;
// Change the whereAmI to avoid load pages in the rule 69.pages
// This is only for performance propose
$url->setWhereAmI('search');
// Get the string to search from the URL
$stringToSearch = $this->webhook($webhook, true, false);
$stringToSearch = trim($stringToSearch, '/');
// Search the string in the cache and get all pages with matches
$list = $this->search($stringToSearch);
$this->numberOfItems = count($list);
// Split the content in pages
// The first page number is 1, so the real is 0
$realPageNumber = $url->pageNumber() - 1;
$itemsPerPage = $site->itemsPerPage();
$chunks = array_chunk($list, $itemsPerPage);
if (isset($chunks[$realPageNumber])) {
$this->pagesFound = $chunks[$realPageNumber];
}
}
}
public function paginator()
{
$webhook = 'search';
if ($this->webhook($webhook, false, false)) {
// Get the pre-defined variable from the rule 99.paginator.php
// Is necessary to change this variable to fit the paginator with the result from the search
global $numberOfItems;
$numberOfItems = $this->numberOfItems;
}
}
public function beforeSiteLoad()
{
$webhook = 'search';
if ($this->webhook($webhook, false, false)) {
global $url;
// Get the pre-defined variable from the rule 69.pages.php
// We change the content to show in the website
global $content;
$content = array();
foreach ($this->pagesFound as $pageKey) {
try {
$page = new Page($pageKey);
array_push($content, $page);
} catch (Exception $e) {
// continue
}
}
}
}
// Generate the cache file
private function createCache()
{
// Include Pages-Object to manage the database of pages
global $pages;
// Number of items to retrive from the database
// -1 means all of them
$numberOfItems = -1;
// Page number is not take in count because we retrive all the pages
$pageNumber = 1;
// Only published pages
$onlyPublished = true;
// Get the list of pages
$list = $pages->getList($pageNumber, $numberOfItems, $onlyPublished);
$cache = array();
foreach ($list as $pageKey) {
$page = buildPage($pageKey);
// Process content
$content = $page->content();
$content = Text::removeHTMLTags($content);
$content = Text::truncate($content, 1200, '');
// Include page to the cache
$cache[$pageKey]['title'] = $page->title();
$cache[$pageKey]['permalink'] = $page->permalink();
$cache[$pageKey]['content'] = $content;
$cache[$pageKey]['key'] = $pageKey;
}
// Generate JSON file with the cache
$json = json_encode($cache);
return file_put_contents($this->cacheFile(), $json, LOCK_EX);
}
// Returns the absolute path where is the cache file stored
private function cacheFile()
{
return $this->workspace().'cache.json';
}
// Search text inside the cache
// Returns an array with the pages related to the text
// The array is order by score
private function search($text)
{
$json = file_get_contents($this->cacheFile());
$cache = json_decode($json, true);
$found = array();
foreach ($cache as $page) {
$score = 0;
if (Text::stringContains($page['title'], $text, false)) {
$score += 10;
}
if (Text::stringContains($page['content'], $text, false)) {
$score += rand(1,5);
}
if ($score>0) {
$found[$score] = $page['key'];
}
}
//arsort($found);
return $found;
}
}