Skip to content

Commit

Permalink
Fix code styling
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Sep 5, 2014
1 parent 2c2112a commit f70904c
Show file tree
Hide file tree
Showing 17 changed files with 1,084 additions and 569 deletions.
Expand Up @@ -32,8 +32,6 @@ class PlgContentCh03test01 extends JPlugin
*
* @param object &$subject The object to observe.
* @param array $config An optional associative array of configuration settings.
*
* @since 1.5
*/
public function __construct(&$subject, $config)
{
Expand Down
Expand Up @@ -2,98 +2,140 @@
/**
* System Plugin for Joomla! - Article Text
*
* @author Jisse Reitsma (jisse@yireo.com)
* @copyright Copyright 2014 Jisse Reitsma
* @license GNU Public License version 3 or later
* @link http://www.yireo.com/books/
* @author Jisse Reitsma <jisse@yireo.com>
* @copyright Copyright 2014 Jisse Reitsma
* @license GNU Public License version 3 or later
* @link http://www.yireo.com/books/
*/

defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');
class plgSystemArticletext extends JPlugin

/**
* Class plgSystemArticletext
*
* @since September 2014
*/
class PlgSystemArticletext extends JPlugin
{
/**
* Constructor.
*
* @param object &$subject The object to observe.
* @param array $config An optional associative array of configuration settings.
*/
public function __construct(& $subject, $config)
{
parent::__construct($subject, $config);
}

public function onAfterRender()
{
$application = JFactory::getApplication();
if ($application->isSite() == false)
{
return;
}

$body = $application->getBody();
$body = $this->replaceTags($body);
$application->setBody($body);
}

public function replaceTags($text)
{
if (!preg_match_all('/\{articletext\ ([^\}]+)\}/', $text, $matches))
{
return $text;
}

foreach ($matches[1] as $matchIndex => $match)
{
$tag = $matches[0][$matchIndex];
$tagArgs = $this->convertTagArgs($match);
$aText = $this->getArticleText($tagArgs);
$text = str_replace($tag, $aText, $text);
}

return $text;
}

protected function convertTagArgs($tagArgs)
{
$args = array();
$namevalues = explode(' ', trim($tagArgs));
foreach ($namevalues as $namevalue)
{
$namevalue = explode('=', $namevalue);
$name = $namevalue[0];
$value = $namevalue[1];
$value = preg_replace('/([^0-9]+)/', '', $value);
$args[$name] = $value;
}

return $args;
}

protected function getArticleText($args)
{
$html = (isset($args['html'])) ? (bool)$args['html'] : true;
$id = (isset($args['id'])) ? (int)$args['id'] : 0;

if (!$id > 0)
{
return null;
}

$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('introtext', 'fulltext')));
$query->from($db->quoteName('#__content'));
$query->where($db->quoteName('id').'='.$id);

$db->setQuery($query);
$row = $db->loadObject();
if (empty($row))
{
return null;
}

$text = $row->introtext.$row->fulltext;
if ($html == false)
{
$text = strip_tags($text);
}

return $text;
}
}

/**
* Event method onAfterRender
*
* @return null
*/
public function onAfterRender()
{
$application = JFactory::getApplication();

if ($application->isSite() == false)
{
return;
}

$body = $application->getBody();
$body = $this->replaceTags($body);
$application->setBody($body);
}

/**
* Method to replace tags in a text
*
* @param string $text Text to replace tags in
*
* @return mixed
*/
public function replaceTags($text)
{
if (!preg_match_all('/\{articletext\ ([^\}]+)\}/', $text, $matches))
{
return $text;
}

foreach ($matches[1] as $matchIndex => $match)
{
$tag = $matches[0][$matchIndex];
$tagArgs = $this->convertTagArgs($match);
$aText = $this->getArticleText($tagArgs);
$text = str_replace($tag, $aText, $text);
}

return $text;
}

/**
* Method to convert a string into an argument array
*
* @param string $tagArgs String to convert into an array
*
* @return array
*/
protected function convertTagArgs($tagArgs)
{
$args = array();
$namevalues = explode(' ', trim($tagArgs));

foreach ($namevalues as $namevalue)
{
$namevalue = explode('=', $namevalue);
$name = $namevalue[0];
$value = $namevalue[1];
$value = preg_replace('/([^0-9]+)/', '', $value);
$args[$name] = $value;
}

return $args;
}

/**
* Method to get the text of an article
*
* @param array $args Arguments array
*
* @return null|string
*/
protected function getArticleText($args)
{
$html = (isset($args['html'])) ? (bool) $args['html'] : true;
$id = (isset($args['id'])) ? (int) $args['id'] : 0;

if (!$id > 0)
{
return null;
}

$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('introtext', 'fulltext')));
$query->from($db->quoteName('#__content'));
$query->where($db->quoteName('id') . '=' . $id);

$db->setQuery($query);
$row = $db->loadObject();

if (empty($row))
{
return null;
}

$text = $row->introtext . $row->fulltext;

if ($html == false)
{
$text = strip_tags($text);
}

return $text;
}
}
Expand Up @@ -2,28 +2,39 @@
/**
* System Plugin for Joomla! - Chapter 06 / Test 01
*
* @author Jisse Reitsma (jisse@yireo.com)
* @copyright Copyright 2014 Jisse Reitsma
* @license GNU Public License version 3 or later
* @link http://www.yireo.com/books/
* @author Jisse Reitsma <jisse@yireo.com>
* @copyright Copyright 2014 Jisse Reitsma
* @license GNU Public License version 3 or later
* @link http://www.yireo.com/books/
*/

defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');
class plgSystemCh06test01 extends JPlugin

/**
* Class plgSystemCh06test01
*
* @since September 2014
*/
class PlgSystemCh06test01 extends JPlugin
{
public function onAfterRender()
{
$app = JFactory::getApplication();
$body = $app->getBody();
/**
* Event method onAfterRender
*
* @return bool
*/
public function onAfterRender()
{
$app = JFactory::getApplication();
$body = $app->getBody();

if ($app->isSite() == false)
{
return false;
}
if ($app->isSite() == false)
{
return false;
}

$body = str_replace('</body>', '<foobar></foobar></body>', $body);
$app->setBody($body);
}
$body = str_replace('</body>', '<foobar></foobar></body>', $body);
$app->setBody($body);
}
}
Expand Up @@ -2,38 +2,56 @@
/**
* System Plugin for Joomla! - Chapter 06 / Test 02
*
* @author Jisse Reitsma (jisse@yireo.com)
* @copyright Copyright 2014 Jisse Reitsma
* @license GNU Public License version 3 or later
* @link http://www.yireo.com/books/
* @author Jisse Reitsma <jisse@yireo.com>
* @copyright Copyright 2014 Jisse Reitsma
* @license GNU Public License version 3 or later
* @link http://www.yireo.com/books/
*/

defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');
class plgSystemCh06test02 extends JPlugin

/**
* Class plgSystemCh06test02
*
* @since September 2014
*/
class PlgSystemCh06test02 extends JPlugin
{
public function onAfterRoute()
{
$app = JFactory::getApplication();
$affiliate_id = $app->input->getInt('affiliate_id');
$url = JURI::getInstance()->current();
$this->trackAffiliate($affiliate_id, $url);
}
/**
* Event method onAfterRoute
*
* @return null
*/
public function onAfterRoute()
{
$app = JFactory::getApplication();
$affiliate_id = $app->input->getInt('affiliate_id');
$url = JURI::getInstance()->current();
$this->trackAffiliate($affiliate_id, $url);
}

/**
* Method to insert an affiliate request to the database
*
* @param int $affiliate_id Affiliate ID
* @param string $url Current URL
*
* @return null
*/
protected function trackAffiliate($affiliate_id, $url)
{
$ip = $_SERVER['REMOTE_ADDR'];
$referer = $_SERVER['HTTP_REFERER'];

protected function trackAffiliate($affiliate_id, $url)
{
$ip = $_SERVER['REMOTE_ADDR'];
$referer = $_SERVER['HTTP_REFERER'];

$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->insert($db->quoteName('#__affiliate_requests'))
->set($db->quoteName('affiliate_id').' = '.$affiliate_id)
->set($db->quoteName('url').' = '.$db->Quote($url))
->set($db->quoteName('ip').' = '.$db->Quote($ip))
->set($db->quoteName('referer').' = '.$db->Quote($referer))
->set($db->quoteName('created_at').' = NOW()')
;
}
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->insert($db->quoteName('#__affiliate_requests'))
->set($db->quoteName('affiliate_id') . '=' . $affiliate_id)
->set($db->quoteName('url') . '=' . $db->Quote($url))
->set($db->quoteName('ip') . '=' . $db->Quote($ip))
->set($db->quoteName('referer') . '=' . $db->Quote($referer))
->set($db->quoteName('created_at') . '= NOW()');
}
}

0 comments on commit f70904c

Please sign in to comment.