Skip to content

Commit

Permalink
#+ [#31488] Add Observer pattern to JTable (and use it for Tags
Browse files Browse the repository at this point in the history
implementation (which also fixes [#31339] too). Thanks Beat.
(Fix #1561)
  • Loading branch information
beat authored and infograf768 committed Jul 21, 2013
1 parent 7169cd2 commit f747e97
Show file tree
Hide file tree
Showing 18 changed files with 837 additions and 272 deletions.
12 changes: 6 additions & 6 deletions administrator/components/com_categories/models/category.php
Expand Up @@ -674,14 +674,14 @@ protected function batchTag($value, $pks, $contexts)
$table->reset();
$table->load($pk);
$tags = array($value);
//$typeAlias = $table->get('tagsHelper')->typeAlias;
$typeAlias = $table->extension . '.category';
$table->get('tagsHelper')->typeAlias = $typeAlias;

$oldTags = $table->get('tagsHelper')->getTagIds($pk, $typeAlias);
$table->get('tagsHelper')->oldTags = $oldTags;
/**
* @var JTableObserverTags $tagsObserver
*/
$tagsObserver = $table->getObserverOfClass('JTableObserverTags');
$result = $tagsObserver->setNewTags($tags, false);

if (!$table->get('tagsHelper')->postStoreProcess($table, $tags, false))
if (!$result)
{
$this->setError($table->getError());

Expand Down
34 changes: 2 additions & 32 deletions administrator/components/com_contact/tables/contact.php
Expand Up @@ -15,14 +15,6 @@
*/
class ContactTableContact extends JTable
{
/**
* Helper object for storing and deleting tag information.
*
* @var JHelperTags
* @since 3.1
*/
protected $tagsHelper = null;

/**
* Constructor
*
Expand All @@ -33,8 +25,6 @@ class ContactTableContact extends JTable
public function __construct(&$db)
{
parent::__construct('#__contact_details', 'id', $db);
$this->tagsHelper = new JHelperTags;
$this->tagsHelper->typeAlias = 'com_contact.contact';
}

/**
Expand Down Expand Up @@ -65,22 +55,6 @@ public function bind($array, $ignore = '')
return parent::bind($array, $ignore);
}

/**
* Override parent delete method to delete tags information.
*
* @param integer $pk Primary key to delete.
*
* @return boolean True on success.
*
* @since 3.1
* @throws UnexpectedValueException
*/
public function delete($pk = null)
{
$result = parent::delete($pk);
return $result && $this->tagsHelper->deleteTagData($this, $pk);
}

/**
* Stores a contact
*
Expand Down Expand Up @@ -156,12 +130,8 @@ public function store($updateNulls = false)
return false;
}

$this->tagsHelper->preStoreProcess($this);
$result = parent::store($updateNulls);

$this->newTags = isset($this->newTags) ? $this->newTags : array();
return $result && $this->tagsHelper->postStoreProcess($this, $this->newTags);
}
return parent::store($updateNulls);
}

/**
* Overloaded check function
Expand Down
34 changes: 1 addition & 33 deletions administrator/components/com_newsfeeds/tables/newsfeed.php
Expand Up @@ -15,14 +15,6 @@
*/
class NewsfeedsTableNewsfeed extends JTable
{
/**
* Helper object for storing and deleting tag information.
*
* @var JHelperTags
* @since 3.1
*/
protected $tagsHelper = null;

/**
* Constructor
*
Expand All @@ -31,8 +23,6 @@ class NewsfeedsTableNewsfeed extends JTable
public function __construct(&$db)
{
parent::__construct('#__newsfeeds', 'id', $db);
$this->tagsHelper = new JHelperTags;
$this->tagsHelper->typeAlias = 'com_newsfeeds.newsfeed';
}

/**
Expand Down Expand Up @@ -133,23 +123,6 @@ public function check()
return true;
}

/**
* Override parent delete method to delete tags information.
*
* @param integer $pk Primary key to delete.
*
* @return boolean True on success.
*
* @since 3.1
* @throws UnexpectedValueException
*/
public function delete($pk = null)
{
$result = parent::delete($pk);
$this->tagsHelper->typeAlias = 'com_newsfeeds.newsfeed';
return $result && $this->tagsHelper->deleteTagData($this, $pk);
}

/**
* Overriden JTable::store to set modified data.
*
Expand Down Expand Up @@ -193,11 +166,6 @@ public function store($updateNulls = false)
// Save links as punycode.
$this->link = JStringPunycode::urlToPunycode($this->link);

$this->tagsHelper->typeAlias = 'com_newsfeeds.newsfeed';
$this->tagsHelper->preStoreProcess($this);
$result = parent::store($updateNulls);

$this->newTags = isset($this->newTags) ? $this->newTags : array();
return $result && $this->tagsHelper->postStoreProcess($this, $this->newTags);
return parent::store($updateNulls);
}
}
33 changes: 1 addition & 32 deletions administrator/components/com_weblinks/tables/weblink.php
Expand Up @@ -18,14 +18,6 @@
*/
class WeblinksTableWeblink extends JTable
{
/**
* Helper object for storing and deleting tag information.
*
* @var JHelperTags
* @since 3.1
*/
protected $tagsHelper = null;

/**
* Constructor
*
Expand All @@ -34,9 +26,6 @@ class WeblinksTableWeblink extends JTable
public function __construct(&$db)
{
parent::__construct('#__weblinks', 'id', $db);

$this->tagsHelper = new JHelperTags;
$this->tagsHelper->typeAlias = 'com_weblinks.weblink';
}

/**
Expand Down Expand Up @@ -132,11 +121,7 @@ public function store($updateNulls = false)
// Convert IDN urls to punycode
$this->url = JStringPunycode::urlToPunycode($this->url);

$this->tagsHelper->preStoreProcess($this);
$result = parent::store($updateNulls);

$this->newTags = isset($this->newTags) ? $this->newTags : array();
return $result && $this->tagsHelper->postStoreProcess($this, $this->newTags);
return parent::store($updateNulls);
}

/**
Expand Down Expand Up @@ -209,22 +194,6 @@ public function check()
return true;
}

/**
* Override parent delete method to delete tags information.
*
* @param integer $pk Primary key to delete.
*
* @return boolean True on success.
*
* @since 3.1
* @throws UnexpectedValueException
*/
public function delete($pk = null)
{
$result = parent::delete($pk);
return $result && $this->tagsHelper->deleteTagData($this, $pk);
}

/**
* Method to set the publishing state for a row or list of rows in the database
* table. The method respects checked out rows by other users and will attempt
Expand Down
36 changes: 19 additions & 17 deletions components/com_tags/views/tag/view.html.php
Expand Up @@ -69,33 +69,35 @@ public function display($tpl = null)
$itemElement->params->merge($temp);
$itemElement->params = (array) json_decode($itemElement->params);
}
foreach ($items as $itemElement)
if ($items !== false)
{
$itemElement->event = new stdClass;
foreach ($items as $itemElement)
{
$itemElement->event = new stdClass;

// For some plugins.
!empty($itemElement->core_body)? $itemElement->text = $itemElement->core_body : $itemElement->text = null;
// For some plugins.
!empty($itemElement->core_body)? $itemElement->text = $itemElement->core_body : $itemElement->text = null;

$dispatcher = JEventDispatcher::getInstance();
$dispatcher = JEventDispatcher::getInstance();

JPluginHelper::importPlugin('content');
$dispatcher->trigger('onContentPrepare', array ('com_tags.tag', &$itemElement, &$itemElement->core_params, 0));
JPluginHelper::importPlugin('content');
$dispatcher->trigger('onContentPrepare', array ('com_tags.tag', &$itemElement, &$itemElement->core_params, 0));

$results = $dispatcher->trigger('onContentAfterTitle', array('com_tags.tag', &$itemElement, &$itemElement->core_params, 0));
$itemElement->event->afterDisplayTitle = trim(implode("\n", $results));
$results = $dispatcher->trigger('onContentAfterTitle', array('com_tags.tag', &$itemElement, &$itemElement->core_params, 0));
$itemElement->event->afterDisplayTitle = trim(implode("\n", $results));

$results = $dispatcher->trigger('onContentBeforeDisplay', array('com_tags.tag', &$itemElement, &$itemElement->core_params, 0));
$itemElement->event->beforeDisplayContent = trim(implode("\n", $results));
$results = $dispatcher->trigger('onContentBeforeDisplay', array('com_tags.tag', &$itemElement, &$itemElement->core_params, 0));
$itemElement->event->beforeDisplayContent = trim(implode("\n", $results));

$results = $dispatcher->trigger('onContentAfterDisplay', array('com_tags.tag', &$itemElement, &$itemElement->core_params, 0));
$itemElement->event->afterDisplayContent = trim(implode("\n", $results));
$results = $dispatcher->trigger('onContentAfterDisplay', array('com_tags.tag', &$itemElement, &$itemElement->core_params, 0));
$itemElement->event->afterDisplayContent = trim(implode("\n", $results));

if ($itemElement->text)
{
$itemElement->core_body = $itemElement->text;
if ($itemElement->text)
{
$itemElement->core_body = $itemElement->text;
}
}
}

}

$this->state = &$state;
Expand Down
1 change: 1 addition & 0 deletions installation/CHANGELOG
Expand Up @@ -29,6 +29,7 @@ $ -> Language fix or change

21-Jul-2013 Jean-Marie Simonet
+ [#31349] Display Error Message when Magic Quotes is Enabled. Thanks Brian Teeman
#+ [#31488] Add Observer pattern to JTable (and use it for Tags implementation (which also fixes [#31339] too). Thanks Beat.

20-Jul-2013 Jean-Marie Simonet
# [#31496] DEBUG_BACKTRACE_IGNORE_ARGS not supported in PHP 5.3.1-5.3.5. Thanks Beat
Expand Down
8 changes: 8 additions & 0 deletions libraries/cms.php
Expand Up @@ -58,3 +58,11 @@
JLoader::register('JInstallerPlugin', JPATH_PLATFORM . '/cms/installer/adapter/plugin.php');
JLoader::register('JInstallerTemplate', JPATH_PLATFORM . '/cms/installer/adapter/template.php');
JLoader::register('JExtension', JPATH_PLATFORM . '/cms/installer/extension.php');

// Register Observers:
// Add Tags to Content, Contact, NewsFeeds, WebLinks and Categories: (this is the only link between them here!):
JObserverMapper::addObserverClassToClass('JTableObserverTags', 'JTableContent', array('typeAlias' => 'com_content.article'));
JObserverMapper::addObserverClassToClass('JTableObserverTags', 'ContactTableContact', array('typeAlias' => 'com_contact.contact'));
JObserverMapper::addObserverClassToClass('JTableObserverTags', 'NewsfeedsTableNewsfeed', array('typeAlias' => 'com_newsfeeds.newsfeed'));
JObserverMapper::addObserverClassToClass('JTableObserverTags', 'WeblinksTableWeblink', array('typeAlias' => 'com_weblinks.weblink'));
JObserverMapper::addObserverClassToClass('JTableObserverTags', 'JTableCategory', array('typeAlias' => '{extension}.category'));
17 changes: 13 additions & 4 deletions libraries/cms/helper/tags.php
Expand Up @@ -347,6 +347,9 @@ public function deleteTagData(JTable $table, $contentItemId)
{
$result = $this->unTagItem($contentItemId, $table);

/**
* @var JTableCorecontent $ucmContentTable
*/
$ucmContentTable = JTable::getInstance('Corecontent');

return $result && $ucmContentTable->deleteByContentId($contentItemId);
Expand Down Expand Up @@ -757,17 +760,22 @@ public static function getTypes($arrayType = 'objectList', $selectTypes = null,
* @param array $newTags Array of new tags
* @param boolean $replace Flag indicating if all exising tags should be replaced
*
* @return null
* @return boolean
*
* @since 3.1
*/
public function postStoreProcess($table, $newTags = array(), $replace = true)
{
if (!empty($table->newTags) && empty($newTags))
{
$newTags = $table->newTags;
}

// If existing row, check to see if tags have changed.
$newTable = clone $table;
$newTable->reset();
$key = $newTable->getKeyName();
$typeAlias = $newTable->get('tagsHelper')->typeAlias;
$typeAlias = $this->typeAlias;

$result = true;

Expand All @@ -784,6 +792,7 @@ public function postStoreProcess($table, $newTags = array(), $replace = true)
{
// Process the tags
$rowdata = new JHelperContent;

$data = $rowdata->getRowData($table);
$ucmContentTable = JTable::getInstance('Corecontent');

Expand Down Expand Up @@ -825,7 +834,7 @@ public function preStoreProcess($table, $newTags = array())
$oldTable = clone $table;
$oldTable->reset();
$key = $oldTable->getKeyName();
$typeAlias = $oldTable->get('tagsHelper')->typeAlias;
$typeAlias = $this->typeAlias;

if ($oldTable->$key && $oldTable->load())
{
Expand Down Expand Up @@ -962,7 +971,7 @@ public function tagDeleteInstances($tag_id)
public function tagItem($ucmId, $table, $tags = array(), $replace = true)
{
$key = $table->get('_tbl_key');
$oldTags = $table->get('tagsHelper')->getTagIds((int) $table->$key, $table->get('tagsHelper')->typeAlias);
$oldTags = $this->getTagIds((int) $table->$key, $this->typeAlias);
$oldTags = explode(',', $oldTags);
$result = $this->unTagItem($ucmId, $table);

Expand Down
1 change: 1 addition & 0 deletions libraries/joomla/observer/index.html
@@ -0,0 +1 @@
<!DOCTYPE html><title></title>

0 comments on commit f747e97

Please sign in to comment.