Current File : /home/pacjaorg/public_html/kmm/administrator/components/com_djclassifieds/models/category.php
<?php
/**
 * @package DJ-Classifieds
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email: contact@dj-extensions.com
 */

defined('_JEXEC') or die;

class DJClassifiedsModelCategory extends DJClassifiedsAdminModel
{
	protected $form_name = 'category';

	public function __construct($config = array())
	{
		$config['event_after_save'] = 'onDJClassifiedsModelAdminAfterSave';
		$config['event_after_delete'] = 'onDJClassifiedsModelAdminAfterDelete';
		parent::__construct($config);
	}

	public function getTable($type = 'Categories', $prefix = 'DJClassifiedsTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}

	public function getForm($data = array(), $loadData = true)
	{
		// Get the form.
		$form = $this->loadForm('com_djclassifieds.'.$this->form_name, $this->form_name, array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form)) {
			return false;
		}

		return $form;
	}

	protected function loadFormData()
	{
		$app = JFactory::getApplication();
		$data = JFactory::getApplication()->getUserState('com_djclassifieds.edit.'.$this->form_name.'.data', array());

		if (empty($data)) {
			$data = $this->getItem();

			$fields = $this->getFieldsXref($data->id);
			$subform_arr = array();
			$active_by_default = array();
			foreach($fields as $key => $f){
				$subform_arr['custom_fields'.$key] = array(
					'id' => $f->id,
					'name' => $f->name,
					'label' => $f->label,
					'type' => $f->type,
					'active' => $f->active
				);

				if(!$data->id && $f->all_cats){
					$subform_arr['custom_fields'.$key]['active'] = 1;
					$active_by_default['custom_fields'.$key] = $subform_arr['custom_fields'.$key];
					unset($subform_arr['custom_fields'.$key]);
				}
			}
			if($active_by_default){
				$subform_arr = $active_by_default + $subform_arr;
			}
			$data->set('custom_fields', $subform_arr);

			if($data->id){
				$data->usergroup_access = $this->getUgAccess($data->id);
			}else{
				$data->parent_id = $app->getUserState('djcf.last_cat_parent_id', 0);
			}

			$data->schema_type = $data->schema_type ? $data->schema_type : 'Offer';

			$data->price = $data->price/100;

			$data->icon = $this->getCatImage();
		}

		return $data;
	}

	protected function prepareTable($table)
	{
		$app = JFactory::getApplication();
		$db = JFactory::getDBO();
		$par = JComponentHelper::getParams('com_djclassifieds');

		$app->triggerEvent('onBeforeAdminDJClassifiedsSaveCategory', array(&$table, &$par));

		$table->price = $table->price * 100;

		if($table->alias){
			$table->alias = DJClassifiedsSEO::getAliasName($table->alias);
		}else{
			$table->alias = DJClassifiedsSEO::getAliasName($table->name);
		}

		if(!empty($app->input->getArray()['jform']['usergroup_access'])){
			$table->access = 1;
		}else{
			$table->access = 0;
		}

		if($table->published){
			DJClassifiedsCategory::publishParents(array($table->id));
		}else{
			DJClassifiedsCategory::unpublishChildren(array($table->id));
		}

		$table->rev_group_id = $table->rev_group_id ? $table->rev_group_id : 0;

		if(!$table->ordering){
			$query = "SELECT ordering FROM #__djcf_categories ORDER BY ordering DESC LIMIT 1";
			$db->setQuery($query);
			$ordering = $db->loadResult();
			$table->ordering = $ordering + 1;
		}
	}
   
	protected function getReorderConditions($table)
	{
		$condition = array();
		$condition[] = 'parent_id = '.(int) $table->parent_id;
		return $condition;
	}

	function getFieldsXref($cat_id)
	{
		$cat_id = $cat_id ? $cat_id : '0';

		$db = JFactory::getDBO();
		$query = "SELECT f.*, IF((fx.id IS NULL), 0, 1) as active "
		."FROM #__djcf_fields f "
		."LEFT JOIN #__djcf_fields_xref as fx "
		."ON fx.field_id=f.id AND fx.cat_id=".$cat_id." "
		."WHERE f.published=1 AND f.source=0 ORDER BY active DESC, fx.ordering, f.ordering";

		$db->setQuery($query);
		$fieldsxref = $db->loadObjectList();

		return $fieldsxref;
	}

	private function getUgAccess($cat_id)
	{
		$db = JFactory::getDBO();			
		$query = "SELECT group_id FROM #__djcf_categories_groups WHERE cat_id=".$cat_id;
		$db->setQuery($query);
		$ug_arr = $db->loadColumn();

		return $ug_arr;
	}

	function getCatImage()
	{
		$db = JFactory::getDBO();
		$app = JFactory::getApplication();
		$cat_id = $app->input->get('id', '0');

		$query = "SELECT * FROM #__djcf_images WHERE item_id=".$cat_id." AND type='category'";
		$db->setQuery($query);
		$cat_img = $db->loadObject();

		if($cat_img){
			$thumb_path = $cat_img->path.$cat_img->name.'_ths.'.$cat_img->ext;
			$cat_img->image_path = file_exists(JPATH_ROOT.$thumb_path) ? $thumb_path : $cat_img->path.$cat_img->name.'.'.$cat_img->ext;
		}

		return $cat_img;
	}

}
Site is undergoing maintenance

PACJA Events

Maintenance mode is on

Site will be available soon. Thank you for your patience!