Your IP : 216.73.216.54


Current Path : /var/www/html/components/com_jvld/models/
Upload File :
Current File : /var/www/html/components/com_jvld/models/editlink.php

<?php
/**
 * @version		$Id$
 * @package		JV-LinkDirectory
 * @subpackage	com_jvld
 * @copyright	Copyright 2008-2013 JV-Extensions. All rights reserved
 * @license		GNU General Public License version 3 or later
 * @author		JV-Extensions
 * @link		http://www.jv-extensions.com
 */

// No direct access
defined('_JEXEC') or die('Restricted access');

class JvldModelEditlink extends JvldFendModel
{
	public function getForm($data = array(), $loadData = true)
	{
		$form = $this->loadForm('com_jvld.editlink', 'editlink', array('control' => 'jform', 'load_data' => $loadData), true);
		if (empty($form))
			return false;

		return $form;
	}

	public function getItem()
	{
		$lid = JFactory::getApplication()->input->getInt('lid', 0);
		$item = JvldDb::getRow("select * from #__jvld_links where id = ".(int)$lid);

		$cats = JvldUtil::getLinkCategories($item->id, 'A');
		$item->link_category = $cats[0];
		$item->link_sec_cats = array_slice($cats, 1);

		return $item;
	}

	protected function loadFormData()
	{
		return $this->getItem();
	}

	public function process()
	{
		try
		{
			parent::preProcess();

			$link = new JvldLinkinfo($this->lid);
			if (!$link->isValid())
				throw new Exception(JText::_("COM_JVLD_ERR_INVALID_REQ"));

			if ($link->getInfo('uid') != $this->user->get('id'))
				throw new Exception(JText::_("COM_JVLD_UNAUTHORIZED"));

			$linkplan = new JvldLinkPlan();
			$linkplan->setPlanCode($link->getInfo('oway_plan'));

			// Check Primary Category
			$old_pcat = $link->getPrimaryCategory();
			if ($old_pcat != $this->link_category)
			{
				$ctmp = new JvldCategoryinfo($this->link_category);
				if (($ctmp->getInfo('maxlinks')) && ((1 + $ctmp->getInfo('numlinks')) > $ctmp->getInfo('maxlinks')))
					throw new Exception(JText::sprintf("COM_JVLD_LINKS_ADD_MAXLINKS_FOR_PCAT", $ctmp->getInfo('title')));

				if (!$ctmp->getInfo('accept_links'))
					throw new Exception(JText::sprintf('COM_JVLD_LINKS_ADD_CAT_NOT_ACCEPT_LINKS', $ctmp->getInfo("title")));

				if (!JvldUtil::doesAllowAddToTopLevelCategory($linkplan, $this->link_category))
					throw new Exception(JText::_('COM_JVLD_ADDLINK_TOPCATADD_ERR'));
			}

			// Secondary Categories Checks
			$pscats_new = array();
			$num_max_seccats = $linkplan->getMaxSecondaryCategories();
			if (($num_max_seccats) && (isset($this->link_sec_cats)))
			{
				// Remove the primary category from the list of secondary categories
				for ($i=0;$i<count($this->link_sec_cats);$i++)
				{
					if ($this->link_sec_cats[$i] == $this->link_category)
						continue;

					array_push($pscats_new, $this->link_sec_cats[$i]);
				}

				// Check the max number of secondary categories for this link
				if (count($pscats_new) > $num_max_seccats)
					throw new Exception(JText::sprintf("COM_JVLD_LINKS_ADD_MAXLINKS_CNT", $num_max_seccats));

				for ($i=0;$i<count($pscats_new);$i++)
				{
					$ctmp = new JvldCategoryinfo($pscats_new[$i]);
					if (($ctmp->getInfo('maxlinks')) && ((1 + $ctmp->getInfo('numlinks')) > $ctmp->getInfo('maxlinks')))
						throw new Exception(JText::sprintf("COM_JVLD_LINKS_ADD_MAXLINKS_FOR_PCAT", $ctmp->getInfo('title')));

					if (!$ctmp->getInfo('accept_links'))
						throw new Exception(JText::sprintf('COM_JVLD_LINKS_ADD_CAT_NOT_ACCEPT_LINKS', $ctmp->getInfo("title")));

					if (!JvldUtil::doesAllowAddToTopLevelCategory($linkplan, $pscats_new[$i]))
						throw new Exception(JText::_('COM_JVLD_ADDLINK_TOPCATADD_ERR'));
				}
			}

			if ((isset($this->sef_string)) && ($this->sef_string != $link->getInfo('sef_string')))
			{
				$this->sef_string = JvldPostinit::getSefUrlStringForLink($link->getInfo('partner_title'), $this->sef_string);
				if ($this->sef_string == '')
					throw new Exception(JText::_("COM_JVLD_MANAGE_EDIT_LINK_SEF_EXISTS"));
			}
			else
			{
				$this->sef_string = $link->getInfo('sef_string');
			}

			$msg = '';
			$recip_url = $link->getInfo('reciprocal_link_url');
			if (($linkplan->isBacklinkRequired()) && ($this->reciprocal_link_url != '') && ($this->reciprocal_link_url != $recip_url))
			{
				if (\Joomla\String\StringHelper::substr($this->reciprocal_link_url, 0, 7) != 'http://')
					throw new Exception(JText::_("COM_JVLD_LINK_RURL_NOHTTP"));

				// reciprocal link url and partner url must be of the same domain
				if (JvldUtil::isUrlHacked($link->getInfo('partner_url'), $this->reciprocal_link_url))
					throw new Exception(JText::_("COM_JVLD_LINK_RURL_URL_SAME_DOMAIN"));

				// check for loopback with my domain
				if (JvldUtil::isLoopback($this->reciprocal_link_url))
					throw new Exception(JText::_("COM_JVLD_LINK_RURL_OUR_DOMAIN"));

				$error = '';
				$ret = JvldUtil::checkReciprocalLink($link->getInfo('selfprofile_id'), $this->reciprocal_link_url, $linkplan->allowRelnofollowInBacklink(), $error);
				if ($ret)
				{ // reciprocal link check failed
					$link->updateStatus(_JVLD_LINKSTATUS_PPENDING);
					throw new Exception($error.' - '.JText::_("COM_JVLD_LINK_RURL_CHECK_FAILED"). " - ".JText::_("COM_JVLD_LINK_MOVED_TO_PP"));
				}
				else
				{
					$recip_url = $this->reciprocal_link_url;
					$link->updateStatus(_JVLD_LINKSTATUS_ESTABLISHED);
					$msg = JText::_("COM_JVLD_LINK_MOVED_TO_ES");
				}
			}

			// Icon
			$this->icon_image = '';
			$icon_image_del = JvldUtil::getJFormVariableValue('icon_image_del', 0, 'int');
			$up_icon_image = JvldFileManager::upload("icon_image", _J_ABSPATH_TMP, array('allowed_extns' => _JVLD_ICONIMAGES_EXTNS, 'maxsize'=>$this->cfg->get('icon_image_x'), 'err_on_nofileupload' => 0));
			if ($up_icon_image != '')
			{
				$imgHandlerIcim = new JvldImageProcessor(_J_ABSPATH_TMP, $up_icon_image);
				$imgHandlerIcim->setImage(_JVLD_ICONIMAGES_ABSPATH);
				$imgHandlerIcim->setDimensions($this->cfg->get('icon_image_w'), $this->cfg->get('icon_image_h'));
				$imgHandlerIcim->usePrefix();
				$this->icon_image = $imgHandlerIcim->resizeImage();

				JFile::copy(_J_ABSPATH_TMP . DIRECTORY_SEPARATOR . $up_icon_image, _JVLD_ICONIMAGES_ABSPATH . DIRECTORY_SEPARATOR . 'org_' . $this->icon_image);
			}
			else
			{
				$this->icon_image = ($icon_image_del) ? '' : $link->getInfo('icon_image');
			}

			// Local thumbnail
			$this->localthumb_img = '';
			if ($this->cfg->get('tb_provider') == 'localthumbs')
			{
				$up_localthumb_img = JvldPreviewsCache::uploadLocalThumbsImage("localthumb_img", $link->getInfo('partner_url'));
				if ($up_localthumb_img != '')
				{
					$this->localthumb_img = $up_localthumb_img;
				}
				else
				{
					$localthumb_img_del = JvldUtil::getJFormVariableValue('localthumb_img_del', 0, 'int');
					$this->localthumb_img = ($localthumb_img_del) ? '' : $link->getInfo('localthumb_img');
				}
			}
			else
			{
				$this->localthumb_img = $link->getInfo('localthumb_img');
			}

			$tn_vote_em = (isset($this->notify_email_rated)) ? "notify_email_rated = ".(int)$this->notify_email_rated.", " : '';
			$tn_review_em = (isset($this->notify_email_reviewed)) ? "notify_email_reviewed = ".(int)$this->notify_email_reviewed.", " : '';
			$tn_bmark_em = (isset($this->notify_email_bookmarked)) ? "notify_email_bookmarked = ".(int)$this->notify_email_bookmarked.", " : '';
			$tn_fdown_em = (isset($this->notify_email_fdownloaded)) ? "notify_email_fdownloaded = ".(int)$this->notify_email_fdownloaded.", " : '';
			$tn_vote_sms = (isset($this->notify_sms_rated)) ? "notify_sms_rated = ".(int)$this->notify_sms_rated.", " : '';
			$tn_review_sms = (isset($this->notify_sms_reviewed)) ? "notify_sms_reviewed = ".(int)$this->notify_sms_reviewed.", " : '';
			$tn_bmark_sms = (isset($this->notify_sms_bookmarked)) ? "notify_sms_bookmarked = ".(int)$this->notify_sms_bookmarked.", " : '';
			$tn_fdown_sms = (isset($this->notify_sms_fdownloaded)) ? "notify_sms_fdownloaded = ".(int)$this->notify_sms_fdownloaded.", " : '';
			$tn_stats_em = (isset($this->receive_mstats_email)) ? "receive_mstats_email = ".(int)$this->receive_mstats_email.", " : '';
			$sql_notify_pref = \Joomla\String\StringHelper::trim($tn_vote_em.$tn_review_em.$tn_bmark_em.$tn_fdown_em.$tn_stats_em.$tn_vote_sms.$tn_review_sms.$tn_bmark_sms.$tn_fdown_sms, ", ");
			$sql_notify_pref = ($sql_notify_pref != '') ? $sql_notify_pref.", " : "";

			$mobvalid = $this->input->getInt('mobvalid', 0);

			if (($tn_vote_sms != '') || ($tn_review_sms != '') || ($tn_bmark_sms != '') || ($tn_fdown_sms != ''))
			{
				// sms notification is required
				if ($this->mobile != '')
				{
					if ($link->getInfo('mobile') != $this->mobile)
					{
						// mobile number provided
						if (!$mobvalid)
							throw new Exception(JText::_("COM_JVLD_ADDLINK_SMS_NOTIFY_MOBILE_INVALID"));
					}
				}
				else
				{
					// mobile number is not provided
					throw new Exception(JText::_("COM_JVLD_ADDLINK_SMS_NOTIFY_MOBILE_EMPTY"));
				}
			}

			// Now update extra fields
			$ef_sql = '';
			$efields = JvldUtil::getExtraFields($this->link_category, $link->getInfo('oway_plan'));
			if (count($efields))
			{
				$ef_err = 0;
				$ef_errmsg = '';

				$efields_values = array();
				foreach ($efields as $myfield)
				{
					$field = new JvldField($myfield->id);

					$field_type_obj = JvldXFieldtype::getInstance($field->getFieldType());
					$field_value = $field_type_obj->getInputFieldValue($field);

					$efields_values["field".$field->getID()] = $field_value;

					try
					{
						if ($field_value == '')
						{
							if ($field->isRequired())
								throw new Exception(JText::sprintf("COM_JVLD_ERR_INSUFFINFO", htmlspecialchars($field->getName())));
							else
								$field_value = $field->getDefaultValue();
						}

						if ($field_value != '')
							$field_type_obj->validateData($field_value, _JVLD_FIELD_ACTION_SOURCE_LINK_EDIT, $field);

						$ef_sql .= ", field".$field->getID()." = '".JvldSecure::defendSQL($field_value)."'";
					}
					catch (Exception $ex1)
					{
						$ef_err = 1;
						$ef_errmsg .= $ex1->getMessage().'<br />';
					}
				}

				JvldSession::set('efields_values', json_encode($efields_values), 'com_jvld.editlink');
				if ($ef_err)
					throw new Exception($ef_errmsg);
			}

			// LECS
			$this->lecs_accept_invites = (isset($this->lecs_accept_invites)) ? $this->lecs_accept_invites : 0;
			$this->lecs_xglink_url = (isset($this->lecs_xglink_url)) ? $this->lecs_xglink_url : '';

			if ($link->getInfo('partner_url') != '')
			{
			    if (($this->lecs_accept_invites) && ($this->lecs_xglink_url == ''))
			        $this->lecs_xglink_url = $link->getInfo('partner_url');
			}

			// Check meta keys
			if ($this->metakeys != '')
			{
			    $metakeys = '';

			    $m = explode(",", $this->metakeys);
			    for ($p=0;$p<count($m);$p++)
			    {
    			    $metakeys .= \Joomla\String\StringHelper::trim($m[$p]).", ";
    			    if ($p+1 == $this->cfg->get("max_keywords"))
    			        break;
			    }

                $this->metakeys = \Joomla\String\StringHelper::trim($metakeys, ", ");
			}

			// contact me
			$sql_contact_me = (isset($this->contact_me)) ? "contact_me = '".(int)$this->contact_me."', " : "";

			// Trigger before save plugins
			$tresults = (array)JvldInit::triggerPlugins('OnLinkBeforeSave', array(&$this, 0));
			if (count($tresults) && ($tresults[0] == false))
			{
			    JvldLog::log('onLinkBeforeSave failed');
			    throw new Exception('onLinkBeforeSave failed');
			}

			//Trigger the finder before save event
			JvldInit::triggerPlugins('onFinderBeforeSave', array('com_jvld.link', $this, 0));

			JvldDb::update("update #__jvld_links set partner_email = '".JvldSecure::defendSQL($this->partner_email)."', reciprocal_link_url = '".JvldSecure::defendSQL($recip_url)."', metakeys = '".JvldSecure::defendSQL($this->metakeys)."', sef_string = '".JvldSecure::defendSQL($this->sef_string)."', ".$sql_notify_pref.$sql_contact_me." localthumb_img = '".JvldSecure::defendSQL($this->localthumb_img)."', lecs_accept_invites = '".(int)$this->lecs_accept_invites."', lecs_xglink_url = '".JvldSecure::defendSQL($this->lecs_xglink_url)."', mobile = '".JvldSecure::defendSQL($this->mobile)."', link_updated_on = '".JvldDate::getDateTimeInUTCInMySqlFormat()."', icon_image = '".JvldSecure::defendSQL($this->icon_image)."' ".$ef_sql." where id = '".(int)$link->getInfo('id')."'");

			// Add Tags
			JvldPostinit::updateTagsAndLinksAssociations($this->metakeys, (int)$link->getInfo('id'));

			// Update Map Coordinates
			$link->resetCoordinates();

			// Update primary category
			if ($old_pcat != $this->link_category)
				JvldDb::update("update #__jvld_catlinks set cid = '".(int)$this->link_category."' where lid = '".(int)$link->getInfo('id')."' and isprimary = '1'");

			// Update secondary categories
			JvldUtil::updateSecondaryCategoriesForLink($link->getInfo('id'), $pscats_new);

			/* Trigger LECS announcement */
			if (($link->getInfo('partner_url') != '') && ($this->cfg->get('lecs_en') && $this->lecs_accept_invites))
			{
			    JvldInit::triggerPlugins('OnLecsAcceptInvite', array($link->getInfo('id')));
			    JvldLog::log(__FUNCTION__.":: OnLecsAcceptInvite Plugins triggered ok");
			}

			JvldInit::triggerPlugins('OnLinkAfterSave', array($link->getInfo('id')));

			//Trigger the finder after save event
			JvldInit::triggerPlugins('onFinderAfterSave', array('com_jvld.link', $this, 0));

			JvldSession::clear(array('efields_values'), 'com_jvld.editlink');

			JvldMsg::display(JText::_("COM_JVLD_MANAGE_EDIT_LINK_DONE").' '.$msg, _JVLD_NEXT_PAGE_GO_TO_RETURN_URL);
			return;
		}
		catch (Exception $ex)
		{
			JvldMsg::display($ex->getMessage(), _JVLD_NEXT_PAGE_GO_BACK);
			return;
		}
	}
}