Current Path :
/
home
/
develito
/
public_html
/
components
/
com_abook
/
models
/
Or
Select Your Path :
Upload File :
New :
File
Dir
/home/develito/public_html/components/com_abook/models/book.php
<?php /** * @package Joomla * @subpackage Abook * @copyright (C) 2010-2019 Federica Ugolotti * @license GNU/GPL, see LICENSE.php * Abook is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License 3 * as published by the Free Software Foundation. * Abook is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with Abook; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ defined( '_JEXEC' ) or die( 'Restricted access' ); jimport('joomla.application.component.modelitem'); jimport('joomla.application.categories'); use Joomla\Registry\Registry; class AbookModelBook extends JModelItem { protected $_context = 'com_abook.book'; //private $_parent = null; private $_categories = null; protected $_lend; protected function populateState() { $app = JFactory::getApplication('site'); // Load state from the request. $pk = $app->input->getInt('id'); $this->setState('book.id', $pk); $offset = $app->input->getUInt('limitstart'); $this->setState('list.offset', $offset); $user = JFactory::getUser(); // If $pk is set then authorise on complete asset, else on component only $asset = empty($pk) ? 'com_abook' : 'com_abook.book.' . $pk; if ((!$user->authorise('core.edit.state', $asset)) && (!$user->authorise('core.edit', $asset))) { $this->setState('filter.published', 1); $this->setState('filter.archived', 2); } // Load the parameters dalla configurazione globale. $params = $app->getParams(); $this->setState('params', $params); } public function getCategories() { if(!is_array($this->_categories)) { $app = JFactory::getApplication(); $menu = $app->getMenu(); $active = $menu->getActive(); $params = new JRegistry(); if($active) { $params->loadString($active->params); } $options = array(); $options['countItems'] = $params->get('show_item_count', 0) || !$params->get('show_empty_categories', 0); $categories = JCategories::getInstance('Abook', $options); $this->_parent = $categories->get($this->getState('filter.parentId')); if(is_object($this->_parent)) { $this->_categories = $this->_parent->getChildren(); } else { $this->_categories = false; } } return $this->_categories; } public function getCategory() { $categories = JCategories::getInstance('Abook'); $this->_category = $categories->get($this->_item[$this->getState('book.id')]->catid); return $this->_category; } public function getParent() { if(!is_object($this->_parent)) { $this->getCategories(); } return $this->_parent; } public function getAuthors($pk = null) { $pk = (!empty($pk)) ? $pk : (int) $this->getState('book.id'); if ($this->_item === null) { $this->_authors[$pk] = array(); } if (!isset($this->_authors[$pk])) { try { $db = $this->getDbo(); $query = $db->getQuery(true); $query->select($this->getState('item.select', 'b.*')); $query->from('#__abbookauth AS b'); // Join on authors table. if ($this->state->params->get('name_display', 0)==1){ $query->select('CONCAT(a.name, " ", a.lastname) AS author_name, a.id AS author_id, a.alias'); }else{ $query->select('CONCAT(a.lastname, " ", a.name) AS author_name, a.id AS author_id, a.alias'); } $query->join('LEFT', '#__abauthor AS a on a.id = b.idauth'); $query->where('b.idbook = ' . (int) $pk); $query->order($this->_item[$pk]->params->get('author_order', 'a.lastname ASC')); $db->setQuery($query); try { $data = $db->loadObjectList(); } catch (RuntimeException $e) { JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error'); return false; } $this->_authors[$pk] = $data; } catch (JException $e) { $this->setError($e); $this->_authors[$pk] = false; } } return $this->_authors[$pk]; } public function getTags($pk = null) { $pk = (!empty($pk)) ? $pk : (int) $this->getState('book.id'); if ($this->_item === null) { $this->_tags = array(); } if (!isset($this->_tags[$pk])) { try { $db = $this->getDbo(); $query = $db->getQuery(true); $query->select($this->getState('item.select', 'ab.*, 1 AS access, a.name AS title, "" AS params')); $query->from('#__abbooktag AS ab'); // Join on authors table. $query->select('a.name AS tag_name, a.id AS tag_id, a.alias'); $query->join('LEFT', '#__abtag AS a on a.id = ab.idtag'); $query->where('ab.idbook = ' . (int) $pk); $query->where('a.language in (' . $db->Quote(JFactory::getLanguage()->getTag()) . ',' . $db->Quote('*') . ')'); $db->setQuery($query); $data = $db->loadObjectList(); if ($error = $db->getErrorMsg()) { throw new Exception($error); } $this->_tags[$pk] = $data; } catch (JException $e) { $this->setError($e); $this->_tags[$pk] = false; } } return $this->_tags[$pk]; } public function getItem($pk = null) { // Initialise variables. $pk = (!empty($pk)) ? $pk : (int) $this->getState('book.id'); if ($this->_item === null) { $this->_item = array(); } if (!isset($this->_item[$pk])) { try { $db = $this->getDbo(); $query = $db->getQuery(true); $query->select($this->getState('item.select', 'a.*')); $query->from('#__abbook AS a'); // Join on category table. $query->select('c.title AS category_title, c.alias AS category_alias, c.access AS category_access'); $query->join('LEFT', '#__abcategories AS c on c.id = a.catid'); // Join on location table. $query->select('l.name AS location_name'); $query->join('LEFT', '#__ablocations AS l on l.id = a.idlocation '); // Join on editor table. $query->select('e.name AS editor_name'); $query->join('LEFT', '#__abeditor AS e on e.id = a.ideditor '); // Join on library table. $query->select('li.name AS library_name'); $query->join('LEFT', '#__ablibrary AS li on li.id = a.idlibrary '); // Join on location table. $query->select('lo.name AS location_name'); $query->join('LEFT', '#__ablocations AS lo on lo.id = a.idlocation '); // Join on user table. $query->select('u.name AS written_by'); $query->join('LEFT', '#__users AS u on u.id = a.user_id'); // Join over the categories to get parent category titles $query->select('parent.title as parent_title, parent.id as parent_id, parent.path as parent_route, parent.alias as parent_alias'); $query->join('LEFT', '#__abcategories as parent ON parent.id = c.parent_id'); $query->where('a.id = ' . (int) $pk); // Filter by published state. $published = $this->getState('filter.published'); $archived = $this->getState('filter.archived'); if (is_numeric($published)) { $query->where('(a.state = ' . (int) $published . ' OR a.state =' . (int) $archived . ')'); } $db->setQuery($query); $data = $db->loadObject(); if ($error = $db->getErrorMsg()) { throw new Exception($error); } if (empty($data)) { throw new Exception(JText::_('COM_ABOOK_ERROR_BOOK_NOT_FOUND'), 404); } // Check for published state if filter set. if (((is_numeric($published)) || (is_numeric($archived))) && (($data->state != $published) && ($data->state != $archived))) { throw new Exception(JText::_('COM_ABOOK_ERROR_BOOK_NOT_FOUND'), 404); } $globalParams = JComponentHelper::getParams('com_abook', true); //book params $bookParams = new Registry($data->params); //item params $data->params = clone $this->getState('params'); // Create an array of just the params set to 'use_book' $menuParamsArray = $this->getState('params')->toArray(); $bookArray = array(); foreach ($menuParamsArray as $key => $value) { if ($value === 'use_book') { // If the book has a value, use it if ($bookParams->get($key) != '') { // Get the value from the book $bookArray[$key] = $bookParams->get($key); } else { // Otherwise, use the global value $bookArray[$key] = $globalParams->get($key); } } } // Merge the selected article params if (count($bookArray) > 0){ $bookParams = new Registry($bookArray); $data->params->merge($bookParams); } // $data->params->merge($registry); $data->metadata = new Registry($data->metadata); $this->_item[$pk] = $data; } catch (JException $e) { $this->setError($e); $this->_item[$pk] = false; } } return $this->_item[$pk]; } function getVoting() { if (empty($this->_voting)){ $db = $this->getDbo(); $query = $db->getQuery(true); $query->select('ROUND( v.rating_sum / v.rating_count, 0 ) AS rating, v.rating_count'); $query->from('#__abbook AS a'); $query->leftjoin('#__abrating AS v ON a.id = v.book_id'); $query->where('a.id='. $db->escape((int) $this->getState('book.id'))); $db->setQuery($query); $this->_voting = $db->loadAssoc(); } return $this->_voting; } public function getTable($type = 'Book', $prefix = 'AbookTable', $config = array()) { return JTable::getInstance($type, $prefix, $config); } public function storeVote($pk = 0, $rate = 0) { if ( $rate >= 1 && $rate <= 5 && $pk > 0 ) { $userIP = $_SERVER['REMOTE_ADDR']; $db = $this->getDbo(); $db->setQuery( 'SELECT *' . ' FROM #__abrating' . ' WHERE book_id = '.$db->escape((int) $pk) ); $rating = $db->loadObject(); if (!$rating){ // There are no ratings yet, so lets insert our rating $db->setQuery( 'INSERT INTO #__abrating ( book_id, lastip, rating_sum, rating_count )' . ' VALUES ( '.(int) $pk.', '.$db->Quote($userIP).', '.(int) $rate.', 1 )' ); if (!$db->query()) { $this->setError($db->getErrorMsg()); return false; } } else { if ($userIP != ($rating->lastip)){ $db->setQuery( 'UPDATE #__abrating' . ' SET rating_count = rating_count + 1, rating_sum = rating_sum + '.(int) $rate.', lastip = '.$db->Quote($userIP) . ' WHERE book_id = '.$db->escape((int) $pk) ); if (!$db->query()) { $this->setError($db->getErrorMsg()); return false; } } else { return false; } } return true; } JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_CONTENT_INVALID_RATING', $rate), 'warning'); return false; } //incrementare il numero delle visite public function hit($id = null) { $input = JFactory::getApplication()->input; $hitcount = $input->getInt('hitcount', 1); if ($hitcount){ if (empty($id)) { $id = $this->getState('book.id'); } $book = $this->getTable(); $book->hit($id); } return true; } public function getLend() { if (!$this->_lend){ $db = $this->getDbo(); $query = $db->getQuery(true); $query->select('sum(CASE WHEN state = 0 THEN 1 ELSE 0 END) AS lent, sum(CASE WHEN state = 2 THEN 1 ELSE 0 END) AS requested'); $query->from('#__ablend'); $query->where('book_id='.$db->escape($this->getState('book.id'))); $query->where('state IN (0,2)'); $query->group('book_id'); $db->setQuery($query); $this->_lend= $db->loadObject(); if (!empty($this->_lend)){ if (($this->_lend->lent + $this->_lend->requested) >= $this->_item[$this->getState('book.id')]->qty){ if ($this->_lend->requested> 0 && $this->_lend->requested <= $this->_item[$this->getState('book.id')]->qty){ //requested $this->_lend->state=2; }elseif($this->_lend->lent <= $this->_item[$this->getState('book.id')]->qty){ //lent $this->_lend->state = 0; } } else{ if ($this->_lend->requested <= $this->_item[$this->getState('book.id')]->qty){ $this->_lend->state=2; }elseif($this->_lend->lent <= $this->_item[$this->getState('book.id')]->qty){ $this->_lend->state = 1; } $this->_lend->state = 1; } } } return $this->_lend; } public function lendRequest($bookid, $lend_out, $lend_in, $name='', $email=""){ if ( $bookid > 0 && $lend_out != '' && $lend_in != '' ) { $user = JFactory::getUser(); $db = $this->getDbo(); if (isset($this->_lend) && $this->_lend->state!=1){ $this->setError(JText::_('COM_ABOOK_LEND_NOT_AVAILABLE')); return false; } if(!$user->guest){ $query = $db->getQuery(true); $query->select('*'); $query->from('#__ablend'); $query->where('book_id='.$bookid.' AND user_id='.$user->id); $query->where('state IN (0,2)'); $db->setQuery($query); $old_request = $db->loadObject(); }else{ $query = $db->getQuery(true); $query->select('*'); $query->from('#__ablend'); $query->where('book_id='.$bookid.' AND custom_user_email="'.$email.'"'); $query->where('state IN (0,2)'); $db->setQuery($query); $old_request = $db->loadObject(); } if (!$old_request){ $request = new stdClass(); $request->book_id=$bookid; if (!$user->guest){ $request->user_id = $user->id; }else{ $request->custom_user_name = $name; $request->custom_user_email = $email; } $request->state=2; $request->admin_id=$user->id; $dateinsert=JFactory::getDate(); $request->dateinsert=$dateinsert->toSql(); $request->lend_out=$lend_out; $request->lend_in=$lend_in; $result = JFactory::getDbo()->insertObject('#__ablend', $request); if (!$result) { $this->setError($db->getErrorMsg()); return false; } JPluginHelper::importPlugin('abook'); $dispatcher = JDispatcher::getInstance(); $results = $dispatcher->trigger( 'onAbookAfterAction', array('com_abook.book',&$request)); if (count($results) && in_array(false, $results, true)){ $this->setError($dispatcher->getError()); $this->data = false; } return true; } else { $this->setError(JText::_('COM_ABOOK_REDUNDANT_LEND_REQUEST')); return false; } } JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_ABOOK_INVALID_LEND_REQUEST', $bookid), 'warning'); return false; } }