function getLatestContents($nbcontents, $sort_type = 0, $catselect = "no", $rootid = null) { switch ($sort_type) { /* Popular */ case 2: $order_sql = "ORDER BY a.views DESC,a.date_created DESC ,a.id DESC "; break; /* Random */ /* Random */ case 1: $order_sql = "ORDER BY RAND() "; break; /* Latest */ /* Latest */ case 0: default: $order_sql = "ORDER BY a.date_created DESC ,a.id DESC "; break; } if (version_compare(JVERSION, '1.6', 'ge')) { $listCategories = TPermissions::getAuthorisedCategories('read'); //If the variable is an array and if it's not empty, we add a filter to the request //If not we're not return any category if (is_array($listCategories) && !empty($listCategories)) { $categories = implode(',', $listCategories); $listCategories = " AND c.id IN (" . $categories . ") "; } else { $listCategories = " AND 0 "; } } else { $listCategories = ""; } $cat_query = ""; switch ($catselect) { case "no": break; case "-1": $catid = JRequest::getInt('catid', 0); if ($catid != 0 && $catid != -1) { $this->_db->setQuery("SELECT c.id, c.name,c.parent " . " FROM #__adsmanager_categories as c " . " WHERE c.published = 1 {$listCategories} ORDER BY c.parent,c.ordering"); $listcats = $this->_db->loadObjectList(); //List $list = array(); $list[] = $catid; $this->_recurseSearch($listcats, $list, $catid); $listids = implode(',', $list); $cat_query = "adcat.catid IN ({$listids}) AND "; } break; default: $this->_db->setQuery("SELECT c.id, c.name,c.parent " . " FROM #__adsmanager_categories as c " . " WHERE c.published = 1 {$listCategories} ORDER BY c.parent,c.ordering"); $listcats = $this->_db->loadObjectList(); $catsid = explode(',', $catselect); //List $list = array(); foreach ($catsid as $catid) { $list[] = $catid; $this->_recurseSearch($listcats, $list, $catid); } $listids = implode(',', $list); $cat_query = " adcat.catid IN ({$listids}) AND "; break; } if (ADSMANAGER_SPECIAL == 'newspaper') { $cat_query .= " (a.ad_publishtype = 'both' OR a.ad_publishtype = 'online') AND "; } $currentSession = JSession::getInstance('none', array()); $sql = $currentSession->get("sqlglobalfilter", ""); $globalfilter = ""; if ($sql != "") { $globalfilter = " AND {$sql} "; } $sql = " SELECT a.*,p.id as parentid,p.name as parent,c.id as catid, c.name as cat,u.username as user " . " FROM #__adsmanager_ads as a " . " INNER JOIN #__adsmanager_adcat as adcat ON adcat.adid = a.id " . " LEFT JOIN #__users as u ON a.userid = u.id " . " INNER JOIN #__adsmanager_categories as c ON adcat.catid = c.id " . " LEFT JOIN #__adsmanager_categories as p ON c.parent = p.id " . " WHERE 1 {$globalfilter} AND {$cat_query} c.published = 1 and a.published = 1 GROUP BY a.id {$order_sql} LIMIT 0, {$nbcontents}"; if (function_exists("updateQuery")) { updateQuery($sql); } $this->_db->setQuery($sql); $contents = $this->_db->loadObjectList(); if ($contents == null) { $contents = array(); } foreach ($contents as &$content) { $content->images = @json_decode($content->images); $content->cat = JText::_($content->cat); if ($content->parent != "") { $content->parent = JText::_($content->parent); } if (!is_array($content->images)) { $content->images = array(); } } return $contents; }
function getCatTree($onlyPublished = true, $getnbads = false, &$nbcontents = null, $mode = 'admin', $rootid = 0) { if ($onlyPublished == true) { $published = " c.published = 1 "; } else { $published = " 1 "; } if (version_compare(JVERSION, '1.6', 'ge')) { //If $mode isn't read or write, we authorised all the categories if (!($mode == 'write' || $mode == 'read')) { $listCategories = ""; } else { $listCategories = TPermissions::getAuthorisedCategories($mode); //If the variable is an array and if it's not empty, we add a filter to the request //If not we're not return any category if (is_array($listCategories) && !empty($listCategories)) { $listCategories = " AND c.id IN (" . implode(',', $listCategories) . ") "; } else { $listCategories = " AND 0 "; } } } else { $listCategories = ""; } if ((int) $rootid != 0) { $list = $this->getAllSubCatids($rootid, false); $listCategories .= " AND c.id IN (" . implode(',', $list) . ")"; } $query = " SELECT c.* FROM #__adsmanager_categories as c WHERE {$published} {$listCategories} " . " ORDER BY c.parent,c.ordering,c.id"; $this->_db->setQuery($query); $list = $this->_db->loadObjectList(); if ($getnbads != null) { $query = " SELECT ac.catid,ac.adid " . " FROM #__adsmanager_adcat as ac" . " INNER JOIN #__adsmanager_ads as a ON a.id = ac.adid " . " WHERE a.published = 1 "; $this->_db->setQuery($query); $listads = $this->_db->loadObjectList(); $nbadsbycat = array(); foreach ($listads as $ad) { if (!isset($nbadsbycat[$ad->catid])) { $nbadsbycat[$ad->catid] = array(); } $nbadsbycat[$ad->catid][]++; } } // establish the hierarchy of the menu $tree = array(); // first pass - collect children if (isset($list)) { foreach ($list as $v) { $pt = $v->parent; $list_temp = @$tree[$pt] ? $tree[$pt] : array(); if (isset($nbadsbycat[$v->id])) { $v->num_ads = count($nbadsbycat[$v->id]); } else { $v->num_ads = 0; } $v->name = JText::_($v->name); array_push($list_temp, $v); $tree[$pt] = $list_temp; } } if ($getnbads != null) { $nbcontents = $this->calc_nb_ads(0, $tree); } return $tree; }
/** * Saves the content item an edit form submit * * @todo */ function save() { $app = JFactory::getApplication(); // Check for request forgeries JRequest::checkToken() or jexit('Invalid Token'); $user = JFactory::getUser(); $content = JTable::getInstance('contents', 'AdsmanagerTable'); $this->addModelPath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_adsmanager' . DS . 'models'); $configurationmodel = $this->getModel("configuration"); $contentmodel = $this->getModel("content"); $usermodel = $this->getModel("user"); $fieldmodel = $this->getModel("field"); $conf = $configurationmodel->getConfiguration(); $plugins = $fieldmodel->getPlugins(); $id = JRequest::getInt('id', 0); // New or Update if ($id != 0) { $content->load($id); if ($content == null || $content->userid != $user->id) { $app->redirect(TRoute::_('index.php?option=com_adsmanager&view=list')); } $isUpdateMode = 1; if ($conf->update_validation == 1) { $redirect_text = JText::_('ADSMANAGER_INSERT_SUCCESSFULL_CONFIRM'); } else { $redirect_text = JText::_('ADSMANAGER_AD_UPDATED'); } } else { $isUpdateMode = 0; if ($conf->auto_publish == 0) { $redirect_text = JText::_('ADSMANAGER_INSERT_SUCCESSFULL_CONFIRM'); } else { $redirect_text = JText::_('ADSMANAGER_INSERT_SUCCESSFULL_PUBLISH'); } } //Check Max Ads by User $nbcats = $conf->nbcats; if (function_exists("getMaxCats")) { $nbcats = getMaxCats($conf->nbcats); } if ($nbcats <= 1) { if (function_exists("checkAuthorisedNumberAds")) { $limitAds = checkAuthorisedNumberAds($contentmodel, JRequest::getInt('category', 0)); if ($limitAds !== true) { $redirect_text = sprintf(JText::_('ADSMANAGER_MAX_NUM_ADS_PER_CATEGORY_REACHED'), $limitAds); $app->redirect(TRoute::_('index.php?option=com_adsmanager&view=list'), $redirect_text, 'message'); } } else { if (JRequest::getInt('category', 0) != 0) { $category = JRequest::getInt('category', 0); // Need to check limit only for new ad and if category of ad is changed if ($isUpdateMode == 0 || !in_array($category, $contentmodel->getContentCategories($id))) { $nb = $contentmodel->getNbContentsOfUser($user->id, $category); //TODO : check authorised number for multi-categories $categoriesModel = $this->getModel("category"); $category = $categoriesModel->getCategory($category); if ($category->limitads !== "" && $category->limitads !== null) { if ($nb >= $category->limitads && $category->limitads != -1) { $redirect_text = sprintf(JText::_('ADSMANAGER_MAX_NUM_ADS_PER_CATEGORY_REACHED'), $category->limitads); $app->redirect(TRoute::_('index.php?option=com_adsmanager&view=list'), $redirect_text, 'message'); } } } } } } if ($id == 0 && $user->id != "0" && $conf->nb_ads_by_user != -1) { $nb = $contentmodel->getNbContentsOfUser($user->id); if ($nb >= $conf->nb_ads_by_user) { $redirect_text = sprintf(JText::_('ADSMANAGER_MAX_NUM_ADS_REACHED'), $conf->nb_ads_by_user); $app->redirect(TRoute::_('index.php?option=com_adsmanager&view=list'), $redirect_text, 'message'); } } //check if user can post an ad in the category selected //TODO : If multiple category if (version_compare(JVERSION, '1.6', 'ge')) { if ($nbcats <= 1) { $authorisedCategory = TPermissions::getAuthorisedCategories('write'); if (array_search(JRequest::getInt('category', 0), $authorisedCategory) === false) { $redirect_text = sprintf(JText::_('ADSMANAGER_FORBIDDEN_CATEGORY'), $conf->nb_ads_by_user); $app->redirect(TRoute::_('index.php?option=com_adsmanager&view=list'), $redirect_text, 'message'); } } } $current = clone $content; $content->bindContent(JRequest::get('post'), JRequest::get('files'), $conf, $this->getModel("adsmanager"), $plugins); if (function_exists('bindPaidSystemContent')) { bindPaidSystemContent($content, JRequest::get('post'), JRequest::get('files'), $conf, $this->getModel("adsmanager")); } $content->current = $current; $errors = $content->getErrors(); if (count($errors) > 0) { $this->reloadForm($content); } if ($conf->metadata_mode == 'backendonly') { $content->metadata_description = JRequest::getVar('ad_text', ''); $content->metadata_keywords = str_replace(" ", ",", JRequest::getVar('ad_headline', '')); } $errorMsg = null; $dispatcher = JDispatcher::getInstance(); JPluginHelper::importPlugin('adsmanagercontent'); try { $results = $dispatcher->trigger('ADSonContentBeforeSave', array()); } catch (Exception $e) { $errorMsg = $e->getMessage(); $this->reloadForm($content, $errorMsg); } //Creation of account if needed if ($conf->submission_type == 0 && $user->id == 0) { $username = JRequest::getVar('username', ""); $password = JRequest::getVar('password', ""); $email = JRequest::getVar('email', ""); $errorMsg = $usermodel->checkAccount($username, $password, $email, $userid, $conf); if (isset($errorMsg)) { $this->reloadForm($content, $errorMsg); } $user->id = $userid; } //Valid account or visitor are allowed to post if ($user->id != 0 || $conf->submission_type == 2) { $content->userid = $user->id; } else { //trying to save ad, without being registered return; } if (function_exists("getPaidSystemMode")) { $mode = getPaidSystemMode(); } else { $mode = "nopaidsystem"; } $total = 0; switch ($mode) { case "credits": computeCost($total, $items, $content, $conf, $isUpdateMode); if ($total == 0) { $content->save(); } else { if (checkCredits($total, $user->id) == true) { //TODO ? //generateBill($content,$total,$items,$mode,"ok"); removeCredits($user->id, $total, $items, 'Adsmanager'); $content->save(); } else { $errorMsg = sprintf(JText::_('PAIDSYSTEM_NOT_ENOUGH_CREDITS'), strtolower(getCurrencySymbol())); $this->reloadForm($content, $errorMsg); } } break; case "payperad": $adid = $content->savePending(); $content->isPending = true; computeCost($total, $items, $content, $conf, $isUpdateMode); if ($total == 0) { $content->save(); } else { $invoice_id = generateBill($content, $items, $adid); } break; case "nopaidsystem": $content->save(); break; } // We need to put "pending or new values" in the $content obj instead of $content->data $content->map(); if ($mode == "payperad" && $total > 0 && $isUpdateMode == 0) { if (@$conf->preview == 1) { $app->redirect('index.php?option=com_adsmanager&view=preview&id=' . $adid); } else { Invoicing::redirectToPayment($invoice_id); //$app->redirect( 'index.php?option=com_paidsystem&view=payment'); } } else { if (@$conf->preview == 1 && JRequest::getInt('pending', 0) == 1) { $app->redirect('index.php?option=com_adsmanager&view=preview&id=' . $adid); } else { if ($mode == "payperad" && $total > 0 && $isUpdateMode == 1) { Invoicing::redirectToPayment($invoice_id); } else { $cache =& JFactory::getCache('com_adsmanager'); $cache->clean(); if ($isUpdateMode == 0) { if ($conf->send_email_on_new_to_user == 1 && $conf->auto_publish == 1) { $contentmodel->sendMailToUser($conf->new_subject, $conf->new_text, $user, $content, $conf, "new"); } else { if ($conf->send_email_waiting_validation_to_user == 1 && $conf->auto_publish == 0) { $contentmodel->sendMailToUser($conf->waiting_validation_subject, $conf->waiting_validation_text, $user, $content, $conf, "waiting_validation"); } } if ($conf->email_on_waiting_validation == 1 && $conf->auto_publish == 0) { if (isset($conf->admin_waiting_validation_subject) && $conf->admin_waiting_validation_text) { $contentmodel->sendMailToAdmin($conf->admin_waiting_validation_subject, $conf->admin_waiting_validation_text, $user, $content, $conf, "admin_waiting_validation"); } } if ($conf->send_email_on_new == 1) { $contentmodel->sendMailToAdmin($conf->admin_new_subject, $conf->admin_new_text, $user, $content, $conf, "new"); } } else { if ($conf->update_validation == 1) { $contentmodel->sendMailToUser($conf->waiting_validation_subject, $conf->waiting_validation_text, $user, $content, $conf, "waiting_validation"); } else { if ($conf->send_email_on_update_to_user == 1) { $contentmodel->sendMailToUser($conf->update_subject, $conf->update_text, $user, $content, $conf, "update"); } } if ($conf->email_on_waiting_validation == 1 && $conf->update_validation == 1) { if (isset($conf->admin_waiting_validation_subject) && $conf->admin_waiting_validation_text) { $contentmodel->sendMailToAdmin($conf->admin_waiting_validation_subject, $conf->admin_waiting_validation_text, $user, $content, $conf, "admin_waiting_validation"); } } if ($conf->send_email_on_update == 1) { $contentmodel->sendMailToAdmin($conf->admin_update_subject, $conf->admin_update_text, $user, $content, $conf, "update"); } } JPluginHelper::importPlugin('adsmanagercontent'); try { $results = $dispatcher->trigger('ADSonContentAfterSave', array($content, $isUpdateMode, $conf)); } catch (Exception $e) { $errorMsg = $e->getMessage(); } //Redirect if ($conf->submission_type == 2) { $app->redirect(TRoute::_('index.php?option=com_adsmanager&view=list'), $redirect_text, 'message'); } else { $app->redirect(TLink::getMyAdsLink(), $redirect_text, 'message'); } } } } }