Ejemplo n.º 1
0
 function _save()
 {
     $this->autoRender = false;
     $this->autoLayout = false;
     $this->components = array('security');
     $this->__initComponents();
     $listing_id = Sanitize::getInt($this->data['Claim'], 'listing_id');
     $response = array();
     # Validate form token
     if ($this->invalidToken) {
         return $this->ajaxError(s2Messages::invalidToken());
     }
     if (!$listing_id) {
         return $this->ajaxError(s2Messages::accessDenied());
     }
     if ($this->Config->claims_enable && $this->_user->id) {
         $this->data['Claim']['claim_text'] = Sanitize::getString($this->data['Claim'], 'claim_text');
         if ($this->data['Claim']['claim_text'] != '') {
             // Check if this user already has a claim for this listing to update it
             $claim_id = $this->Claim->findOne(array('fields' => array('Claim.claim_id AS `Claim.claim_id`'), 'conditions' => array('Claim.user_id = ' . (int) $this->_user->id, 'Claim.listing_id = ' . $listing_id, 'Claim.approved <= 0')));
             if ($claim_id > 0) {
                 $this->data['Claim']['claim_id'] = $claim_id;
             }
             $this->data['Claim']['user_id'] = $this->_user->id;
             $this->data['Claim']['created'] = date('Y-m-d H:i:s');
             $this->data['Claim']['approved'] = 0;
             if ($this->Claim->store($this->data)) {
                 $update_text = __t("Your claim was submitted, thank you.", true);
                 $response[] = "jQuery('#jr_claimImg{$listing_id}').remove();";
                 return $this->ajaxUpdateDialog($update_text, $response);
             }
         } else {
             # Validation failed
             if (isset($this->Security)) {
                 $response[] = "jQuery('#jr_claimToken').val('" . $this->Security->reissueToken() . "');";
             }
             return $this->ajaxValidation(__t("The message is empty.", true), $response);
         }
     }
     return $this->ajaxError(s2Messages::submitErrorDb());
 }
 function _save()
 {
     $this->autoRender = false;
     $this->autoLayout = false;
     $this->Discussion->isNew = true;
     $response = array();
     $parent_id = Sanitize::getInt($this->data['Discussion'], 'parent_post_id');
     $isNew = Sanitize::getBool($this->data['Discussion'], 'discussion_id');
     # Load the notifications observer model component and initialize it.
     # Done here so it only loads on save and not for all controlller actions.
     $this->components = array('security');
     $this->__initComponents();
     # Validate form token
     if ($this->invalidToken) {
         return $this->ajaxError(s2Messages::invalidToken());
     }
     if (!$this->Config->review_discussions || !$this->Access->canAddPost()) {
         // Server side validation
         return $this->ajaxError(__t("You are not allowed to submit comments.", true, true));
     }
     # Validate input fields
     $this->Discussion->validateInput(Sanitize::getString($this->data['Discussion'], 'name'), "name", "text", __t("You must fill in your name.", true), !$this->_user->id && ($this->Config->discussform_name == 'required' ? true : false));
     $this->Discussion->validateInput(Sanitize::getString($this->data['Discussion'], 'email'), "email", "email", __t("You must fill in a valid email address.", true), ($this->Config->discussform_email == 'required' ? true : false) && !$this->_user->id && $isNew);
     $this->Discussion->validateInput($this->data['Discussion']['text'], "text", "text", __t("You must fill in your comment.", true), true);
     # Validate security code
     if ($this->Access->showCaptcha()) {
         if (!isset($this->data['Captcha']['code'])) {
             $this->Discussion->validateSetError("code", __t("The security code you entered was invalid.", true));
         } elseif ($this->data['Captcha']['code'] == '') {
             $this->Discussion->validateInput($this->data['Captcha']['code'], "code", "text", __t("You must fill in the security code.", true), 1);
         } else {
             if (!$this->Captcha->checkCode($this->data['Captcha']['code'], $this->ipaddress)) {
                 $this->Discussion->validateSetError("code", __t("The security code you entered was invalid.", true));
             }
         }
     }
     $validation_text = implode('<br />', $this->Discussion->validateGetErrorArray());
     if ($validation_text != '') {
         $response[] = "jQuery('#jr_postCommentSubmit{$parent_id}').removeAttr('disabled');";
         $response[] = "jQuery('#jr_postCommentCancel{$parent_id}').removeAttr('disabled');";
         // Replace captcha with new instance
         $captcha = $this->Captcha->displayCode();
         $response[] = "jQuery('.jr_captcha').find('img').attr('src','{$captcha['src']}');";
         $response[] = "jQuery('.jr_captcha_code').val('');";
         return $this->ajaxValidation($validation_text, $response);
     }
     $this->data['Discussion']['user_id'] = $this->_user->id;
     $this->data['Discussion']['ipaddress'] = $this->ipaddress;
     if ($this->_user->id) {
         $this->data['Discussion']['name'] = $this->_user->name;
         $this->data['Discussion']['username'] = $this->_user->username;
         $this->data['Discussion']['email'] = $this->_user->email;
     } else {
         $this->data['Discussion']['username'] = $this->data['Discussion']['name'];
     }
     $this->data['Discussion']['created'] = date('Y-m-d H:i:s');
     $this->data['Discussion']['approved'] = (int) (!$this->Access->moderatePost());
     if ($this->Discussion->store($this->data)) {
         if (!$this->data['Discussion']['approved']) {
             $submit_text = __t("Thank you for your submission. It will be published once it is verified.", true, true);
             return $this->ajaxUpdatePage('jr_postCommentForm' . $parent_id, $submit_text);
         }
         // Query post to get full info for instant refresh
         $discussion = $this->Discussion->findRow(array('conditions' => array('Discussion.type = "review"', 'Discussion.discussion_id = ' . $this->data['Discussion']['discussion_id'])));
         $this->set(array('Access' => $this->Access, 'User' => $this->_user, 'post' => $discussion));
         $update_text = __t("Thank you for your submission.", true, true);
         $update_html = $this->render('discussions', 'post');
         $target_id_after = 'jr_post' . $parent_id;
         $response[] = 'jreviews.discussion.parentCommentPopOver();';
         return $this->ajaxUpdatePage('jr_postCommentFormOuter' . $parent_id, $update_text, $update_html, compact('target_id_after', 'response'));
     }
 }
Ejemplo n.º 3
0
 function _save()
 {
     $this->autoRender = false;
     $this->autoLayout = false;
     $response = array();
     # Done here so it only loads on save and not for all controlller actions.
     $this->components = array('security', 'notifications');
     $this->__initComponents();
     # Validate form token
     if ($this->invalidToken) {
         return $this->ajaxError(s2Messages::invalidToken());
     }
     $selected = '';
     $msg = '';
     $msgAlert = '';
     $msgTags = array();
     # Clean formValues
     $review_id = Sanitize::getInt($this->data['Review'], 'id', 0);
     $this->data['Review']['pid'] = $pid = Sanitize::getInt($this->data['Review'], 'pid', 0);
     if ($review_id == 0) {
         $isNew = $this->Review->isNew = true;
     } else {
         $isNew = $this->Review->isNew = false;
         $this->action = '_edit';
     }
     $this->data['Criteria']['id'] = Sanitize::getInt($this->data['Criteria'], 'id', 0);
     $this->data['Criteria']['state'] = Sanitize::getInt($this->data['Criteria'], 'state', 0);
     $this->data['Review']['pid'] = Sanitize::getInt($this->data['Review'], 'pid');
     $this->data['Review']['email'] = Sanitize::html($this->data['Review'], 'email', '', true);
     $this->data['Review']['title'] = Sanitize::html($this->data['Review'], 'title', '', true);
     $this->data['Review']['comments'] = Sanitize::html($this->data['Review'], 'comments', '', true);
     $this->data['Review']['mode'] = Sanitize::html($this->data['Review'], 'mode', 'com_content', true);
     # Override configuration
     $listing_type = $this->Criteria->findRow(array('conditions' => array('Criteria.id = ' . $this->data['Criteria']['id'])));
     isset($listing_type['ListingType']) and $this->Config->override($listing_type['ListingType']['config']);
     if ($isNew || !$isNew && !$this->Access->isManager()) {
         $this->data['Review']['name'] = $this->data['Review']['username'] = Sanitize::html($this->data['Review'], 'name', '', true);
     }
     // Check if user allowed to post new review
     if ($isNew) {
         if (method_exists($this->Listing, 'getListingOwner')) {
             $owner = $this->Listing->getListingOwner($this->data['Review']['pid']);
             if (!$this->Access->canAddReview($owner['user_id'])) {
                 return $this->ajaxUpdatePage('jr_review0Form', __t("You are not allowed to review your own listing.", true));
             }
         }
         // Get reviewer type, for now editor reviews don't work in Everywhere components
         $this->data['Review']['author'] = $this->data['Review']['mode'] != 'com_content' ? 0 : (int) $this->Access->isJreviewsEditor($this->_user->id);
     } else {
         $currentReview = $this->Review->findRow(array('conditions' => array('Review.id = ' . $review_id)), array());
         # Stop form data tampering
         $formData = $this->data['Review'] + array('criteria_id' => Sanitize::getInt($this->data['Criteria'], 'id'));
         $formToken = cmsFramework::formIntegrityToken($formData, array_keys($this->formTokenKeys), false);
         if (!$this->Access->canEditReview($currentReview['User']['user_id']) || !$this->__validateToken($formToken)) {
             return $this->ajaxError(s2Messages::accessDenied());
         }
         $this->data['Review']['author'] = $currentReview['Review']['editor'];
     }
     # If we are in multiple editor review mode, and this editor has already posted an editor review,
     # he is not allowed to post any kind of review.
     # if we are in single-editor-review mode, his review will become a user review.
     if ($isNew && $this->data['Review']['mode'] == 'com_content' && $this->data['Review']['author']) {
         if ($this->Review->findCount(array('conditions' => array('Review.pid = ' . $this->data['Review']['pid'], 'Review.author = 1', "Review.mode = '" . $this->data['Review']['mode'] . "'", $this->Config->author_review == 2 ? 'Review.userid = ' . $this->_user->id : '1 = 1')))) {
             if ($this->Config->author_review == 2) {
                 return $this->ajaxUpdatePage('jr_review0Form', __t("You already submitted a review.", true));
             } else {
                 $this->data['Review']['author'] = 0;
             }
         }
     }
     # check for duplicate reviews
     $is_jr_editor = $this->Access->isJreviewsEditor($this->_user->id);
     $is_duplicate = false;
     // It's a guest so we only care about checking the IP address if this feature is not disabled and
     // server is not localhost
     if (!$this->_user->id) {
         if (!$this->Config->review_ipcheck_disable && $this->ipaddress != '127.0.0.1') {
             // Do the ip address check everywhere except in localhost
             $is_duplicate = (bool) $this->Review->findCount(array('conditions' => array('Review.pid = ' . $this->data['Review']['pid'], "Review.ipaddress = '{$this->ipaddress}'", "Review.mode = '{$this->data['Review']['mode']}'", "Review.published >= 0")));
         }
     } elseif (!$is_jr_editor && !$this->Config->user_multiple_reviews || $is_jr_editor && $this->Config->author_review == 2) {
         $is_duplicate = (bool) $this->Review->findCount(array('conditions' => array('Review.pid = ' . $this->data['Review']['pid'], "(Review.userid = {$this->_user->id}" . ($this->ipaddress != '127.0.0.1' && !$this->Config->review_ipcheck_disable && !$is_jr_editor ? " OR Review.ipaddress = '{$this->ipaddress}') " : ')'), "Review.mode = '{$this->data['Review']['mode']}'", "Review.published >= 0")));
     }
     if ($isNew && $is_duplicate) {
         return $this->ajaxUpdatePage('jr_review0Form', __t("You already submitted a review.", true));
     }
     # Validate standard fields
     $this->Review->validateInput($this->data['Review']['name'], "name", "text", __t("You must fill in your name.", true), !$this->_user->id && ($this->Config->reviewform_name == 'required' ? true : false));
     $this->Review->validateInput($this->data['Review']['email'], "email", "email", __t("You must fill in a valid email address.", true), ($this->Config->reviewform_email == 'required' ? true : false) && !$this->_user->id && $isNew);
     $this->Review->validateInput($this->data['Review']['title'], "title", "text", __t("You must fill in a title for the review.", true), $this->Config->reviewform_title == 'required' ? true : false);
     if ($listing_type['Criteria']['state'] == 1) {
         # Validate rating fields
         $criteria_qty = $listing_type['Criteria']['quantity'];
         $ratingErr = 0;
         if (!isset($this->data['Rating'])) {
             $ratingErr = $criteria_qty;
         } else {
             for ($i = 0; $i < $criteria_qty; $i++) {
                 if (!isset($this->data['Rating']['ratings'][$i]) || (empty($this->data['Rating']['ratings'][$i]) || $this->data['Rating']['ratings'][$i] == 'undefined' || (double) $this->data['Rating']['ratings'][$i] > $this->Config->rating_scale)) {
                     $ratingErr++;
                 }
             }
         }
         $this->Review->validateInput('', "rating", "text", sprintf(__t("You are missing a rating in %s criteria.", true), $ratingErr), $ratingErr);
     }
     # Validate custom fields
     $review_valid_fields = $this->Field->validate($this->data, 'review', $this->Access);
     $this->Review->validateErrors = array_merge($this->Review->validateErrors, $this->Field->validateErrors);
     $this->Review->validateInput($this->data['Review']['comments'], "comments", "text", __t("You must fill in your comment.", true), $this->Config->reviewform_comment == 'required' ? true : false);
     # Validate security code
     if ($isNew && $this->Access->showCaptcha()) {
         if (!isset($this->data['Captcha']['code'])) {
             $this->Review->validateSetError("code", __t("The security code you entered was invalid.", true));
         } elseif ($this->data['Captcha']['code'] == '') {
             $this->Review->validateInput($this->data['Captcha']['code'], "code", "text", __t("You must fill in the security code.", true), 1);
         } else {
             if (!$this->Captcha->checkCode($this->data['Captcha']['code'], $this->ipaddress)) {
                 $this->Review->validateSetError("code", __t("The security code you entered was invalid.", true));
             }
         }
     }
     # Process validation errors
     $validation = $this->Review->validateGetErrorArray();
     if (!empty($validation)) {
         if ($isNew && $this->Access->showCaptcha()) {
             // Replace captcha with new instance
             $captcha = $this->Captcha->displayCode();
             $response[] = "jQuery('.jr_captcha').find('img').attr('src','{$captcha['src']}');";
             $response[] = "jQuery('.jr_captcha_code').val('');";
         }
         return $this->ajaxValidation(implode('<br />', $validation), $response);
     }
     $savedReview = $this->Review->save($this->data, $this->Access, $review_valid_fields);
     $review_id = $this->data['Review']['id'];
     // Error on review save
     if (Sanitize::getString($savedReview, 'err')) {
         return $this->ajaxError($savedReview['err']);
     }
     // Process moderated actions
     if ($isNew && $this->Access->moderateReview() && !$this->data['Review']['author'] || !$isNew && ($this->Config->moderation_review_edit && $this->Access->moderateReview()) && !$this->data['Review']['author'] || $isNew && $this->Config->moderation_editor_reviews && $this->data['Review']['author'] || !$isNew && ($this->Config->moderation_editor_review_edit && $this->Config->moderation_editor_reviews && $this->Access->moderateReview()) && $this->data['Review']['author']) {
         $target_id = $isNew ? 'jr_review0Form' : 'jr_review_' . $review_id;
         $update_text = __t("Thank you for your submission. It will be published once it is verified.", true);
         return $this->ajaxUpdatePage($target_id, $update_text, '');
     }
     // Get updated review info for non-moderated actions and plugin callback
     $fields = array('Criteria.id AS `Criteria.criteria_id`', 'Criteria.criteria AS `Criteria.criteria`', 'Criteria.state AS `Criteria.state`', 'Criteria.tooltips AS `Criteria.tooltips`', 'Criteria.weights AS `Criteria.weights`');
     $joins = $this->Listing->joinsReviews;
     // Triggers the afterFind in the Observer Model
     $this->EverywhereAfterFind = true;
     if (isset($this->viewVars['reviews'])) {
         $review = current($this->viewVars['reviews']);
     } else {
         $this->Review->runProcessRatings = true;
         $review = $this->Review->findRow(array('fields' => $fields, 'conditions' => 'Review.id = ' . $this->data['Review']['id'], 'joins' => $joins), array('afterFind'));
     }
     $this->set(array('reviewType' => 'user', 'User' => $this->_user, 'Access' => $this->Access, 'reviews' => array($review['Review']['review_id'] => $review)));
     $response = array();
     $fb_checkbox = Sanitize::getBool($this->data, 'fb_publish');
     $facebook_integration = Sanitize::getBool($this->Config, 'facebook_enable') && Sanitize::getBool($this->Config, 'facebook_reviews') && $fb_checkbox;
     // Process non moderated actions
     # New user review
     if ($isNew && !$this->data['Review']['author']) {
         $remove_class = true;
         $target_id = 'jr_user_reviews';
         $update_text = __t("Thank you for your submission.", true);
         $update_html = $this->render('reviews', 'reviews');
         # Facebook wall integration
         $token = cmsFramework::getCustomToken($review['Review']['review_id']);
         $facebook_integration and $response[] = "\n                    jQuery.get(s2AjaxUri+jreviews.ajax_params()+'&url=facebook/_postReview/id:{$review['Review']['review_id']}&{$token}=1');\n                ";
         return $this->ajaxUpdatePage($target_id, $update_text, $update_html, compact('response', 'remove_class'));
     }
     # Edited user review
     if (!$isNew && !$this->data['Review']['author']) {
         // Setup vars for post submit effects
         $target_id = 'jr_review_' . $review_id;
         $update_text = __t("Your changes were saved.", true);
         $update_html = $this->render('reviews', 'reviews');
         return $this->ajaxUpdatePage($target_id, $update_text, $update_html);
     }
     # New editor review
     if ($isNew && $this->data['Review']['author']) {
         $target_id = 'jr_review_' . $review_id;
         $update_text = Sanitize::getInt($review['Criteria'], 'state') != 2 ? __t("Thank you for your submission. Refresh the page to see your review.", true) : __t("Thank you for your submission. Refresh the page to see your comment.", true);
         # Facebook wall integration
         $token = cmsFramework::getCustomToken($review['Review']['review_id']);
         $facebook_integration and $response[] = "\n                    jQuery.get(s2AjaxUri+jreviews.ajax_params()+'&url=facebook/_postReview/id:{$review['Review']['review_id']}&{$token}=1');\n                ";
         return $this->ajaxUpdatePage($target_id, $update_text, '', compact('response'));
     }
     # Edited editor review
     if (!$isNew && $this->data['Review']['author']) {
         $target_id = 'jr_review_' . $review_id;
         $update_text = __t("Your changes were saved, refresh the page to see them.", true);
         return $this->ajaxUpdatePage($target_id, $update_text);
     }
 }
 function _save()
 {
     /*******************************************************************
      * This method is processed inside an iframe
      * To access any of the DOM elements via jQuery it's necessary to prepend
      * all jQuery calls with $parentFrame (i.e. $parentFrame.jQuery)
      ********************************************************************/
     $this->autoRender = false;
     $this->autoLayout = false;
     $response = array();
     $parentFrame = 'window.parent';
     $validation = '';
     $listing_id = Sanitize::getInt($this->data['Listing'], 'id', 0);
     $isNew = $this->Listing->isNew = $listing_id == 0 ? true : false;
     $this->data['email'] = Sanitize::getString($this->data, 'email');
     $this->data['name'] = Sanitize::getString($this->data, 'name');
     $this->data['categoryid_hidden'] = Sanitize::getInt($this->data['Listing'], 'categoryid_hidden');
     $cat_id = Sanitize::getVar($this->data['Listing'], 'catid');
     $this->data['Listing']['catid'] = is_array($cat_id) ? (int) array_pop(array_filter($cat_id)) : (int) $cat_id;
     /*J16*/
     $this->data['Listing']['title'] = Sanitize::getString($this->data['Listing'], 'title', '');
     $this->data['Listing']['created_by_alias'] = Sanitize::getString($this->data, 'name', '');
     if ($this->cmsVersion == CMS_JOOMLA15) {
         $this->data['sectionid_hidden'] = Sanitize::getInt($this->data['Listing'], 'sectionid_hidden');
         $this->data['Listing']['sectionid'] = Sanitize::getInt($this->data['Listing'], 'sectionid');
     } else {
         $this->data['Listing']['language'] = '*';
         $this->data['Listing']['access'] = 1;
     }
     $category_id = $this->data['Listing']['catid'] ? $this->data['Listing']['catid'] : $this->data['categoryid_hidden'];
     # Get criteria info
     $criteria = $this->Criteria->findRow(array('conditions' => array('Criteria.id = 
             (SELECT criteriaid FROM #__jreviews_categories WHERE id = ' . (int) $category_id . ' AND `option` = "com_content")
         ')));
     if (!$criteria) {
         $validation = __t("The category selected is invalid.", true, true);
         $response[] = "{$parentFrame}.jQuery('#jr_listingFormValidation').html('{$validation}');";
         $response[] = "{$parentFrame}.jQuery('.button').removeAttr('disabled');";
         $response[] = "{$parentFrame}.jQuery('.jr_loadingSmall').hide();";
         return $this->makeJS($response);
     }
     $this->data['Criteria']['id'] = $criteria['Criteria']['criteria_id'];
     # Override global configuration
     isset($criteria['ListingType']) and $this->Config->override($criteria['ListingType']['config']);
     # Perform access checks
     if ($isNew && !$this->Access->canAddListing()) {
         return $this->makeJS("{$parentFrame}.s2Alert('" . __t("You are not allowed to submit listings in this category.", true, true) . "')");
     } elseif (!$isNew) {
         $query = "SELECT created_by FROM #__content WHERE id = " . $listing_id;
         $this->_db->setQuery($query);
         $listing_owner = $this->_db->loadResult();
         if (!$this->Access->canEditListing($listing_owner)) {
             return $this->makeJS("{$parentFrame}.s2Alert('" . s2Messages::accessDenied() . "')");
         }
     }
     # Load the notifications observer model component and initialize it.
     # Done here so it only loads on save and not for all controlller actions.
     $this->components = array('security', 'notifications');
     $this->__initComponents();
     if ($this->invalidToken == true) {
         return $this->makeJS("{$parentFrame}.s2Alert('" . s2Messages::invalidToken() . "')");
     }
     # Override configuration
     $category = $this->Category->findRow(array('conditions' => array('Category.id = ' . $this->data['Listing']['catid'])));
     $this->Config->override($category['ListingType']['config']);
     if ($this->Access->loadWysiwygEditor()) {
         $this->data['Listing']['introtext'] = Sanitize::stripScripts(Sanitize::stripWhitespace(Sanitize::getVar($this->data['__raw']['Listing'], 'introtext')));
         $this->data['Listing']['fulltext'] = Sanitize::stripScripts(Sanitize::stripWhitespace(Sanitize::getVar($this->data['__raw']['Listing'], 'fulltext')));
         $this->data['Listing']['introtext'] = html_entity_decode($this->data['Listing']['introtext'], ENT_QUOTES, cmsFramework::getCharset());
         $this->data['Listing']['fulltext'] = html_entity_decode($this->data['Listing']['fulltext'], ENT_QUOTES, cmsFramework::getCharset());
     } else {
         $this->data['Listing']['introtext'] = Sanitize::stripAll($this->data['Listing'], 'introtext', '');
         if (isset($this->data['Listing']['fulltext'])) {
             $this->data['Listing']['fulltext'] = Sanitize::stripAll($this->data['Listing'], 'fulltext', '');
         } else {
             $this->data['Listing']['fulltext'] = '';
         }
     }
     $this->data['Listing']['introtext'] = str_replace('<br>', '<br />', $this->data['Listing']['introtext']);
     $this->data['Listing']['fulltext'] = str_replace('<br>', '<br />', $this->data['Listing']['fulltext']);
     if ($this->Access->canAddMeta()) {
         $this->data['Listing']['metadesc'] = Sanitize::getString($this->data['Listing'], 'metadesc');
         $this->data['Listing']['metakey'] = Sanitize::getString($this->data['Listing'], 'metakey');
     }
     // Title alias handling
     $slug = '';
     $alias = Sanitize::getString($this->data['Listing'], 'alias');
     if ($isNew && $alias == '') {
         $slug = S2Router::sefUrlEncode($this->data['Listing']['title']);
         if (trim(str_replace('-', '', $slug)) == '') {
             $slug = date("Y-m-d-H-i-s");
         }
     } elseif ($alias != '') {
         // Alias filled in so we convert it to a valid alias
         $slug = S2Router::sefUrlEncode($alias);
         if (trim(str_replace('-', '', $slug)) == '') {
             $slug = date("Y-m-d-H-i-s");
         }
     }
     $slug != '' and $this->data['Listing']['alias'] = $slug;
     # Check for duplicates
     switch ($this->Config->content_title_duplicates) {
         case 'category':
             // Checks for duplicates in the same category
             $query = "\r\n                        SELECT \r\n                            count(*) \r\n                        FROM \r\n                            #__content AS Listing WHERE Listing.title = " . $this->_db->Quote($this->data['Listing']['title']) . "\r\n                            AND Listing.state >= 0 \r\n                            AND Listing.catid = " . $this->data['Listing']['catid'] . (!$isNew ? " AND Listing.id <> " . $listing_id : '');
             $this->_db->setQuery($query);
             $titleExists = $this->_db->loadResult();
             break;
         case 'no':
             // Checks for duplicates all over the place
             $query = "\r\n                        SELECT \r\n                            count(*) \r\n                        FROM \r\n                            #__content AS Listing\r\n                        WHERE \r\n                            Listing.title = " . $this->_db->Quote($this->data['Listing']['title']) . "\r\n                           AND Listing.state >= 0\r\n                           " . (!$isNew ? " AND Listing.id <> " . $listing_id : '');
             $this->_db->setQuery($query);
             $titleExists = $this->_db->loadResult();
             break;
         case 'yes':
             // Duplicates are allowed, no checking necessary
             $titleExists = false;
             break;
     }
     if ($titleExists && $this->data['Listing']['title'] != '') {
         // if listing exists
         $validation = '<span>' . __t("A listing with that title already exists.", true, true) . "</span>";
         $response[] = "{$parentFrame}.jQuery('#jr_listingFormValidation').html('{$validation}');";
         $response[] = "{$parentFrame}.jQuery('.button').removeAttr('disabled');";
         $response[] = "{$parentFrame}.jQuery('.jr_loadingSmall').hide();";
         return $this->makeJS($response);
     }
     // Review form display check logic used several times below
     $revFormSetting = $this->Config->content_show_reviewform;
     if ($revFormSetting == 'noteditors' && !$this->Config->author_review) {
         $revFormSetting = 'all';
     }
     $revFormEnabled = !isset($this->data['review_optional']) && $this->Access->canAddReview() && $isNew && ($revFormSetting == 'all' && ($this->Config->author_review || $this->Config->user_reviews) || $revFormSetting == 'authors' && $this->Access->isJreviewsEditor($this->_user->id) || $revFormSetting == 'noteditors' && !$this->Access->isJreviewsEditor($this->_user->id));
     // Validation of content default input fields
     if ($this->cmsVersion == CMS_JOOMLA15) {
         if (!$this->data['Listing']['catid'] || !$this->data['Listing']['sectionid']) {
             $this->Listing->validateSetError("sec_cat", __t("You need to select both a section and a category.", true));
         }
     } else {
         !$this->data['Listing']['catid'] and $this->Listing->validateSetError("sec_cat", __t("You need to select a category.", true));
     }
     // Validate only if it's a new listing
     if ($isNew) {
         if (!$this->_user->id) {
             $this->Listing->validateInput($this->data['name'], "name", "text", __t("You must fill in your name.", true), $this->Config->content_name == "required" ? 1 : 0);
             $this->Listing->validateInput($this->data['email'], "email", "email", __t("You must fill in a valid email address.", true), $this->Config->content_email == "required" ? 1 : 0);
             $this->data['name'] = Sanitize::getString($this->data, 'name', '');
             $this->data['email'] = Sanitize::getString($this->data, 'email', '');
         } else {
             $this->data['name'] = $this->_user->name;
             $this->data['email'] = $this->_user->email;
         }
     }
     $this->Listing->validateInput($this->data['Listing']['title'], "title", "text", __t("You must fill in a title for the new listing.", true, true), 1);
     # Validate listing custom fields
     $listing_valid_fields =& $this->Field->validate($this->data, 'listing', $this->Access);
     $this->Listing->validateErrors = array_merge($this->Listing->validateErrors, $this->Field->validateErrors);
     $this->Listing->validateInput($this->data['Listing']['introtext'], "introtext", "text", __t("You must fill in a summary for the new listing.", true, true), $this->Config->content_summary == "required" ? 1 : 0);
     $this->Listing->validateInput($this->data['Listing']['fulltext'], "fulltext", "text", __t("You must fill in a description for the new listing.", true, true), $this->Config->content_description == "required" ? 1 : 0);
     # Validate review custom fields
     if ($revFormEnabled && $criteria['Criteria']['state']) {
         // Review inputs
         $this->data['Review']['userid'] = $this->_user->id;
         $this->data['Review']['email'] = $this->data['email'];
         $this->data['Review']['name'] = $this->data['name'];
         $this->data['Review']['username'] = Sanitize::getString($this->data, 'name', '');
         $this->data['Review']['title'] = Sanitize::getString($this->data['Review'], 'title');
         $this->data['Review']['location'] = Sanitize::getString($this->data['Review'], 'location');
         // deprecated
         $this->data['Review']['comments'] = Sanitize::getString($this->data['Review'], 'comments');
         // Review standard fields
         $this->Listing->validateInput($this->data['Review']['title'], "rev_title", "text", __t("You must fill in a title for the review.", true, true), $this->Config->reviewform_title == 'required' ? true : false);
         if ($criteria['Criteria']['state'] == 1) {
             $criteria_qty = $criteria['Criteria']['quantity'];
             $ratingErr = 0;
             if (!isset($this->data['Rating'])) {
                 $ratingErr = $criteria_qty;
             } else {
                 for ($i = 0; $i < $criteria_qty; $i++) {
                     if (!isset($this->data['Rating']['ratings'][$i]) || (empty($this->data['Rating']['ratings'][$i]) || $this->data['Rating']['ratings'][$i] == 'undefined' || (double) $this->data['Rating']['ratings'][$i] > $this->Config->rating_scale)) {
                         $ratingErr++;
                     }
                 }
             }
             $this->Listing->validateInput('', "rating", "text", sprintf(__t("You are missing a rating in %s criteria.", true, true), $ratingErr), $ratingErr);
         }
         // Review custom fields
         $this->Field->validateErrors = array();
         // Clear any previous validation errors
         $review_valid_fields = $this->Field->validate($this->data, 'review', $this->Access);
         $this->Listing->validateErrors = array_merge($this->Listing->validateErrors, $this->Field->validateErrors);
         $this->Listing->validateInput($this->data['Review']['comments'], "comments", "text", __t("You must fill in your comment.", true, true), $this->Config->reviewform_comment == 'required' ? true : false);
     }
     // if ($revFormEnabled && $criteria['Criteria']['state'])
     # Validate image fields
     $this->Uploads->validateImages();
     # Validate Captcha security code
     if ($isNew && $this->Access->showCaptcha()) {
         if (!isset($this->data['Captcha']['code'])) {
             $this->Listing->validateSetError("code", __t("The security code you entered was invalid.", true, true));
         } elseif ($this->data['Captcha']['code'] == '') {
             $this->Listing->validateInput($this->data['Captcha']['code'], "code", "text", __t("You must fill in the security code.", true), 1);
         } else {
             if (!$this->Captcha->checkCode($this->data['Captcha']['code'], $this->ipaddress)) {
                 $this->Listing->validateSetError("code", __t("The security code you entered was invalid.", true, true));
             }
         }
     }
     # Get all validation messages
     $validation = $this->Listing->validateGetError() . $this->Uploads->getMsg();
     # Validation failed
     if ($validation != '') {
         $response[] = "var parentForm = {$parentFrame}.jQuery('#jr_listingForm');";
         $response[] = "{$parentFrame}.jQuery('#jr_listingFormValidation').html('{$validation}');";
         $response[] = "parentForm.find('.button').removeAttr('disabled');";
         // Transform textareas into wysiwyg editors
         if ($this->Access->loadWysiwygEditor()) {
             App::import('Helper', 'Editor', 'jreviews');
             $Editor = new EditorHelper();
             $response[] = $parentFrame . '.' . $Editor->transform(true);
         }
         // Replace captcha with new instance
         if ($this->Access->in_groups($this->Config->security_image)) {
             $captcha = $this->Captcha->displayCode();
             $response[] = "{$parentFrame}.jQuery('#captcha').attr('src','{$captcha['src']}');";
             $response[] = "{$parentFrame}.jQuery('#jr_captchaCode').val('');";
         }
         $response[] = "parentForm.find('.jr_loadingSmall').hide();";
         return $this->makeJS($response);
         // Can't use ajaxResponse b/c we are in an iframe
     }
     # Validation passed, continue...
     if ($isNew) {
         $this->data['Listing']['created'] = _CURRENT_SERVER_TIME;
         //gmdate('Y-m-d H:i:s');
         $this->data['Listing']['publish_up'] = _CURRENT_SERVER_TIME;
         //gmdate('Y-m-d H:i:s');
         $this->data['Listing']['created_by'] = $this->_user->id;
         $this->data['Listing']['publish_down'] = NULL_DATE;
         $this->data['Field']['Listing']['email'] = $this->data['email'];
         // If visitor, assign name field to content Alias
         if (!$this->_user->id) {
             $this->data['Listing']['created_by_alias'] = $this->data['name'];
         }
         // Check moderation settings
         $this->data['Listing']['state'] = (int) (!$this->Access->moderateListing());
         // If listing moderation is enabled, then the review is also moderated
         if (!$this->data['Listing']['state']) {
             $this->Config->moderation_reviews = $this->Config->moderation_editor_reviews = $this->Config->moderation_item;
         }
     } else {
         if ($this->Config->moderation_item_edit) {
             $this->data['Listing']['state'] = (int) (!$this->Access->moderateListing());
         }
         $this->data['Listing']['modified'] = _CURRENT_SERVER_TIME;
         //gmdate('Y-m-d H:i:s');
         $this->data['Listing']['modified_by'] = $this->_user->id;
         $query = 'SELECT images FROM #__content WHERE id = ' . $this->data['Listing']['id'];
         $this->_db->setQuery($query);
         $this->data['Listing']['images'] = $this->_db->loadResult();
         // Check total number of images
         if (!$this->Uploads->checkImageCount($this->data['Listing']['images'])) {
             $validation .= '<span>' . sprintf(__t("The total number of images is limited to %s", true, true), $this->Config->content_images) . '</span><br />';
             $response[] = "{$parentFrame}.jQuery('#jr_listingFormValidation').html('{$validation}');";
             $response[] = "{$parentFrame}.jQuery('.button').removeAttr('disabled');";
             $response[] = "{$parentFrame}.jQuery('.jr_loadingSmall').hide();";
             return $this->makeJS($response);
         }
     }
     // Process images and update data array
     if ($this->Uploads->success) {
         $imageUploadPath = PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS;
         $this->Uploads->uploadImages($this->data['Listing']['id'], $imageUploadPath);
         if ($isNew) {
             // New item
             $currImages = $this->Uploads->images;
         } elseif ($this->data['Listing']['images'] != '') {
             // Editing and there are existing images
             $currImages = array_merge(explode("\n", $this->data['Listing']['images']), $this->Uploads->images);
         } else {
             // Editing and there are no existing images
             $currImages = $this->Uploads->images;
         }
         $this->data['Listing']['images'] = implode("\n", $currImages);
     }
     # Save listing
     $savedListing = $this->Listing->store($this->data);
     $listing_id = $this->data['Listing']['id'];
     if (!$savedListing) {
         $validation .= __t("The was a problem saving the listing", true, true);
     }
     // Error on listing save
     if ($validation != '') {
         $response[] = "{$parentFrame}.jQuery('#jr_listingFormValidation').html('{$validation}');";
         $response[] = "{$parentFrame}.jQuery('.button').removeAttr('disabled');";
         $response[] = "{$parentFrame}.jQuery('.jr_loadingSmall').hide();";
         return $this->makeJS($response);
     }
     # Save listing custom fields
     $this->data['Field']['Listing']['contentid'] = $this->data['Listing']['id'];
     $this->Field->save($this->data, 'listing', $isNew, $listing_valid_fields);
     # Begin insert review in table
     if ($revFormEnabled && $criteria['Criteria']['state']) {
         // Get reviewer type, for now editor reviews don't work in Everywhere components
         $this->data['Review']['author'] = (int) $this->Access->isJreviewsEditor($this->_user->id);
         $this->data['Review']['mode'] = 'com_content';
         $this->data['Review']['pid'] = (int) $this->data['Listing']['id'];
         // Force plugin loading on Review model
         $this->_initPlugins('Review');
         $this->Review->isNew = true;
         $savedReview = $this->Review->save($this->data, $this->Access, $review_valid_fields);
     }
     # Before render callback
     if ($isNew && isset($this->Listing->plgBeforeRenderListingSaveTrigger)) {
         $plgBeforeRenderListingSave = $this->Listing->plgBeforeRenderListingSave();
         switch ($plgBeforeRenderListingSave) {
             case '0':
                 $this->data['Listing']['state'] = 1;
                 break;
             case '1':
                 $this->data['Listing']['state'] = 0;
                 break;
             case '':
                 break;
             default:
                 return $plgBeforeRenderListingSave;
                 break;
         }
     }
     # Moderation disabled
     if (!isset($this->data['Listing']['state']) || $this->data['Listing']['state']) {
         $fields = array('Criteria.criteria AS `Criteria.criteria`', 'Criteria.tooltips AS `Criteria.tooltips`');
         $listing = $this->Listing->findRow(array('fields' => $fields, 'conditions' => array('Listing.id = ' . $listing_id)), array('afterFind'));
         # Facebook wall integration
         $fb_checkbox = Sanitize::getBool($this->data, 'fb_publish');
         $facebook_integration = Sanitize::getBool($this->Config, 'facebook_enable') && Sanitize::getBool($this->Config, 'facebook_listings') && $fb_checkbox;
         $token = cmsFramework::getCustomToken($listing_id);
         $facebook_integration and $response[] = $parentFrame . '.jQuery.get(' . $parentFrame . '.s2AjaxUri+' . $parentFrame . '.jreviews.ajax_params()+\'&url=facebook/_postListing/id:' . $listing_id . '&' . $token . '=1\');
             ';
         $url = cmsFramework::route($listing['Listing']['url']);
         $update_text = $isNew ? __t("Thank you for your submission.", true, true) : __t("The listing was successfully saved.", true, true);
         //JOEYG CODE
         //THE FOLLOWING GETS THE LISTING TYPE FROM THE DB FOR THE NEWLY SAVED LISTING
         //IF THE TYPE IS BUSINESS PROFILE OR PROJECT LISTING THEN DISPLAY THE after_submit.thtml file
         //ELSE DISPLAY NORMAL MESSAGE
         //IF WE ONLY WANT TO ADD THE after_submit.thtml if the listing is new then add
         if ($isNew) {
             $query = "SELECT `listing_type` FROM `jos_vpbd_content_criteria` WHERE `jos_vpbd_content_criteria`.`listing_id` = " . $this->data['Listing']['id'];
             $this->_db->setQuery($query);
             $jg_listing_type = $this->_db->loadResult();
             if ($jg_listing_type == 2 || $jg_listing_type == 7) {
                 $update_html = $this->render('listings', 'after_submit');
             } else {
                 $update_html = "<a href=\"{$url}\">" . __t("Click here to view your listing", true) . "</a>";
             }
             //ends if/else
         } else {
             //not new
             $update_html = "<a href=\"{$url}\">" . __t("Click here to view your listing", true) . "</a>";
         }
         //ends if($isNew)
         //ENDS JOEYG ALTER CODE
         $jsonObject = json_encode(compact('target_id', 'update_text', 'update_html'));
         $response[] = '
                 var $parentForm = ' . $parentFrame . '.jQuery(\'#jr_listingForm\');
                 $parentForm.scrollTo({duration:400,offset:-100});
                 $parentForm.s2ShowUpdate(' . $jsonObject . ');                                                       
             ';
         return $this->makeJS($response);
     }
     # Moderation enabled
     $update_text = __t("Thank you for your submission. It will be published once it is verified.", true);
     $update_html = '<div id=\\"s2Msgjr_listingForm\\" class=\\"jr_postUpdate\\">' . $update_text . '</div>';
     $response[] = '
         var $parentForm = ' . $parentFrame . '.jQuery(\'#jr_listingForm\');
         $parentForm.scrollTo({duration:400,offset:-100},function(){
             $parentForm.fadeOut(250,function(){$parentForm.html("' . $update_html . '").show();});
         });
     ';
     return $this->makeJS($response);
 }