1 <?php
  2 /**
  3  * @package     FrameworkOnFramework
  4  * @subpackage  table
  5  * @copyright   Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
  6  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  7  */
  8 // Protect from unauthorized access
  9 defined('FOF_INCLUDED') or die;
 10 
 11 /**
 12  * FrameworkOnFramework table behavior class for tags
 13  *
 14  * @package  FrameworkOnFramework
 15  * @since    2.1
 16  */
 17 class FOFTableBehaviorTags extends FOFTableBehavior
 18 {
 19     /**
 20      * The event which runs after binding data to the table
 21      *
 22      * @param   FOFTable        &$table     The table which calls this event
 23      * @param   object|array    &$src       The data to bind
 24      * @param   array           $options    The options of the table
 25      *
 26      * @return  boolean  True on success
 27      */
 28     public function onAfterBind(&$table, &$src, $options = array())
 29     {
 30         // Bind tags
 31         if ($table->hasTags())
 32         {
 33             if ((!empty($src['tags']) && $src['tags'][0] != ''))
 34             {
 35                 $table->newTags = $src['tags'];
 36             }
 37 
 38             // Check if the content type exists, and create it if it does not
 39             $table->checkContentType();
 40 
 41             $tagsTable = clone($table);
 42 
 43             $tagsHelper = new JHelperTags();
 44             $tagsHelper->typeAlias = $table->getContentType();
 45 
 46             // TODO: This little guy here fails because JHelperTags
 47             // need a JTable object to work, while our is FOFTable
 48             // Need probably to write our own FOFHelperTags
 49             // Thank you com_tags
 50             if (!$tagsHelper->postStoreProcess($tagsTable))
 51             {
 52                 $table->setError('Error storing tags');
 53                 return false;
 54             }
 55         }
 56 
 57         return true;
 58     }
 59 
 60     /**
 61      * The event which runs before storing (saving) data to the database
 62      *
 63      * @param   FOFTable  &$table  The table which calls this event
 64      * @param   boolean  $updateNulls  Should nulls be saved as nulls (true) or just skipped over (false)?
 65      *
 66      * @return  boolean  True to allow saving
 67      */
 68     public function onBeforeStore(&$table, $updateNulls)
 69     {
 70         if ($table->hasTags())
 71         {
 72             $tagsHelper = new JHelperTags();
 73             $tagsHelper->typeAlias = $table->getContentType();
 74 
 75             // TODO: JHelperTags sucks in Joomla! 3.1, it requires that tags are
 76             // stored in the metadata property. Not our case, therefore we need
 77             // to add it in a fake object. We sent a PR to Joomla! CMS to fix
 78             // that. Once it's accepted, we'll have to remove the attrocity
 79             // here...
 80             $tagsTable = clone($table);
 81             $tagsHelper->preStoreProcess($tagsTable);
 82         }
 83     }
 84 
 85     /**
 86      * The event which runs after deleting a record
 87      *
 88      * @param   FOFTable &$table  The table which calls this event
 89      * @param   integer  $oid  The PK value of the record which was deleted
 90      *
 91      * @return  boolean  True to allow the deletion without errors
 92      */
 93     public function onAfterDelete(&$table, $oid)
 94     {
 95         // If this resource has tags, delete the tags first
 96         if ($table->hasTags())
 97         {
 98             $tagsHelper = new JHelperTags();
 99             $tagsHelper->typeAlias = $table->getContentType();
100 
101             if (!$tagsHelper->deleteTagData($table, $oid))
102             {
103                 $table->setError('Error deleting Tags');
104                 return false;
105             }
106         }
107     }
108 }
109