Questions-and-Answers/site/models/questions_and_answers.php

205 lines
6.4 KiB
PHP
Raw Normal View History

2018-04-24 12:12:24 +00:00
<?php
/*--------------------------------------------------------------------------------------------------------| www.vdm.io |------/
__ __ _ _____ _ _ __ __ _ _ _
\ \ / / | | | __ \ | | | | | \/ | | | | | | |
\ \ / /_ _ ___| |_ | | | | _____ _____| | ___ _ __ _ __ ___ ___ _ __ | |_ | \ / | ___| |_| |__ ___ __| |
\ \/ / _` / __| __| | | | |/ _ \ \ / / _ \ |/ _ \| '_ \| '_ ` _ \ / _ \ '_ \| __| | |\/| |/ _ \ __| '_ \ / _ \ / _` |
\ / (_| \__ \ |_ | |__| | __/\ V / __/ | (_) | |_) | | | | | | __/ | | | |_ | | | | __/ |_| | | | (_) | (_| |
\/ \__,_|___/\__| |_____/ \___| \_/ \___|_|\___/| .__/|_| |_| |_|\___|_| |_|\__| |_| |_|\___|\__|_| |_|\___/ \__,_|
2018-12-27 11:06:27 +00:00
| |
|_|
2018-04-24 12:12:24 +00:00
/-------------------------------------------------------------------------------------------------------------------------------/
@version 1.0.x
2019-08-14 02:00:02 +00:00
@build 14th August, 2019
2018-04-24 12:12:24 +00:00
@created 30th January, 2017
@package Questions and Answers
@subpackage questions_and_answers.php
2018-12-27 11:06:27 +00:00
@author Llewellyn van der Merwe <https://www.vdm.io/>
2018-04-24 12:12:24 +00:00
@copyright Copyright (C) 2015. All Rights Reserved
2018-12-27 11:06:27 +00:00
@license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
Questions &amp; Answers
2018-04-24 12:12:24 +00:00
/-----------------------------------------------------------------------------------------------------------------------------*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* Questionsanswers Model for Questions_and_answers
*/
class QuestionsanswersModelQuestions_and_answers extends JModelList
{
/**
* Model user data.
*
* @var strings
*/
protected $user;
protected $userId;
protected $guest;
protected $groups;
protected $levels;
protected $app;
protected $input;
protected $uikitComp;
/**
* Method to build an SQL query to load the list data.
*
* @return string An SQL query
*/
protected function getListQuery()
{
// Get the current user for authorisation checks
$this->user = JFactory::getUser();
$this->userId = $this->user->get('id');
$this->guest = $this->user->get('guest');
$this->groups = $this->user->get('groups');
$this->authorisedGroups = $this->user->getAuthorisedGroups();
$this->levels = $this->user->getAuthorisedViewLevels();
$this->app = JFactory::getApplication();
$this->input = $this->app->input;
$this->initSet = true;
// Make sure all records load, since no pagination allowed.
$this->setState('list.limit', 0);
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
2019-06-12 20:58:33 +00:00
// Get from #__questionsanswers_question_and_answer as a
$query->select($db->quoteName(
array('a.id','a.catid'),
array('id','catid')));
$query->from($db->quoteName('#__questionsanswers_question_and_answer', 'a'));
2018-04-24 12:12:24 +00:00
// Filtering.
$catid = $this->input->get('catid', null);
$this->childrenButtonIDs = 'root';
if ($catid > 0)
{
if ($children = QuestionsanswersHelper::getVars('categories', $catid, 'parent_id', 'id', 'IN', ''))
{
// load the fist found children for buttons
$this->childrenButtonIDs = $children;
// set to bucket
$childrenBucket = array($children);
while ($children = QuestionsanswersHelper::getVars('categories', $children, 'parent_id', 'id', 'IN', ''))
{
$childrenBucket[] = $children;
}
$getAllCats = QuestionsanswersHelper::mergeArrays($childrenBucket);
// load main category
$getAllCats[] = $catid;
// load children category if set
$query->where('a.catid IN (' . implode(',',$getAllCats) . ')');
}
else
{
unset($this->childrenButtonIDs);
// load main category if set
$query->where('a.catid = ' . (int) $catid);
}
}
// Get where a.published is 1
$query->where('a.published = 1');
// return the query object
return $query;
}
/**
* Method to get an array of data items.
*
* @return mixed An array of data items on success, false on failure.
*/
public function getItems()
{
2018-12-27 11:06:27 +00:00
$user = JFactory::getUser();
2018-04-24 12:12:24 +00:00
// load parent items
$items = parent::getItems();
// Get the global params
$globalParams = JComponentHelper::getParams('com_questionsanswers', true);
// Insure all item fields are adapted where needed.
if (QuestionsanswersHelper::checkArray($items))
{
foreach ($items as $nr => &$item)
{
// Always create a slug for sef URL's
$item->slug = (isset($item->alias) && isset($item->id)) ? $item->id.':'.$item->alias : $item->id;
}
2018-12-27 11:06:27 +00:00
}
2018-04-24 12:12:24 +00:00
// return items
return $items;
2018-12-27 11:06:27 +00:00
}
2018-04-24 12:12:24 +00:00
/**
2018-12-27 11:06:27 +00:00
* Get the uikit needed components
*
* @return mixed An array of objects on success.
*
*/
2018-04-24 12:12:24 +00:00
public function getUikitComp()
{
if (isset($this->uikitComp) && QuestionsanswersHelper::checkArray($this->uikitComp))
{
return $this->uikitComp;
}
return false;
2018-12-27 11:06:27 +00:00
}
2018-04-24 12:12:24 +00:00
public function getButtons()
{
// add back button
$this->addBackButton = true;
// load the children buttons
if (isset($this->childrenButtonIDs) && QuestionsanswersHelper::checkArray($this->childrenButtonIDs))
{
return QuestionsanswersHelper::getNames($this->childrenButtonIDs, 'catid');
}
// load the top parent buttons
elseif (isset($this->childrenButtonIDs) && QuestionsanswersHelper::checkString($this->childrenButtonIDs) && $this->childrenButtonIDs === 'root' && $parents = $this->getParentButtonIDs())
{
// don't add back button
$this->addBackButton = false;
return QuestionsanswersHelper::getNames($parents, 'catid');
}
return false;
}
public function getBackButton()
{
// set back button
return $this->addBackButton;
}
protected function getParentButtonIDs()
{
// get the db object
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query->select('a.id');
$query->from($db->quoteName('#__categories', 'a'));
$query->where('a.extension = ' . $db->quote('com_questionsanswers.questions_and_answers'));
$query->where('a.published = 1');
$query->where('a.parent_id = 1');
// set query
$db->setQuery($query);
$db->execute();
if ($db->getNumRows())
{
return $db->loadColumn();
}
return false;
2018-12-27 11:06:27 +00:00
}
2018-04-24 12:12:24 +00:00
}