Skip to content

Instantly share code, notes, and snippets.

@joomdonation
Created June 17, 2014 14:45
Show Gist options
  • Select an option

  • Save joomdonation/02790230063029b407eb to your computer and use it in GitHub Desktop.

Select an option

Save joomdonation/02790230063029b407eb to your computer and use it in GitHub Desktop.
EDocman category table class
<?php
/**
* @version 1.5.7
* @package Joomla
* @subpackage Edocman
* @author Tuan Pham Ngoc
* @copyright Copyright (C) 2011 - 2013 Ossolution Team
* @license GNU/GPL, see LICENSE.php
*/
// No direct access
defined('_JEXEC') or die;
/**
* Category Table class
*/
class EdocmanTablecategory extends JTable
{
/**
* Constructor
*
* @param JDatabase A database connector object
*/
public function __construct(&$db)
{
parent::__construct('#__edocman_categories', 'id', $db);
}
/**
* Method to return name to use for asset table
* @return string
*/
protected function _getAssetName()
{
return 'com_edocman.category.'.(int) $this->id;
}
/**
* Method to return the title to use for the asset table.
*
* @return string
*
*/
protected function _getAssetTitle()
{
return $this->title;
}
/**
* Get the parent asset id for the record
*
* @param JTable $table A JTable object for the asset parent.
* @param integer $id The id for the asset
*
* @return integer The id of the asset's parent
*
*/
public function _getAssetParentId(JTable $table = null, $id = null)
{
// Initialise variables.
$assetId = null;
$db = $this->getDbo();
// This is a category under a category.
if ($this->parent_id > 0) {
// Build the query to get the asset id for the parent category.
$query = $db->getQuery(true);
$query->select('asset_id');
$query->from('#__edocman_categories');
$query->where('id = '.(int) $this->parent_id);
// Get the asset id from the database.
$db->setQuery($query);
if ($result = $db->loadResult()) {
$assetId = (int) $result;
}
}
// This is a category that needs to parent with the extension.
elseif ($assetId === null) {
// Build the query to get the asset id for the parent category.
$query = $db->getQuery(true);
$query->select('id');
$query->from('#__assets');
$query->where('name = '.$db->quote('com_edocman'));
// Get the asset id from the database.
$db->setQuery($query);
if ($result = $db->loadResult()) {
$assetId = (int) $result;
}
}
// Return the asset id.
if ($assetId) {
return $assetId;
} else {
return parent::_getAssetParentId($table, $id);
}
}
/**
* Bind data from request into table object
* @see JTable::bind()
*/
public function bind($array, $ignore = '')
{
// Bind the rules.
if (isset($array['rules']) && is_array($array['rules'])) {
$rules = new JAccessRules($array['rules']);
$this->setRules($rules);
}
$ret = parent::bind($array, $ignore);
//We need to setup the ordering here
if (!$this->ordering)
{
$db = JFactory::getDbo();
$db->setQuery('SELECT MAX(ordering) FROM #__edocman_categories WHERE parent_id='.(int)$this->parent_id);
$this->ordering = (int) $db->loadResult() + 1;
}
return $ret;
}
/**
* Store the information into database
* @see JTable::store()
*/
public function store($updateNulls = false)
{
$date = JFactory::getDate();
$user = JFactory::getUser();
$config = EdocmanHelper::getConfig() ;
if ($this->id)
{
// Existing category
$this->modified_time = $date->toSql();
$this->modified_user_id = $user->get('id');
}
else
{
// New category
$this->created_time = $date->toSql();
$this->created_user_id = $user->get('id');
$db = JFactory::getDbo() ;
$sql = 'SELECT MAX(ordering) FROM #__edocman_categories WHERE parent_id='.(int)$this->parent_id ;
$db->setQuery($sql);
$this->ordering = (int) $db->loadResult() + 1 ;
}
if (!$this->alias)
{
$this->alias = JApplication::stringURLSafe($this->title) ;
}
if ($this->path)
{
$this->path = str_replace("\\", "/", $this->path) ;
$path = JString::strtolower($this->path) ;
$path = JFolder::makeSafe($path) ;
$this->path = $path ;
$path = $config->documents_path.'/'.$this->path ;
if (!JFolder::exists($path))
{
JFolder::create($path, 0755);
}
}
return parent::store($updateNulls);
}
/**
* Override delete function
* @see JTable::delete()
*/
function delete($pk = null) {
//Delete all documents belong this category
$db = JFactory::getDbo() ;
$query = $db->getQuery(true);
$query->delete('#__edocman_urls')
->where('object_name="category"')
->where('object_id='.$pk);
//Delete from relative table
$sql = 'DELETE FROM #__edocman_document_category WHERE category_id='.$pk;
$db->setQuery($sql) ;
$db->query();
//Check if the asset id for this record
$sql = 'SELECT asset_id FROM #__edocman_categories WHERE id='.$pk;
$db->setQuery($sql) ;
$assetId = $db->loadResult() ;
if ($assetId)
{
return parent::delete($pk);
}
else
{
$sql = 'DELETE FROM #__edocman_categories WHERE id='.$pk;
$db->setQuery($sql) ;
$db->query() ;
return true ;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment