setId($id); } /** * Reset the model ID and data */ public function setId($id=0) { $this->_id = $id; $this->_attachment = null; } /** * Load the attachment data * * @return true if loaded successfully */ private function _loadAttachment() { if ($this->_id == 0) { return false; } if ( empty($this->_attachment) ) { $user = JFactory::getUser(); $user_levels = $user->getAuthorisedViewLevels(); // If the user is not logged in, add extra view levels (if configured) if ( $user->get('username') == '' ) { // Get the component parameters jimport('joomla.application.component.helper'); $params = JComponentHelper::getParams('com_attachments'); // Add the specified access levels $guest_levels = $params->get('show_guest_access_levels', Array('1')); if (is_array($guest_levels)) { foreach ($guest_levels as $glevel) { $user_levels[] = $glevel; } } else { $user_levels[] = $glevel; } } $user_levels = implode(',', array_unique($user_levels)); // Load the attachment data and make sure this user has access $db = $this->getDbo(); $query = $db->getQuery(true); $query->select('a.*, a.id as id'); $query->from('#__attachments as a'); $query->where('a.id = '.(int)$this->_id); if ( !$user->authorise('core.admin') ) { $query->where('a.access in ('.$user_levels.')'); } $db->setQuery($query, 0, 1); $this->_attachment = $db->loadObject(); if ( empty($this->_attachment) ) { return false; } // Retrieve the information about the parent $parent_type = $this->_attachment->parent_type; $parent_entity = $this->_attachment->parent_entity; JPluginHelper::importPlugin('attachments'); $apm = getAttachmentsPluginManager(); if ( !$apm->attachmentsPluginInstalled($parent_type) ) { $this->_attachment->parent_type = false; return false; } $parent = $apm->getAttachmentsPlugin($parent_type); // Set up the parent info $parent_id = $this->_attachment->parent_id; $this->_attachment->parent_title = $parent->getTitle($parent_id, $parent_entity); $this->_attachment->parent_published = $parent->isParentPublished($parent_id, $parent_entity); } return true; } /** * Create a new Attachment object */ private function _initAttachment() { echo "_initData not implemented yet
"; return null; } /** * Get the data * * @return object */ public function getAttachment() { if ( !$this->_loadAttachment() ) { // If the load fails, create a new one $this->_initAttachment(); } return $this->_attachment; } /** * Save the attachment * * @param object $data mixed object or associative array of data to save * * @return Boolean true on success */ public function save($data) { // Get the table $table = $this->getTable('Attachments'); // Save the data if ( !$table->save($data) ) { // An error occured, save the model error message $this->setError($table->getError()); return false; } return true; } /** * Increment the download cout * * @param int $attachment_id the attachment ID */ public function incrementDownloadCount() { // Update the download count $db = JFactory::getDBO(); $query = $db->getQuery(true); $query->update('#__attachments')->set('download_count = (download_count + 1)'); $query->where('id = ' .(int)$this->_id); $db->setQuery($query); if ( !$db->query() ) { $errmsg = $db->stderr() . ' (ERR 49)'; JError::raiseError(500, $errmsg); } } }