Current File : /home/pacjaorg/public_html/kmm/administrator/components/com_djclassifieds/controllers/item.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 DJClassifiedsControllerItem extends JControllerForm
{
static private $_oldrow = null;
public function save($key = null, $urlVar = null)
{
$app = JFactory::getApplication();
$db = JFactory::getDBO();
$id = $app->input->getInt('id');
$query = "SELECT * FROM #__djcf_items WHERE id=".$id;
$db->setQuery($query);
self::$_oldrow = $db->loadObject();
return parent::save($key, $urlVar);
}
protected function postSaveHook(JModelLegacy $model, $validData = array())
{
$app = JFactory::getApplication();
$db = JFactory::getDBO();
$par = JComponentHelper::getParams('com_djclassifieds');
$date_now = JFactory::getDate()->toSQL();
$is_new = $app->input->getInt('id') ? false : true;
$item = $model->getItem();
// custom plugins support (saving additional columns with no jform wrapper)
$row = JTable::getInstance('Items', 'DJClassifiedsTable');
$row->load($item->id);
$row->bind($app->input->post->getArray());
$row->store();
DJClassifiedsImage::saveItemImages($item, $is_new);
if(!$is_new){
$query = "DELETE FROM #__djcf_fields_values WHERE item_id=".$item->id;
$db->setQuery($query);
$db->execute();
}
$mcat_ids = $app->input->get('mcat_ids', array(), 'ARRAY');
if($mcat_ids){
$mcats_list = '';
for($mi=0;$mi<count($mcat_ids);$mi++){
$mcats_list .= $mcat_ids[$mi].',';
}
$mcats_list .= $item->cat_id;
$mcat_where = ' IN ('.$mcats_list.')';
}else{
$mcat_where = ' = '.$item->cat_id.' ';
}
$query = "SELECT f.* "
."FROM #__djcf_fields f "
."INNER JOIN #__djcf_fields_xref fx ON f.id=fx.field_id WHERE f.source=0 AND fx.cat_id ".$mcat_where." "
."UNION "
."SELECT f.* FROM #__djcf_fields f WHERE f.source=1";
$db->setQuery($query);
$fields_list = $db->loadObjectList();
DJClassifiedsField::saveFieldsValues($fields_list, $item->id);
// promotions
foreach($validData['proms'] as $prom){
$old_active_prom = $model->getActivePromotionDays($item->id, $prom['prom_id']);
if(!$old_active_prom){
$old_active_prom = new stdClass();
$old_active_prom->days = null;
$old_active_prom->date_exp = null;
}
if($prom['prom_date_exp'] != $old_active_prom->date_exp || $prom['prom_days'] != $old_active_prom->days){
if($prom['prom_date_exp'] && $prom['prom_date_exp'] < $date_now){
continue;
}
if(!empty($old_active_prom->id)){
$query = "DELETE FROM #__djcf_items_promotions WHERE id=".$old_active_prom->id;
$db->setQuery($query);
$db->execute();
}
if(!$prom['prom_days']){ // no days - don't insert prom
continue;
}elseif($prom['prom_days'] != $old_active_prom->days){ // new/changed days - calc/recalc date_exp
$new_date_exp = JFactory::getDate('now +'.$prom['prom_days'].'day')->toSQL();
}else{ // same days, different exp_date
$new_date_exp = $prom['prom_date_exp'];
}
$query = "INSERT INTO #__djcf_items_promotions(`item_id`,`prom_id`,`date_start`,`date_exp`,`days`) VALUES (".$item->id.",".$prom['prom_id'].",".$db->q(JFactory::getDate()->toSQL()).",".$db->q($new_date_exp).",".$prom['prom_days'].")";
$db->setQuery($query);
$db->execute();
}
}
$query = "SELECT p.name "
."FROM #__djcf_items_promotions ip "
."INNER JOIN #__djcf_promotions p ON ip.prom_id=p.id "
."WHERE ip.item_id=".$item->id." AND ip.date_exp>".$db->q($date_now)." "
."ORDER BY ip.id";
$db->setQuery($query);
$new_proms_arr = $db->loadColumn();
$new_prom = $new_proms_arr ? implode(',', $new_proms_arr).',': '';
if(strstr($new_prom, 'p_first')){
$special = 1;
}else{
$special = 0;
}
$query = "UPDATE #__djcf_items SET promotions='".$new_prom."', special='".$special."' WHERE id=".$item->id;
$db->setQuery($query);
$db->execute();
// auction/bids
if(isset($validData['bids'])){
$bids_ids_arr = array(0);
foreach($validData['bids'] as $b){
$bids_ids_arr[] = $b['bid_id'];
}
$query = "SELECT * FROM #__djcf_auctions WHERE item_id=".$item->id." AND id NOT IN (".implode(',', $bids_ids_arr).")";
$db->setQuery($query);
$bids_to_delete = $db->loadObjectList();
foreach($bids_to_delete as $bid_d){
$query = "DELETE FROM #__djcf_auctions_assist WHERE item_id=".$bid_d->item_id." AND user_id=".$bid_d->user_id;
$db->setQuery($query);
$db->execute();
$query = "DELETE FROM #__djcf_auctions WHERE id=".$bid_d->id;
$db->setQuery($query);
$db->execute();
}
}
// buynow/orders
if(isset($validData['orders'])){
$orders_ids_arr = array(0);
foreach($validData['orders'] as $o){
$orders_ids_arr[] = $o['order_id'];
}
$query = "DELETE FROM #__djcf_orders WHERE item_id=".$item->id." AND id NOT IN (".implode(',', $orders_ids_arr).")";
$db->setQuery($query);
$db->execute();
}
// ask messages
if(isset($validData['askmessages'])){
$ask_ids_arr = array(0);
foreach($validData['askmessages'] as $am){
$ask_ids_arr[] = $am['askmessage_id'];
}
$query = "DELETE FROM #__djcf_itemsask WHERE item_id=".$item->id." AND id NOT IN (".implode(',', $ask_ids_arr).")";
$db->setQuery($query);
$db->execute();
}
// abuse reports
if(isset($validData['abusereports'])){
$abuse_ids_arr = array(0);
foreach($validData['abusereports'] as $ar){
$abuse_ids_arr[] = $ar['abusereport_id'];
}
$query = "DELETE FROM #__djcf_items_abuse WHERE item_id=".$item->id." AND id NOT IN (".implode(',', $abuse_ids_arr).")";
$db->setQuery($query);
$db->execute();
}
// other
if(self::$_oldrow && ($item->user_id || $item->email)){
if(self::$_oldrow->published != $item->published){
if($par->get('notify_status_change','0')=='2'){
DJClassifiedsNotify::notifyUserPublication($item->id, $item->published);
}
}
}
if(isset($validData['exp_days'])){
$exp_days = $validData['exp_days'];
$old_exp_days = self::$_oldrow ? self::$_oldrow->exp_days : '';
if($old_exp_days == '0' && self::$_oldrow->date_exp != '2038-01-01 00:00:00'){
$old_exp_days = ''; // 0 means empty here, not unlimited
}
if($exp_days !== '' && $old_exp_days != $exp_days){
if($exp_days == 0){
$date_exp = '2038-01-01 00:00:00';
}else{
//$date_exp = JFactory::getDate($item->date_start)->modify('+'.$exp_days.' day')->toSQL();
$date_exp = JFactory::getDate()->modify('+'.$exp_days.' day')->toSQL();
}
if($date_exp == '1970-01-01 1:00:00' || $date_exp > '2038-01-01 00:00:00'){
$date_exp = '2038-01-01 00:00:00';
}
$query = "UPDATE #__djcf_items SET date_exp=".$db->q($date_exp)." WHERE id=".$item->id;
$db->setQuery($query);
$db->execute();
$item->date_exp = $date_exp;
}
}
if(!$is_new){
if(JFactory::getDate(self::$_oldrow->date_exp)->format('Y-m-d') != JFactory::getDate($item->date_exp)->format('Y-m-d')){
$query = "UPDATE #__djcf_items SET notify=0 WHERE id=".$item->id;
$db->setQuery($query);
$db->execute();
}
}
$app->triggerEvent('onAfterAdminDJClassifiedsSaveAdvert', array($item, $is_new));
}
public function getFields()
{
header("Content-type: text/html; charset=utf-8");
$app = JFactory::getApplication();
$db = JFactory::getDBO();
$cid = $app->input->getInt('cat_id', 0);
$id = $app->input->getInt('id', 0);
$mcat_ids = $app->input->getStr('mcat_ids');
if($mcat_ids){
$mcat_ids .= $cid;
$cat_where = ' IN ('.$mcat_ids.')';
}else{
$cat_where = ' = '.$cid.' ';
}
$query = $db->getQuery(true);
$query->select(array('fv.*', 'f.*', 'fx.ordering'))
->from('#__djcf_fields f')
->join('INNER', '#__djcf_fields_xref fx ON f.id = fx.field_id AND fx.cat_id '.$cat_where)
->join('LEFT', '#__djcf_fields_values fv ON fv.field_id = fx.field_id AND fv.item_id = '.$id)
->where('f.source = 0')
->where('f.published = 1')
->where('f.name NOT IN ('.implode(',', DJClassifiedsField::getOverwritableFieldNames(true)).')')
->order('fx.cat_id, fx.ordering, f.ordering');
if($mcat_ids){
$query->group('fx.field_id');
}
$app->triggerEvent('onDJClassifiedsAdminGetFormFieldsQuery', array(&$query, 'item'));
$db->setQuery($query);
$fields_list = $db->loadObjectList();
foreach($fields_list as $f){
if($f->item_id === null){
DJClassifiedsField::setFieldDefaultValue($f);
}
echo DJClassifiedsField::renderAdminFormField($f, $id);
}
$app->close();
}
function getContactFields()
{
header("Content-type: text/html; charset=utf-8");
$app = JFactory::getApplication();
$db = JFactory::getDBO();
$id = $app->input->getInt('id', '0');
$query = $db->getQuery(true);
$query->select(array('fv.*', 'f.*'))
->from('#__djcf_fields f')
->join('LEFT', '#__djcf_fields_values fv ON fv.field_id = f.id AND fv.item_id = '.$id)
->where('f.source = 1')
->where('f.published = 1')
->where('f.name NOT IN ('.implode(',', DJClassifiedsField::getOverwritableFieldNames(true)).')')
->order('f.ordering');
$app->triggerEvent('onDJClassifiedsAdminGetFormFieldsQuery', array(&$query, 'item'));
$db->setQuery($query);
$fields_list = $db->loadObjectList();
foreach($fields_list as $f){
if($f->item_id === null){
DJClassifiedsField::setFieldDefaultValue($f);
}
echo DJClassifiedsField::renderAdminFormField($f, $id);
}
$app->close();
}
function sendMessage()
{
$app = JFactory::getApplication();
$config = JFactory::getConfig();
$mailto = $app->input->getVar('djmsg_email', '');
$subject = $app->input->getVar('djmsg_title', '');
$message = $app->input->get('djmsg_description', '', 'RAW');
$item_id = $app->input->getInt('djmsg_item_id', 0);
$mailfrom = $app->getCfg('mailfrom');
$fromname = $config->get('sitename');
$mailer = JFactory::getMailer();
$mailer->sendMail($mailfrom, $fromname, $mailto, $subject, $message, $mode=1);
$msg = JText::_('COM_DJCLASSIFIEDS_MESSAGE_SENT');
$link = 'index.php?option=com_djclassifieds&view=item&layout=edit&id='.$item_id;
$app->enqueueMessage($msg, 'success');
$app->redirect($link);
}
function download()
{
require_once(JPATH_ROOT.'/plugins/djclassifieds/files/helper.php');
$app = JFactory::getApplication();
$file_id = $app->input->getInt('fid', 0);
if(!DJClassifiedsFileHelper::getFile($file_id)){
throw new Exception('', 404);
return false;
}
$app->close();
}
function rotateImage()
{
$app = JFactory::getApplication();
$img_src = $app->input->getStr('img_src');
$filename = JPATH_ROOT.'/'.$img_src;
DJClassifiedsImage::rotate($filename, 1);
$app->close();
}
}
?>