diff --git a/contrib/extensions/k2/model/articles.php b/contrib/extensions/k2/model/articles.php new file mode 100644 index 000000000..50cbf40f4 --- /dev/null +++ b/contrib/extensions/k2/model/articles.php @@ -0,0 +1,213 @@ + + * @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository + */ + +class ExtK2ModelArticles extends ComPagesModelDatabase +{ + public function __construct(KObjectConfig $config) + { + parent::__construct($config); + + $this->getState() + ->insertUnique('id' , 'cmd') + ->insertUnique('slug', 'cmd') + ->insert('category' , 'cmd') + ->insert('tags' , 'cmd') + + ->insert('published' , 'boolean') + ->insert('archived' , 'boolean') + ->insert('trashed' , 'boolean', false) + ->insert('featured' , 'boolean') + + ->insert('author' , 'string') + ->insert('editor' , 'string') + ->insert('access' , 'cmd', array_unique($this->getObject('user')->getRoles())) + ; + } + + protected function _initialize(KObjectConfig $config) + { + $config->append(array( + 'persistable' => false, + 'type' => 'articles', + 'entity' => 'article', + 'table' => 'k2_items', + 'aliases' => array() + )); + parent::_initialize($config); + } + + public function getAliases() + { + return $this->getConfig()->aliases; + } + + public function getQuery($columns = true) + { + $state = $this->getState(); + + $query = $this->getObject('database.query.select') + ->table(array('tbl' => $this->getTable()->getName())); + + //#__content + if($columns) + { + //#__tags + $query->columns([ + 'tags' => $this->getObject('database.query.select') + ->table(array('t' => 'k2_tags')) + ->columns('GROUP_CONCAT(t.name)') + ->join(['m' => 'k2_tags_xref'], 'm.tagID = t.id') + ->where('m.itemID = tbl.id') + ->where('(t.published = :published)')->bind(['published' => 1]) + ]); + + + $query->columns([ + 'id' => 'tbl.id', + 'title' => 'tbl.title', + 'slug' => 'tbl.alias', + 'summary' => 'tbl.metadesc', + 'content' => 'CONCAT_WS("", tbl.introtext, IF(LENGTH(tbl.fulltext), tbl.fulltext ,NULL))', + 'category' => 'tbl.catid', + + 'published' => 'tbl.published', + 'archived' => 'IF(tbl.publish_down > CURRENT_TIMESTAMP, 1, 0)', + 'trashed' => 'tbl.trash', + 'featured' => 'tbl.featured', + + 'author' => 'tbl.created_by', + 'editor' => 'GREATEST(tbl.created_by, tbl.modified_by)', + + 'date' => 'tbl.created', + 'edited_date' => 'GREATEST(tbl.created, tbl.modified)', + 'published_date' => 'tbl.publish_up', + 'archived_date' => 'tbl.publish_down', + + 'fields' => 'tbl.extra_fields', + 'parameters' => 'tbl.params', + 'impressions' => 'tbl.hits', + + //Protected properties (for getters) + '_metadata' => 'tbl.metadata', + '_image_caption' => 'tbl.image_caption', + ]); + } + + //Joins + $query + ->join(['c' => 'k2_categories'] , 'tbl.catid = c.id') + ->join(['g' => 'usergroups'] , 'tbl.access = g.id') + ->join(['m' => 'k2_tags_xref'] , 'tbl.id = m.itemID') + ->join(['t' => 'k2_tags'] , 't.id = m.tagID'); + + if(!is_null($state->id)) + { + if(is_string($state->id)) { + $articles = array_unique(explode(',', $state->id)); + } else { + $articles = (array) $state->id; + } + + $query->where('(tbl.id IN :articles)')->bind(['articles' => $articles]); + } + else if(!is_null($state->slug)) { + $query->where('(tbl.alias = :article)')->bind(['article' => $state->slug]); + } + + if(!is_null($state->category)) + { + if(is_string($state->category)) { + $categories = array_unique(explode(',', $state->category)); + } else { + $categories = (array) $state->category; + } + + $query->where('(tbl.catid IN :category)')->bind(['category' => $categories]); + } + + if(!is_null($state->tags)) + { + if(is_string($state->tags)) { + $tags = array_unique(explode(',', $state->tags)); + } else { + $tags = (array) $state->tags; + } + + $query->where('(t.title IN :tags)')->bind(['tags' => $tags]); + } + + if(!is_null($state->author)) + { + if(is_string($state->author)) { + $users = array_unique(explode(',', $state->author)); + } else { + $users = (array) $state->author; + } + + $query->where('(tbl.created_by IN :authors)')->bind(['authors' => $users]); + } + + if (!is_null($state->editor)) + { + if(is_string($state->editor)) { + $users = array_unique(explode(',', $state->editor)); + } else { + $users = (array) $state->editor; + } + + $query->where('(tbl.modified_by IN :editors)')->bind(['editors' => $users]); + } + + if (!is_null($state->access)) + { + if(is_string($state->access)) { + $access = array_unique(explode(',', $state->access)); + } else { + $access = (array) $state->access; + } + + //If user doesn't have access to the category, he doesn't have access to the articles + $query->where('(tbl.access IN :access)')->bind(['access' => $access]); + $query->where('(c.access IN :access)')->bind(['access' => $access]); + } + + if (!is_null($state->published)) + { + if($state->published) { + $query->where('(tbl.published = 1)'); + } else { + $query->where('(tbl.published = 0)'); + } + } + + if (!is_null($state->trashed)) + { + if($state->trashed) { + $query->where('(tbl.trash = 1)'); + } else { + $query->where('(tbl.trash = 0)'); + } + } + + if (!is_null($state->archived)) + { + if($state->archived) { + $query->where('(tbl.publish_down > CURRENT_TIMESTAMP)'); + } else { + $query->where('(tbl.publish_down < CURRENT_TIMESTAMP)'); + } + } + + if (!is_null($state->featured)) { + $query->where('(tbl.featured = :featured)')->bind(['featured' => (bool) $state->featured]); + } + + return $query; + } +} diff --git a/contrib/extensions/k2/model/attachments.php b/contrib/extensions/k2/model/attachments.php new file mode 100644 index 000000000..7bfb2a58d --- /dev/null +++ b/contrib/extensions/k2/model/attachments.php @@ -0,0 +1,74 @@ + + * @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository + */ + +class ExtK2ModelAttachments extends ComPagesModelDatabase +{ + public function __construct(KObjectConfig $config) + { + parent::__construct($config); + + $this->getState() + ->insertUnique('id', 'cmd') + ->insert('article', 'int'); + } + + protected function _initialize(KObjectConfig $config) + { + $config->append(array( + 'persistable' => false, + 'type' => 'article_attachments', + 'entity' => 'attachment', + 'table' => 'k2_attachments', + )); + parent::_initialize($config); + } + + public function getQuery($columns = true) + { + $state = $this->getState(); + + $query = $this->getObject('database.query.select') + ->table(array('tbl' => $this->getTable()->getName())); + + if($columns) + { + $query->columns([ + 'id' => 'tbl.id', + 'title' => 'tbl.title', + 'url' => 'tbl.filename', + 'alt' => 'tbl.titleAttribute', + 'impressions' => 'tbl.hits', + ]); + } + + if(!is_null($state->id)) + { + if(is_string($state->id)) { + $articles = array_unique(explode(',', $state->id)); + } else { + $articles = (array) $state->id; + } + + $query->where('(tbl.id IN :articles)')->bind(['articles' => $articles]); + } + + if(!is_null($state->article)) + { + if(is_string($state->article)) { + $articles = array_unique(explode(',', $state->article)); + } else { + $articles = (array) $state->article; + } + + $query->where('(tbl.itemID IN :articles)')->bind(['articles' => $articles]); + } + + return $query; + } +} diff --git a/contrib/extensions/k2/model/categories.php b/contrib/extensions/k2/model/categories.php new file mode 100644 index 000000000..88fdc02d9 --- /dev/null +++ b/contrib/extensions/k2/model/categories.php @@ -0,0 +1,92 @@ + + * @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository + */ + +class ExtK2ModelCategories extends ComPagesModelDatabase +{ + public function __construct(KObjectConfig $config) + { + parent::__construct($config); + + $this->getState() + ->insertUnique('id' , 'cmd') + ->insert('published' , 'boolean') + ->insert('access' , 'cmd', array_unique($this->getObject('user')->getRoles())) + ; + } + + protected function _initialize(KObjectConfig $config) + { + $config->append(array( + 'persistable' => false, + 'type' => 'article_categories', + 'entity' => 'category', + 'table' => 'k2_categories', + )); + parent::_initialize($config); + } + + public function getQuery($columns = true) + { + $state = $this->getState(); + + $query = $this->getObject('database.query.select') + ->table(array('tbl' => $this->getTable()->getName())); + + //#__k2_categories + if($columns) + { + $query->columns([ + 'id' => 'tbl.id', + 'title' => 'tbl.name', + 'slug' => 'tbl.alias', + 'parent' => 'IF(tbl.parent > 0, tbl.parent, NULL)', + 'content' => 'tbl.description', + 'image' => 'tbl.image', + + 'published' => 'tbl.published', + + 'parameters' => 'tbl.params', + ]); + } + + if(!is_null($state->id)) + { + if(is_string($state->id)) { + $categories = array_unique(explode(',', $state->id)); + } else { + $categories = (array) $state->id; + } + + $query->where('(tbl.id IN :categories)')->bind(['categories' => $categories]); + } + + if (!is_null($state->access)) + { + if(is_string($state->access)) { + $access = array_unique(explode(',', $state->access)); + } else { + $access = (array) $state->access; + } + + //If user doesn't have access to the category, he doesn't have access to the articles + $query->where('(tbl.access IN :access)')->bind(['access' => $access]); + } + + if (!is_null($state->published)) + { + if($state->published) { + $query->where('(tbl.published = 1)'); + } else { + $query->where('(tbl.published = 0)'); + } + } + + return $query; + } +} diff --git a/contrib/extensions/k2/model/entity/abstract.php b/contrib/extensions/k2/model/entity/abstract.php new file mode 100644 index 000000000..a589cbf02 --- /dev/null +++ b/contrib/extensions/k2/model/entity/abstract.php @@ -0,0 +1,190 @@ + + * @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository + */ + +abstract class ExtK2ModelEntityAbstract extends ComPagesModelEntityItem +{ + protected function _initialize(KObjectConfig $config) + { + $config->append([ + 'data' => [ + 'id' => '', + 'slug' => '', + 'name' => '', + 'title' => '', + 'summary' => '', + 'text' => '', + 'date' => 'now', + 'author' => '', + 'image' => [ + 'url' => '', + 'alt' => '', + 'caption' => '', + ], + 'metadata' => [ + 'og:type' => 'website', + 'og:title' => null, + 'og:url' => null, + 'og:image' => null, + 'og:description' => null, + ], + 'parameters' => [], + 'direction' => 'auto', + 'language' => 'en-GB', + ], + 'internal_properties' => ['content', 'text', 'parameters'], + 'base_path' => $this->getObject('request')->getBasePath(), + ]); + + parent::_initialize($config); + } + + public function get($property, $default = null) + { + if($this->hasProperty($property)) { + $result = $this->getProperty($property); + } else { + $result = $default; + } + + return $result; + } + + public function getPropertyName() + { + if(empty($name)) { + $name = ucwords(str_replace(array('_', '-'), ' ', $this->slug)); + } + + return $name; + } + + public function getPropertyText() + { + return $this->getContent(); + } + + public function getPropertyMetadata() + { + //Get the raw metadata + $metadata = $this->_metadata; + + if(is_string($metadata)) { + $metadata = array_filter((array) json_decode($metadata, true)); + } + + //Remove empty values + $metadata = new ComPagesObjectConfig($metadata); + $metadata->append($this->getConfig()->data->metadata); + + if(!isset($metadata->description) && $this->summary) { + $metadata->set('description', $this->summary); + } + + //Only set one image (give priority to the text image) + if($this->image && $this->image->url) { + $metadata->set('og:image', $this->image->url); + } + + //Type and image are required. If they are not set remove any opengraph properties + if(!empty($metadata->get('og:type')) && $metadata->has('og:image')) + { + if($this->title) { + $metadata->set('og:title', $this->title); + } + + if($this->summary) { + $metadata->set('og:description', $this->summary); + } + + if($this->language) { + $metadata->set('og:locale', $this->language); + } + } + else + { + foreach($metadata as $name => $value) + { + if(strpos($name, 'og:') === 0 || strpos($name, 'twitter:') === 0) { + $metadata->remove($name); + } + } + } + + return $metadata; + } + + public function setPropertyDate($value) + { + if(is_integer($value)) { + $date = $this->getObject('date')->setTimestamp($value); + } else { + $date = $this->getObject('date', array('date' => trim($value))); + } + + return $date; + } + + public function setPropertyParameters($value) + { + return new ComPagesObjectConfig($value); + } + + public function getBasePath() + { + return $this->getConfig()->base_path; + } + + public function getAuthor() + { + $user = $this->getObject('user.provider')->load($this->author); + return $user; + } + + public function getContent() + { + static $prepared; + + //Prepare the content + if(!$prepared) + { + JPluginHelper::importPlugin('content'); + + $content = new stdClass; + $content->text = $this->content; + $params = (object) $this->parameters->toArray(); + $name = $this->getIdentifier()->getName(); + + JEventDispatcher::getInstance()->trigger( + 'onContentPrepare', + ['com_pages.'.$name, &$content, &$params, 0] + ); + + $this->content = $content->text; + + $prepared = true; + } + + return $this->content; + } + + public function getContentType() + { + return 'text/html'; + } + + public function getHash() + { + return hash("crc32b", $this->getContent()); + } + + public function __toString() + { + return $this->getContent(); + } +} diff --git a/contrib/extensions/k2/model/entity/article.php b/contrib/extensions/k2/model/entity/article.php new file mode 100644 index 000000000..5b75c893e --- /dev/null +++ b/contrib/extensions/k2/model/entity/article.php @@ -0,0 +1,280 @@ + + * @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository + */ + +class ExtK2ModelEntityArticle extends ExtK2ModelEntityAbstract +{ + protected function _initialize(KObjectConfig $config) + { + $config->append([ + 'data' => [ + 'id' => '', + 'slug' => '', + 'name' => '', + 'title' => '', + 'summary' => '', + 'excerpt' => '', + 'text' => '', + 'category' => '', + 'tags' => '', + 'date' => 'now', + 'edited_date' => '', + 'published_date' => '', + 'archived_date' => '', + 'author' => '', + 'editor' => '', + 'image' => [ + 'url' => '', + 'alt' => '', + 'caption' => '', + ], + 'metadata' => [ + 'og:type' => 'article', + 'og:title' => null, + 'og:url' => null, + 'og:image' => null, + 'og:description' => null, + + //http://ogp.me/ns/article + 'article:published_time' => null, + 'article:modified_time' => null, + 'article:expiration_time' => null, + 'article:tag' => [], + ], + 'fields' => [], + 'parameters' => [], + 'impressions' => 0, + 'direction' => 'auto', + 'language' => 'en-GB', + ], + 'internal_properties' => [ 'excerpt'], + ]); + + parent::_initialize($config); + } + + public function getPropertyExcerpt() + { + $parts = preg_split('##i', $this->getContent(), 2); + + if(count($parts) > 1) { + $excerpt = $parts[0]; + } else { + $excerpt = ''; + } + + return $excerpt; + } + + public function getPropertyText() + { + $parts = preg_split('##i', $this->getContent(), 2); + + if(count($parts) > 1) { + $text = $parts[1]; + } else { + $text = $parts[0]; + } + + return $text; + } + + public function getPropertyImage() + { + //Normalize images + $image = array(); + + $hash = md5("Image".$this->id); + $path = '/media/k2/items/src/'.$hash.'.jpg'; + + if(file_exists(JPATH_ROOT.$path)) + { + $url = $this->getBasePath().$path; + $url = $this->getObject('lib:http.url')->setUrl($url); + + $image = [ + 'url' => $url, + 'alt' => '', + 'caption' => $this->_image_caption ?? '' + ]; + } + + return new ComPagesObjectConfig($image); + } + + public function getPropertyMetadata() + { + $metadata = parent::getPropertyMetadata(); + + if(!empty($this->published_date)) { + $metadata->set('article:published_time', (string) $this->published_date); + } + + if(!empty($this->edited_date)) { + $metadata->set('article:modified_time', (string) $this->edited_date); + } + + if(!empty($this->archived_date)) { + $metadata->set('article:expiration_time', (string) $this->archived_date); + } + + if(count($this->tags)) { + $metadata->set('article:tag', implode(',', $this->tags->toArray())); + } + + return $metadata; + } + + public function getPropertyAttachments() + { + //Get the single category + $attachments = $this->getObject('ext:k2.model.attachments') + ->article($this->id) + ->fetch(); + + return $attachments; + } + + public function setPropertyTags($value) + { + if($value) + { + if(is_string($value)) { + $value = explode(',', $value); + } + } + else $value = []; + + return new ComPagesObjectConfig($value); + } + + public function setPropertyCategory($value) + { + if($value) + { + //Get the single category + $value = $this->getObject('ext:k2.model.categories') + ->id($value) + ->fetch() + ->find($value); + } + + return $value; + } + + public function setPropertyEditedDate($value) + { + $date = null; + + if($value && $value != '0000-00-00 00:00:00') + { + if(is_integer($value)) { + $date = $this->getObject('date')->setTimestamp($value); + } else { + $date = $this->getObject('date', array('date' => trim($value))); + } + } + + return $date; + } + + public function setPropertyPublishedDate($value) + { + $date = null; + + if($value && $value != '0000-00-00 00:00:00') + { + if(is_integer($value)) { + $date = $this->getObject('date')->setTimestamp($value); + } else { + $date = $this->getObject('date', array('date' => trim($value))); + } + } + + return $date; + } + + public function setPropertyArchivedDate($value) + { + $date = null; + + if($value && $value != '0000-00-00 00:00:00') + { + if(is_integer($value)) { + $date = $this->getObject('date')->setTimestamp($value); + } else { + $date = $this->getObject('date', array('date' => trim($value))); + } + } + + return $date; + } + + public function setPropertyFields($value) + { + if($value && is_string($value)) { + $value = json_decode($value, true); + } + + if($value && !empty($value)) + { + //Get the single field + $fields = $this->getObject('ext:k2.model.fields') + ->id(array_column($value, 'id')) + ->fetch(); + + foreach($value as $v) + { + if($field = $fields->find($v['id'])) + { + //Set the value + $field->value = $v['value']; + } + } + } + else $fields = $this->getObject('ext:k2.model.fields')->create(); + + return $fields; + } + + public function setPropertyParameters($value) + { + if($value && is_string($value)) { + $value = json_decode($value, true); + } + + //$params = JComponentHelper::getParams('com_k2')->toArray(); + + $config = new ComPagesObjectConfig(/*$params*/); + $config->merge($value); // Override global params with specific params + + return $config; + } + + public function getEditor() + { + $user = $this->getObject('user.provider')->load($this->editor); + return $user; + } + + public function getImages() + { + $hash = md5("Image".$this->id); + $path = '/media/k2/items/cache/'.$hash; + + $images = [ + 'XS' => $path.'_XS.jpg', + 'S' => $path.'_S.jpg', + 'M' => $path.'_M.jpg', + 'L' => $path.'_L.jpg', + 'XL' => $path.'_XL.jpg', + ]; + + return $images; + } +} diff --git a/contrib/extensions/k2/model/entity/attachment.php b/contrib/extensions/k2/model/entity/attachment.php new file mode 100644 index 000000000..8ce26b2c8 --- /dev/null +++ b/contrib/extensions/k2/model/entity/attachment.php @@ -0,0 +1,49 @@ + + * @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository + */ + +class ExtK2ModelEntityAttachment extends ComPagesModelEntityItem +{ + protected function _initialize(KObjectConfig $config) + { + $config->append([ + 'data' => [ + 'id' => '', + 'title' => '', + 'url' => '', + 'alt' => '', + 'impressions' => 0, + ], + 'internal_properties' => [ 'id'], + 'base_path' => $this->getObject('request')->getBasePath(), + ]); + + parent::_initialize($config); + } + + public function setPropertyUrl($value) + { + if($value) + { + $path = '/media/k2/attachments/'.$value; + + if(file_exists(JPATH_ROOT.$path)) + { + $url = $this->getBasePath().$path; + $value = $this->getObject('lib:http.url')->setUrl($url); + } + } + + return $value; + } + + public function getBasePath() + { + return $this->getConfig()->base_path; + } +} diff --git a/contrib/extensions/k2/model/entity/category.php b/contrib/extensions/k2/model/entity/category.php new file mode 100644 index 000000000..c5c9a005b --- /dev/null +++ b/contrib/extensions/k2/model/entity/category.php @@ -0,0 +1,106 @@ + + * @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository + */ + +class ExtK2ModelEntityCategory extends ExtK2ModelEntityAbstract +{ + protected function _initialize(KObjectConfig $config) + { + $config->append([ + 'data' => [ + 'id' => '', + 'slug' => '', + 'name' => '', + 'title' => '', + 'summary' => '', + 'text' => '', + 'date' => 'now', + 'author' => '', + 'image' => [ + 'url' => '', + 'alt' => '', + 'caption' => '', + ], + 'metadata' => [ + 'og:type' => 'website', + 'og:title' => null, + 'og:url' => null, + 'og:image' => null, + 'og:description' => null, + ], + 'parameters' => [], + 'impressions' => null, + 'direction' => 'auto', + 'language' => 'en-GB', + ], + //metatags are used internally for calculating metadata through getter + 'internal_properties' => [], + ]); + + parent::_initialize($config); + } + + public function getPropertySummary() + { + return $this->parameters->catMetaDesc; + } + + public function getPropertyAuthor() + { + return $this->parameters->catMetaAuthor; + } + + public function getPropertyMetadata() + { + $metadata = parent::getPropertyMetadata(); + + if($robots = $this->parameters->get('catMetaRobots')) { + $metadata->robots = $robots; + } + + return $metadata; + } + + public function setPropertyImage($value) + { + $image = array(); + + if($value) + { + $path = '/media/k2/categories/'.$value; + + if(file_exists(JPATH_ROOT.$path)) + { + $url = $this->getBasePath().$path; + $url = $this->getObject('lib:http.url')->setUrl($url); + + $image = [ + 'url' => $url, + 'alt' => '', + 'caption' => '', + ]; + } + } + + return new ComPagesObjectConfig($image); + } + + public function setPropertyParameters($value) + { + if($value && is_string($value)) { + $value = json_decode($value, true); + } + + //$params = JComponentHelper::getParams('com_k2')->toArray(); + + $config = new ComPagesObjectConfig(/*$params*/); + $config->merge($value); // Override global params with article specific params + + return $config; + } +} diff --git a/contrib/extensions/k2/model/entity/field.php b/contrib/extensions/k2/model/entity/field.php new file mode 100644 index 000000000..dbe5f0bf6 --- /dev/null +++ b/contrib/extensions/k2/model/entity/field.php @@ -0,0 +1,44 @@ + + * @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository + */ + +class ExtK2ModelEntityField extends ComPagesModelEntityItem +{ + protected function _initialize(KObjectConfig $config) + { + $config->append([ + 'data' => [ + 'id' => '', + 'name' => '', + 'title' => '', + ], + ]); + + parent::_initialize($config); + } + + public function getPropertyName() + { + $value = null; + + if($this->_value) + { + if(is_string($this->_value)) { + $value = array_pop(json_decode($this->_value, true)); + } + + if($value['alias']) { + $value = $value['alias']; + } else { + $value = ucwords(str_replace(array('_', '-'), ' ', $this->title)); + } + } + + return $value; + } +} diff --git a/contrib/extensions/k2/model/fields.php b/contrib/extensions/k2/model/fields.php new file mode 100644 index 000000000..4ef717b06 --- /dev/null +++ b/contrib/extensions/k2/model/fields.php @@ -0,0 +1,86 @@ + + * @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository + */ + +class ExtK2ModelFields extends ComPagesModelDatabase +{ + public function __construct(KObjectConfig $config) + { + parent::__construct($config); + + $this->getState() + ->insertUnique('id', 'cmd') + ->insert('group' , 'int') + ->insert('published' , 'boolean'); + } + + protected function _initialize(KObjectConfig $config) + { + $config->append(array( + 'persistable' => false, + 'type' => 'article_fields', + 'entity' => 'field', + 'table' => 'k2_extra_fields', + )); + parent::_initialize($config); + } + + public function getQuery($columns = true) + { + $state = $this->getState(); + + $query = $this->getObject('database.query.select') + ->table(array('tbl' => $this->getTable()->getName())); + + if($columns) + { + $query->columns([ + 'id' => 'tbl.id', + 'title' => 'tbl.name', + 'type' => 'tbl.type', + 'published' => 'tbl.published', + + //Protected properties (for getters) + '_value' => 'tbl.value', + ]); + } + + if(!is_null($state->id)) + { + if(is_string($state->id)) { + $fields = array_unique(explode(',', $state->id)); + } else { + $fields = (array) $state->id; + } + + $query->where('(tbl.id IN :fields)')->bind(['fields' => $fields]); + } + + if(!is_null($state->group)) + { + if(is_string($state->group)) { + $groups = array_unique(explode(',', $state->group)); + } else { + $groups = (array) $state->group; + } + + $query->where('(tbl.group IN :groups)')->bind(['groups' => $groups]); + } + + if (!is_null($state->published)) + { + if($state->published) { + $query->where('(tbl.published = 1)'); + } else { + $query->where('(tbl.published = 0)'); + } + } + + return $query; + } +} diff --git a/contrib/extensions/k2/template/function/k2_article.php b/contrib/extensions/k2/template/function/k2_article.php new file mode 100644 index 000000000..7f0859277 --- /dev/null +++ b/contrib/extensions/k2/template/function/k2_article.php @@ -0,0 +1,27 @@ + + * @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository + */ + +return function($id) +{ + static $entities; + + if(!isset($entities[$id])) + { + //Check if an alias exists + $id = $this->getObject('ext:k2.model.articles')->getAliases()->get($id, $id); + + if(is_numeric($id)) { + $entities[$id] = $this->getObject('ext:k2.model.articles')->id($id)->fetch(); + } else { + $entities[$id] = $this->getObject('ext:k2.model.articles')->slug($id)->fetch(); + } + } + + return $entities[$id]; +};