Current File : /home/pacjaorg/public_html/km/administrator/components/com_djclassifieds/models/field.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('Restricted access');
class DJClassifiedsModelField extends DJClassifiedsAdminModel
{
protected $form_name = 'field';
public function __construct($config = array())
{
$config['event_after_save'] = 'onDJClassifiedsModelAdminAfterSave';
$config['event_after_delete'] = 'onDJClassifiedsModelAdminAfterDelete';
parent::__construct($config);
}
public function getTable($type = 'Fields', $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()
{
$data = JFactory::getApplication()->getUserState('com_djclassifieds.edit.'.$this->form_name.'.data', array());
if (empty($data)) {
$data = $this->getItem();
if($data->id){
$data->cat_id = $this->getCatIds($data->id);
foreach($data->params as $fieldname => $value){
$data->{'params_'.$fieldname} = $value;
}
$data->group_id = explode(',', $data->group_id);
}else{
$data->access = 1;
}
if(!$data->id || $data->type != 'link'){
$data->params_link_target = '_blank';
$data->params_link_rel = 'nofollow';
}
}
if(!$data->id){ // new field's source autoset based on fields filter
require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/models/fields.php');
$fields_model = new DjClassifiedsModelFields();
$fields_source = $fields_model->getUserStateFromRequest('com_djclassifieds.fields.filter.source', 'filter_source', '');
if($fields_source){
$data->source = $fields_source;
}
}
return $data;
}
public function getItem($pk = null)
{
$item = parent::getItem($pk);
if($item === false) {
return false;
}
return $item;
}
protected function prepareTable($table)
{
$app = JFactory::getApplication();
$db = JFactory::getDBO();
$jform = $app->input->getRaw('jform');
$table->name = $this->checkName($table);
$table->values = trim(preg_replace('/;[\r\n\s]+/', ';', $table->values));
$table->search_value1 = trim(preg_replace('/;[\r\n\s]+/', ';', $table->search_value1));
$table->search_value2 = trim(preg_replace('/;[\r\n\s]+/', ';', $table->search_value2));
if(!$table->source){
$table->source = 0;
}
$field_params = array();
foreach($jform as $fieldname => $value){
if(substr($fieldname, 0, 7) == 'params_'){
$field_params[str_replace('params_', '', $fieldname)] = $value;
}
}
if($table->type != 'image'){
unset($field_params['image_th_width']);
unset($field_params['image_th_height']);
unset($field_params['image_maxsize']);
}
if($table->type != 'link'){
unset($field_params['link_target']);
unset($field_params['link_rel']);
unset($field_params['link_scheme']);
unset($field_params['link_text']);
}
if($table->type != 'textarea'){
unset($field_params['textarea_newlines']);
}
if($table->type != 'checkbox'){
unset($field_params['values_separator']);
unset($field_params['show_in_modal']);
}
if(!in_array($table->type, array('inputbox', 'textarea', 'link'))){
unset($field_params['char_limit']);
}
if($table->search_type != 'checkbox'){
unset($field_params['search_show_in_modal']);
}
if($table->sortable != '1' || $table->all_cats == '1'){
unset($field_params['sortable_all_cats']);
}
if($table->type != 'inputbox'){
$table->input_type = '';
}
if($table->type != 'inputbox' || $table->input_type != 'email'){
$table->askform_cc = '0';
}
$table->params = json_encode($field_params);
$table->group_id = !empty($jform['group_id']) ? implode(',',$jform['group_id']) : '0';
if(!$table->ordering){
$query = "SELECT ordering FROM #__djcf_fields WHERE source=".$table->source." ORDER BY ordering DESC LIMIT 1";
$db->setQuery($query);
$ordering = $db->loadResult();
$table->ordering = $ordering + 1;
}
$table->profile_source = $table->profile_source ? $table->profile_source : 0;
}
protected function getReorderConditions($table)
{
$condition = array();
$condition[] = 'source = '.(int) $table->source;
return $condition;
}
private function checkName($table)
{
$app = JFactory::getApplication();
$db = JFactory::getDBO();
if(!$table->name){
$table->name = $table->label;
}
$table->name = strtolower(trim($table->name));
$table->name = str_ireplace(' ', '_', $table->name);
$table->name = preg_replace("/[^a-z0-9_]/", "", $table->name);
if(in_array($table->source, array('0','1')) && in_array($table->name, DJClassifiedsField::getOverwritableFieldNames())){
// allow the core name to overwrite the core field for category/contact field
}elseif(in_array($table->name, array('name', 'website', 'video', 'description', 'intro_desc', 'post_code', 'address', 'latitude', 'longitude', 'type_id', 'exp_days', 'metadesc', 'metakey', 'email', 'currency', 'price_negotiable', 'terms_and_conditions', 'privacy_policy', 'gdpr_agreement', 'preview_value', 'option', 'id', 'token', 'view', 'task', 'price', 'contact'))){
$app->enqueueMessage('Field name has been changed as it can\'t be the same as the name of the core field', 'warning');
$table->name = $table->name.'_1';
}
$name = $table->name;
$next_name = 1;
$query = "SELECT count(id) FROM #__djcf_fields WHERE name=".$db->q($name)." AND id!=".$table->id;
$db->setQuery($query);
$exist = $db->loadResult();
while($exist){
$name = $table->name.'_'.$next_name;
$query = "SELECT count(id) FROM #__djcf_fields WHERE name=".$db->q($name)." AND id!=".$table->id;
$db->setQuery($query);
$exist = $db->loadResult();
$next_name++;
}
return $name;
}
private function getCatIds($field_id)
{
$db = JFactory::getDBO();
$query = "SELECT cat_id FROM #__djcf_fields_xref WHERE field_id=".$field_id;
$db->setQuery($query);
$cat_ids = $db->loadColumn();
return $cat_ids;
}
}