Current Path :
/
home
/
develito
/
public_html
/
components
/
com_raforms
/
models
/
Or
Select Your Path :
Upload File :
New :
File
Dir
/home/develito/public_html/components/com_raforms/models/comments.php
<?php defined('_JEXEC') or die; jimport('joomla.application.component.modellist'); class RAformsModelComments extends JModelList { protected $_params; protected $_data; public function __construct($config = array()) { $app = JFactory::getApplication(); $this->_params = $app->getParams(); if (empty($config['filter_fields'])) { $config['filter_fields'] = array( 'a.datetime', 'a.datetime', ); } parent::__construct($config); } public function setParams($params = null) { $app =& JFactory::getApplication(); $this->_params = &$app->getParams(); if (!empty($params)) { $this->_params = $params; $this->formid = $this->_params->get('formid'); } } protected function populateState($ordering = null, $direction = null) { $app = JFactory::getApplication(); $search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search'); $this->setState('filter.search', $search); $ordering = $this->getUserStateFromRequest($this->context.'.list.ordering', 'list_ordering', $ordering, 'int'); $this->setState('list.ordering', $ordering); $direction = $this->getUserStateFromRequest($this->context.'.list.direction', 'list_ordering', $direction, 'int'); $this->setState('list.direction', $direction); // Load the parameters. //$this->setState('params', $this->_params); $this->setState('layout', JRequest::getCmd('layout')); parent::populateState('a.datetime', 'ASC'); } protected function getListQuery() { if (!isset($this->entryId) || empty($this->entryId)) { $sql = 'SELECT 1 LIMIT 0'; } else { JLoader::import( 'raform', JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_raforms' . DIRECTORY_SEPARATOR . 'models' ); $db = JFactory::getDbo(); $user = JFactory::getUser(); // Gather output format information $formModel = JModelLegacy::getInstance( 'RAform', 'RAformsModel' ); $formModel->setId($this->formid); $formData = &$formModel->getData(); $format = array ( 'comment-format' => $formData->config['comment-format'], 'comment-date-format' => $formData->config['comment-date-format'], 'comment-time-format' => $formData->config['comment-time-format'] ); $result = array(); $mapping = array ( '%j' => 'DATE_FORMAT(a.datetime, "' . $format['comment-date-format'] . ' ' . $format['comment-time-format'] . '") AS datetime', '%d' => 'DATE_FORMAT(a.datetime, "' . $format['comment-date-format'] . '") AS date', '%t' => 'DATE_FORMAT(a.datetime, "' . $format['comment-time-format'] . '") AS time', '%u' => 'b.username', '%U' => 'b.name', '%c' => 'a.comment', '%b' => 'CONCAT("<img src=\"' . JUri::root() . 'images/comprofiler/", d.avatar, "\" style=\"width: ' . $formData->config['comment-avatar-size'] . 'px; height: ' . $formData->config['comment-avatar-size'] . 'px;\"/>") AS avatar' ); $formData->config['comment-avatar-size'] = preg_replace('/[^0-9]/', '', $formData->config['comment-avatar-size']); if ($formData->config['comment-avatar-size'] >= 1 && $formData->config['comment-avatar-size'] <= 36) { $kunenaAvatar = 'CONCAT("<img src=\"' . JUri::root() . 'media/kunena/avatars/resized/size36/", c.avatar, "\" style=\"width: ' . $formData->config['comment-avatar-size'] . 'px; height: ' . $formData->config['comment-avatar-size'] . 'px;\"/>") AS avatar'; } elseif ($formData->config['comment-avatar-size'] >= 36 && $formData->config['comment-avatar-size'] < 200) { $kunenaAvatar = 'CONCAT("<img src=\"' . JUri::root() . 'media/kunena/avatars/resized/size72/", c.avatar, "\" style=\"width: ' . $formData->config['comment-avatar-size'] . 'px; height: ' . $formData->config['comment-avatar-size'] . 'px;\"/>") AS avatar'; } elseif ($formData->config['comment-avatar-size'] >= 200) { $kunenaAvatar = 'CONCAT("<img src=\"' . JUri::root() . 'media/kunena/avatars/resized/size200/", c.avatar, "\" style=\"width: ' . $formData->config['comment-avatar-size'] . 'px; height: ' . $formData->config['comment-avatar-size'] . 'px;\"/>") AS avatar'; } $mapping['%k'] = $kunenaAvatar; if ($user->id == 0) { if (isset($mapping['%k'])) { $mapping['%k'] = '"<img src=\"' . JUri::root() . 'components/com_raforms/views/fform/images/guest.png\" style=\"width: ' . $formData->config['comment-avatar-size'] . 'px; height: ' . $formData->config['comment-avatar-size'] . 'px;\"/>" as avatar'; } if (isset($mapping['%b'])) { $mapping['%b'] = '"<img src=\"' . JUri::root() . 'components/com_raforms/views/fform/images/guest.png\" style=\"width: ' . $formData->config['comment-avatar-size'] . 'px; height: ' . $formData->config['comment-avatar-size'] . 'px;\"/>" as avatar'; } } foreach (preg_split("/ /", $format['comment-format']) as $column) { if (array_key_exists($column, $mapping)) { $result[$column] = $mapping[$column]; } } if (!count($result)) { $result[] = '*'; } $this->format = $result; // Get items $sql = $db->getQuery(true); // Select the required fields from the table. $sql->select( $this->getState( 'list.select', 'a.id, a.user AS userid, ' . implode(',', $this->format) ) ); $sql->from('#__raforms_' . $this->formid .'_comments a'); if (array_key_exists('%u', $this->format) || array_key_exists('%U', $this->format)) { $sql->join('LEFT', '#__users b ON a.user = b.id'); } if (array_key_exists('%k', $this->format)) { $sql->join('LEFT', '#__kunena_users c ON a.user = c.userid'); } if (array_key_exists('%b', $this->format)) { $sql->join('LEFT', '#__comprofiler d ON a.user = d.id'); } if (!empty($this->currentCommentId) && empty($this->cId)) { $sql->where('a.id > ' . (int) $this->currentCommentId); $this->currentCommentId = null; } $sql->where('a.entryid = ' . (int) $this->entryId); $search = $this->getState('filter.search'); if (!empty($search)) { $search = $db->Quote('%'.$db->getEscaped($search, true).'%'); $sql->where('(a.comment LIKE '.$search.' OR b.username LIKE '.$search.')'); } if (!empty($this->cId)) { $sql->where('a.id = ' . (int) $this->cId); } // Add the list ordering clause. // $orderCol = $this->state->get('list.ordering'); // $orderDirn = $this->state->get('list.direction'); // $sql->order($db->getEscaped($orderCol.' '.$orderDirn)); $sql->order('datetime'); } return $sql; } public function getItems() { // Get a storage key. $store = $this->getStoreId(); // Try to load the data from internal storage. if (!empty($this->cache[$store])) { return $this->cache[$store]; } // Load the list items. $query = $this->_getListQuery(); //$items = $this->_getList($query, $this->getStart(), $this->getState('list.limit')); $items = $this->_getList($query, $this->getStart(), 0); // Check for a database error. if ($this->_db->getErrorNum()) { $this->setError($this->_db->getErrorMsg()); return false; } // Add the items to the internal cache. $this->cache[$store] = $items; return $this->cache[$store]; } public function getTotal() { // Get a storage key. $store = $this->getStoreId('getTotal'); // Try to load the data from internal storage. if (!empty($this->cache[$store])) { return $this->cache[$store]; } // Load the total. $query = $this->_getListQuery(); $total = (int) $this->_getListCount($query); // Check for a database error. if ($this->_db->getErrorNum()) { $this->setError($this->_db->getErrorMsg()); return false; } // Add the total to the internal cache. $this->cache[$store] = $total; return $this->cache[$store]; } public function setId($formid = null) { if ($formid == null) { $this->setError('RAFORMS_NO_FORMID_GIVEN'); return false; } $this->formid = $formid; } public function setEntryId($entryId = null) { if ($entryId == null) { $this->setError('RAFORMS_NO_ENTRYID_GIVEN'); return false; } $this->entryId = $entryId; } public function setCommentId($commentId = null) { $this->cId = ($commentId == null) ? 0 : $commentId; } public function addComment($comment = null) { $db = JFactory::getDbo(); $user = JFactory::getUser(); $table = $this->getTable('comments'); $table->setTableNameId($this->formid); $canDo = RAformsHelper::getActions($this->formid, 'comment'); if (!empty($this->cId)) { $old_data = $this->getItems(); if ($canDo->get('core.edit') || ($canDo->get('core.edit.own') && $user->id == $old_data[0]->userid)) { $data['id'] = $this->cId; } else { $this->setError('RAFORMS_NO_PERMISSION'); print JText::_('RAFORMS_NO_PERMISSION'); return false; } } elseif ($canDo->get('core.create')) { $data['entryid'] = $this->entryId; $data['datetime'] = strftime('%Y-%m-%d %H:%M:%S', time()); $data['user'] = (int) $user->id; } $data['comment'] = $comment; // Bind the form fields to the table if( ! $table->bind($data)) { return $this->_db->stderr(); } //-- Make sure the record is valid if( ! $table->check()) { return $this->_db->stderr(); } if (!$table->store()) { return $this->_db->getErrorMsg(); } // Get formatted comment $this->currentCommentId = (int) $table->id - 1; // avoid caching mechanisms when adding / editing comments $query = $this->_getListQuery(); $items = $this->_getList($query, $this->getStart(), 0); return $items; } public function deleteComment() { $db =& JFactory::getDbo(); $user =& JFactory::getUser(); $table =& $this->getTable('comments'); $table->setTableNameId($this->formid); $canDo = RAformsHelper::getActions($this->formid, 'comment'); if (empty($this->cId)) { $this->setError('RAFORMS_NO_COMMENTID_GIVEN'); print JText::_('RAFORMS_NO_COMMENTID_GIVEN'); return false; } else { $old_data = $this->getItems(); if (!$canDo->get('core.delete') && !($canDo->get('core.edit.own') && $user->id == $old_data[0]->userid)) { $this->setError('RAFORMS_NO_PERMISSION'); print JText::_('RAFORMS_NO_PERMISSION'); return false; } } // Bind the form fields to the table if( ! $table->delete($this->cId)) { $this->setError('RAFORMS_ERROR_COMMENT'); print JText::_('RAFORMS_ERROR_COMMENT'); return $this->_db->stderr(); } return true; } public function getComments($currentCommentId = 0) { $this->currentCommentId = $currentCommentId; return $this->getItems(); } }