Source for file assets.php

Documentation is available at assets.php

  1. <?php
  2. /**
  3.  * @package     FrameworkOnFramework
  4.  * @subpackage  table
  5.  * @copyright   Copyright (C) 2010 - 2012 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('_JEXEC'or die;
  10.  
  11. /**
  12.  * FrameworkOnFramework table behavior class for assets
  13.  *
  14.  * @package  FrameworkOnFramework
  15.  * @since    2.1
  16.  */
  17. {
  18.     /**
  19.      * The event which runs before storing (saving) data to the database
  20.      *
  21.      * @param   FOFTable  &$table       The table which calls this event
  22.      * @param   boolean   $updateNulls  Should nulls be saved as nulls (true) or just skipped over (false)?
  23.      *
  24.      * @return  boolean  True to allow saving
  25.      */
  26.     public function onBeforeStore(&$table$updateNulls)
  27.     {
  28.         $result true;
  29.  
  30.         $asset_id_field    $table->getColumnAlias('asset_id');
  31.  
  32.         if (in_array($asset_id_field$table->getKnownFields()))
  33.         {
  34.             if (!empty($table->$asset_id_field))
  35.             {
  36.                 $currentAssetId $table->$asset_id_field;
  37.             }
  38.  
  39.             // The asset id field is managed privately by this class.
  40.             if ($table->isAssetsTracked())
  41.             {
  42.                 unset($table->$asset_id_field);
  43.             }
  44.         }
  45.  
  46.         // Create the object used for inserting/udpating data to the database
  47.         $fields     $table->getTableFields();
  48.  
  49.         // Let's remove the asset_id field, since we unset the property above and we would get a PHP notice
  50.         if (isset($fields[$asset_id_field]))
  51.         {
  52.             unset($fields[$asset_id_field]);
  53.         }
  54.  
  55.         /*
  56.          * Asset Tracking
  57.          */
  58.         if (in_array($asset_id_field$table->getKnownFields()) && $table->isAssetsTracked())
  59.         {
  60.             $parentId $table->getAssetParentId();
  61.             $name     $table->getAssetName();
  62.             $title    $table->getAssetTitle();
  63.  
  64.             $asset JTable::getInstance('Asset''JTable'array('dbo' => $table->getDbo()));
  65.             $asset->loadByName($name);
  66.  
  67.             // Re-inject the asset id.
  68.             $this->$asset_id_field $asset->id;
  69.  
  70.             // Check for an error.
  71.             $error $asset->getError();
  72.  
  73.             if ($error)
  74.             {
  75.                 $table->setError($error);
  76.  
  77.                 return false;
  78.             }
  79.  
  80.             // Specify how a new or moved node asset is inserted into the tree.
  81.             if (empty($table->$asset_id_field|| $asset->parent_id != $parentId)
  82.             {
  83.                 $asset->setLocation($parentId'last-child');
  84.             }
  85.  
  86.             // Prepare the asset to be stored.
  87.             $asset->parent_id $parentId;
  88.             $asset->name      $name;
  89.             $asset->title     $title;
  90.  
  91.             if ($table->getRules(instanceof JAccessRules)
  92.             {
  93.                 $asset->rules = (string) $table->getRules();
  94.             }
  95.  
  96.             if (!$asset->check(|| !$asset->store($updateNulls))
  97.             {
  98.                 $table->setError($asset->getError());
  99.  
  100.                 return false;
  101.             }
  102.  
  103.             // Create an asset_id or heal one that is corrupted.
  104.             if (empty($table->$asset_id_field|| (($currentAssetId != $table->$asset_id_field&& !empty($table->$asset_id_field)))
  105.             {
  106.                 // Update the asset_id field in this table.
  107.                 $table->$asset_id_field = (int) $asset->id;
  108.  
  109.                 $k $table->getKeyName();
  110.  
  111.                 $query $table->getDbo()->getQuery(true);
  112.                 $query->update($table->getDbo()->qn($table->getTableName()));
  113.                 $query->set('asset_id = ' . (int) $table->$asset_id_field);
  114.                 $query->where($table->getDbo()->qn($k' = ' . (int) $table->$k);
  115.                 $table->getDbo()->setQuery($query);
  116.  
  117.                 $table->getDbo()->execute();
  118.             }
  119.  
  120.             $result true;
  121.         }
  122.  
  123.         return $result;
  124.     }
  125.  
  126.     /**
  127.      * The event which runs after binding data to the table
  128.      *
  129.      * @param   FOFTable      &$table  The table which calls this event
  130.      * @param   object|array &$src    The data to bind
  131.      *
  132.      * @return  boolean  True on success
  133.      */
  134.     public function onAfterBind(&$table&$src)
  135.     {
  136.         // Set rules for assets enabled tables
  137.         if ($table->isAssetsTracked())
  138.         {
  139.             // Bind the rules.
  140.             if (isset($src['rules']&& is_array($src['rules']))
  141.             {
  142.                 $table->setRules($src['rules']);
  143.             }
  144.         }
  145.  
  146.         return true;
  147.     }
  148.  
  149.     /**
  150.      * The event which runs before deleting a record
  151.      *
  152.      * @param   FOFTable  &$table  The table which calls this event
  153.      * @param   integer   $oid     The PK value of the record to delete
  154.      *
  155.      * @return  boolean  True to allow the deletion
  156.      */
  157.     public function onBeforeDelete(&$table$oid)
  158.     {
  159.         // If tracking assets, remove the asset first.
  160.         if ($table->isAssetsTracked())
  161.         {
  162.             // Get and the asset name.
  163.             $name        $table->getAssetName();
  164.  
  165.             // Do NOT touch JTable here -- we are loading the core asset table which is a JTable, not a FOFTable
  166.             $asset    JTable::getInstance('Asset');
  167.  
  168.             if ($asset->loadByName($name))
  169.             {
  170.                 if (!$asset->delete())
  171.                 {
  172.                     $table->setError($asset->getError());
  173.  
  174.                     return false;
  175.                 }
  176.             }
  177.             else
  178.             {
  179.                 $table->setError($asset->getError());
  180.  
  181.                 return false;
  182.             }
  183.         }
  184.  
  185.         return true;
  186.     }
  187. }

Documentation generated on Tue, 19 Nov 2013 14:54:06 +0100 by phpDocumentor 1.4.3