1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Table
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Table class supporting modified pre-order tree traversal behavior.
14 *
15 * @since 3.2
16 */
17 class JTableObserverContenthistory extends JTableObserver
18 {
19 /**
20 * Helper object for storing and deleting version history information associated with this table observer
21 *
22 * @var JHelperContenthistory
23 * @since 3.2
24 */
25 protected $contenthistoryHelper;
26
27 /**
28 * The pattern for this table's TypeAlias
29 *
30 * @var string
31 * @since 3.2
32 */
33 protected $typeAliasPattern = null;
34
35 /**
36 * Not public, so marking private and deprecated, but needed internally in parseTypeAlias for
37 * PHP < 5.4.0 as it's not passing context $this to closure function.
38 *
39 * @var JTableObserverContenthistory
40 * @since 3.2
41 * @deprecated Never use this
42 * @private
43 */
44 public static $_myTableForPregreplaceOnly;
45
46 /**
47 * Creates the associated observer instance and attaches it to the $observableObject
48 * Creates the associated content history helper class instance
49 * $typeAlias can be of the form "{variableName}.type", automatically replacing {variableName} with table-instance variables variableName
50 *
51 * @param JObservableInterface $observableObject The subject object to be observed
52 * @param array $params ( 'typeAlias' => $typeAlias )
53 *
54 * @return JTableObserverContenthistory
55 *
56 * @since 3.2
57 */
58 public static function createObserver(JObservableInterface $observableObject, $params = array())
59 {
60 $typeAlias = $params['typeAlias'];
61
62 $observer = new self($observableObject);
63
64 $observer->contenthistoryHelper = new JHelperContenthistory($typeAlias);
65 $observer->typeAliasPattern = $typeAlias;
66
67 return $observer;
68 }
69
70 /**
71 * Post-processor for $table->store($updateNulls)
72 *
73 * @param boolean &$result The result of the load
74 *
75 * @return void
76 *
77 * @since 3.2
78 */
79 public function onAfterStore(&$result)
80 {
81 if ($result)
82 {
83 $this->parseTypeAlias();
84 $aliasParts = explode('.', $this->contenthistoryHelper->typeAlias);
85
86 if (JComponentHelper::getParams($aliasParts[0])->get('save_history', 0))
87 {
88 $this->contenthistoryHelper->store($this->table);
89 }
90 }
91 }
92
93 /**
94 * Pre-processor for $table->delete($pk)
95 *
96 * @param mixed $pk An optional primary key value to delete. If not set the instance property value is used.
97 *
98 * @return void
99 *
100 * @since 3.2
101 * @throws UnexpectedValueException
102 */
103 public function onBeforeDelete($pk)
104 {
105 $this->parseTypeAlias();
106 $aliasParts = explode('.', $this->contenthistoryHelper->typeAlias);
107
108 if (JComponentHelper::getParams($aliasParts[0])->get('save_history', 0))
109 {
110 $this->parseTypeAlias();
111 $this->contenthistoryHelper->deleteHistory($this->table);
112 }
113 }
114
115 /**
116 * Internal method
117 * Parses a TypeAlias of the form "{variableName}.type", replacing {variableName} with table-instance variables variableName
118 * Storing result into $this->contenthistoryHelper->typeAlias
119 *
120 * @return void
121 *
122 * @since 3.2
123 */
124 protected function parseTypeAlias()
125 {
126 // Needed for PHP < 5.4.0 as it's not passing context $this to closure function
127 static::$_myTableForPregreplaceOnly = $this->table;
128
129 $this->contenthistoryHelper->typeAlias = preg_replace_callback('/{([^}]+)}/',
130 function($matches)
131 {
132 return JTableObserverContenthistory::$_myTableForPregreplaceOnly->{$matches[1]};
133 },
134 $this->typeAliasPattern
135 );
136 }
137 }
138