Skip to content

Instantly share code, notes, and snippets.

@sebsel
Created June 28, 2017 18:00
Indexer for Kirby
<?php
class Indexer {
static $fields;
static $methods;
static $site;
static function get($where = null, $num = 20, $hideDeleted = true) {
if (!static::$site) static::$site = site();
$pages = new Pages();
$query = db::connect()->table('page')->select('page.id');
$query->where($where)
->where('visible = 1')
->where('published < \'' . strftime('%F %T').'\'')
->where('private = 0');
if($hideDeleted)
$query->where('(deleted is null or deleted > \'' . strftime('%F %T').'\')');
foreach ($query->order('page.published desc')->limit($num)->all() as $page) {
$pages->append($page->id, static::$site->children()->find($page->id));
}
return $pages;
}
static function getWith($with, $value, $where = [], $num = 20, $hideDeleted = true) {
if (!static::$site) static::$site = site();
$pages = new Pages();
$query = db::connect()->table($with)->select('page_id as id');
$query->join('page', 'page_id = page.id');
$query->where($where)
->where([$with => $value])
->where('visible = 1')
->where('published < \'' . strftime('%F %T').'\'')
->where('private = 0');
if($hideDeleted)
$query->where('(deleted is null or deleted > \'' . strftime('%F %T').'\')');
foreach ($query->order('page.published desc')->limit($num)->all() as $page) {
$pages->append($page->id, static::$site->children()->find($page->id));
}
return $pages;
}
static function exists($page) {
return db::one('page', 'id', ['id' => $page->uri()]) != false;
}
static function add($page) {
$fields = ['id' => $page->id()] + static::handleFields($page);
if (!db::insert('page', $fields))
throw new Error('Database error! '.db::connection()->lastError());
}
static function update($page) {
$fields = static::handleFields($page);
if (!db::update('page', $fields, ['id' => $page->id()]))
throw new Error('Database error! '.db::connection()->lastError());
}
static function delete($page) {
$fields = static::handleFields($page);
if (!db::delete('page', ['id' => $page->id()]))
throw new Error('Database error! '.db::connection()->lastError());
}
private static function handleFields($page) {
$fields = [];
foreach (static::$fields as $field => $options) {
if (is_callable(static::$methods[$options['type']])) {
$value = call(static::$methods[$options['type']], [$page, $field, $options]);
if (isset($value)) $fields[$field] = $value;
} else {
throw new Error('Field type not found');
}
}
return $fields;
}
}
indexer::$methods['visible'] = function ($page, $field, $options) {
return $page->isVisible() ? 1 : 0;
};
indexer::$methods['template'] = function ($page, $field, $options) {
return $page->template();
};
indexer::$methods['function'] = function ($page, $field, $options) {
return call([$page, $options['action']]);
};
indexer::$methods['bool'] = function ($page, $field, $options) {
return $page->content()->get($options['field']) == '' ? 0 : 1;
};
indexer::$methods['literal'] = function ($page, $field, $options) {
$options['default'] = array_key_exists('default', $options) ? $options['default'] : null;
return $page->content()->get($options['field']) == '' ? $options['default'] : $page->content()->get($options['field']);
};
indexer::$methods['table'] = function ($page, $field, $options) {
db::delete($field, ['page_id' => $page->id()]);
if ($page->content()->get($options['field'])->value() != '') {
$parts = explode(',', strtolower($page->content()->get($options['field'])->value()));
foreach ($parts as $part) {
db::insert($field, [
'page_id' => $page->id(),
$field => $part,
]);
}
}
return null;
};
indexer::$methods['table_callback'] = function ($page, $field, $options) {
db::delete($field, ['page_id' => $page->id()]);
$parts = call($options['callback'], $page);
foreach ($parts as $part) {
db::insert($field, [
'page_id' => $page->id(),
$field => $part,
]);
}
return null;
};
indexer::$fields['type'] = [
'type' => 'function',
'action' => 'postType'
];
indexer::$fields['visible'] = [
'type' => 'visible'
];
indexer::$fields['published'] = [
'field' => 'published',
'type' => 'literal'
];
indexer::$fields['deleted'] = [
'field' => 'deleted',
'type' => 'literal'
];
indexer::$fields['lang'] = [
'field' => 'lang',
'type' => 'literal',
'default' => 'nl'
];
indexer::$fields['private'] = [
'field' => 'private',
'type' => 'literal',
'default' => '0'
];
indexer::$fields['category'] = [
'field' => 'category',
'type' => 'table_callback',
'callback' => function($page) {
if ($page->content()->get('text')->isEmpty()
and $page->content()->get('category')->isEmpty()) return [];
$parts = [];
foreach($page->categories() as $cat) $parts[] = $cat->tag();
return $parts;
}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment