CMS  Version 3.9
content_version.inc
Go to the documentation of this file.
1 <?php
8 class ContentVersion extends DataItem
9 {
10  var $table = "content_version";
11  var $primary_key = "content_version_id";
12 
13  var $fields = array("content_version_id" => Number,
14  "content_class" => String,
15  "content_id" => Number,
16  "version_number" => Number,
17  "content" => Text,
18  "last_modified" => DateTime,
19  "last_modified_by" => Number,
20  "approved" => Boolean,
21  "approved_date" => DateTime,
22  "approved_by" => Number);
23 
28  static function saveDraft($target)
29  {
30  global $user;
31 
32  $tx = new DataTransaction();
33  try
34  {
35  try
36  {
37  $contentVersion = Query::create(ContentVersion, "WHERE content_class=:c AND content_id=:i AND approved=0")
38  ->bind(":c", get_class($target), ":i", $target->getPrimaryKeyValue())
39  ->executeSingle();
40  }
41  catch (DataNotFoundException $e)
42  {
43  $contentVersion = new ContentVersion();
44  $contentVersion->content_class = get_class($target);
45  $contentVersion->content_id = $target->getPrimaryKeyValue();
46  $contentVersion->version_number = ContentVersion::nextVersion($target, $tx);
47  $contentVersion->approved = false;
48  $contentVersion->approved_by = 0;
49  }
50 
51  $contentVersion->last_modified_by = $user->getPrimaryKeyValue();
52  $contentVersion->last_modified = now();
53 
54  $contentVersion->populateFrom($target);
55 
56  $contentVersion->joinTransaction($tx);
57  $contentVersion->save();
58 
59  $tx->commit();
60  }
61  catch(Exception $e)
62  {
63  $tx->rollback();
64  }
65  }
66 
71  static function loadDraft($target)
72  {
73  try
74  {
75  trace(print_r($target, true), 3);
76 
77  $contentVersion = Query::create(ContentVersion, "WHERE content_class=:c AND content_id=:i AND approved=0")
78  ->bind(":c", get_class($target), ":i", $target->getPrimaryKeyValue())
79  ->executeSingle();
80 
81  $contentVersion->populateTo($target);
82  }
83  catch(DataNotFoundException $e)
84  {
85  trace("Draft not found", 3);
86  // Nothing to do
87  }
88 
89  }
90 
96  static function hasDraft($target)
97  {
98  return Query::create(ContentVersion, "WHERE content_class=:c AND content_id=:i AND approved=0")
99  ->bind(":c", get_class($target), ":i", $target->getPrimaryKeyValue())
100  ->exists();
101  }
102 
107  static function deleteDraft($target)
108  {
109  try
110  {
111  $doomed = Query::create(ContentVersion, "WHERE content_class=:c AND content_id=:i AND approved=0")
112  ->bind(":c", get_class($target), ":i", $target->getPrimaryKeyValue())
113  ->executeSingle();
114  $doomed->delete();
115  }
116  catch(DataNotFoundException $e)
117  {
118  trace("Draft not found", 3);
119  // Nothing to do
120  }
121 
122  }
123 
129  static function approvedVersions($target, $includeContent = false)
130  {
131  $query = Query::create(ContentVersion, "WHERE content_class=:c AND content_id=:i AND approved=1 ORDER BY version_number DESC")
132  ->bind(":c", get_class($target), ":i", $target->getPrimaryKeyValue());
133 
134  if (!$includeContent)
135  {
136  $query->filter(new ExclusionFilter("content"));
137  }
138 
139  return $query->execute();
140  }
141 
142  static function getVersion($target, $version)
143  {
144  if ($version == "draft")
145  {
146  // Get current draft
147  try
148  {
149  $contentVersion = Query::create(ContentVersion, "WHERE content_class=:c AND content_id=:i AND approved=0")
150  ->bind(":c", get_class($target), ":i", $target->getPrimaryKeyValue())
151  ->executeSingle();
152  }
153  catch(DataNotFoundException $e)
154  {
155  $contentVersion = null;
156  }
157  }
158  else if ($version == "")
159  {
160  try
161  {
162  $contentVersion = Query::create(ContentVersion, "WHERE content_class=:c AND content_id=:i AND approved=1 ORDER BY version_number DESC LIMIT 1")
163  ->bind(":c", get_class($target), ":i", $target->getPrimaryKeyValue())
164  ->executeSingle();
165  }
166  catch(DataNotFoundException $e)
167  {
168  $contentVersion = null;
169  }
170  }
171  else
172  {
173  try
174  {
175  $contentVersion = Query::create(ContentVersion, "WHERE content_class=:c AND content_id=:i AND version_number=:v AND approved=1")
176  ->bind(":c", get_class($target), ":i", $target->getPrimaryKeyValue(), ":v", $version)
177  ->executeSingle();
178  }
179  catch(DataNotFoundException $e)
180  {
181  $contentVersion = null;
182  }
183  }
184 
185  return $contentVersion;
186  }
187 
188  function approve($target)
189  {
190  global $user;
191 
192  $tx = new DataTransaction();
193  try
194  {
195  $this->joinTransaction($tx);
196  $target->joinTransaction($tx);
197  $this->populateTo($target);
198  $target->save();
199  $this->approved=1;
200  $this->approved_by = $user->getPrimaryKeyValue();
201  $this->approved_date = now();
202  $this->save();
203  $tx->commit();
204  }
205  catch (Exception $e)
206  {
207  $tx->rollback();
208  throw $e;
209  }
210  }
211 
218  static function loadVersion($target, $version)
219  {
220  checkNumeric($version);
221  try
222  {
223  $contentVersion = Query::create(ContentVersion, "WHERE content_class=:c AND content_id=:i AND version_number=:v")
224  ->bind(":c", get_class($target), ":i", $target->getPrimaryKeyValue(), ":v", $version)
225  ->executeSingle();
226 
227  $contentVersion->populateTo($target);
228  }
229  catch(DataNotFoundException $e)
230  {
231  throw new FakoliException("Version $version not found");
232  }
233 
234  }
235 
236  private function populateFrom($target)
237  {
238  $representation = array();
239  foreach($target->versioned_fields as $field)
240  {
241  $representation[$field] = $target->get($field);
242  }
243 
244  $this->content = json_encode($representation);
245  }
246 
247  private function populateTo($target)
248  {
249  $representation = json_decode($this->content, true);
250  foreach($target->versioned_fields as $field)
251  {
252  $target->set($field, $representation[$field]);
253  }
254  }
255 
256  private static function nextVersion($target, $tx)
257  {
258  $version = Query::create(ContentVersion, "WHERE content_class=:c AND content_id=:i")
259  ->bind(":c", get_class($target), ":i", $target->getPrimaryKeyValue())
260  ->executeValue("MAX(version_number)");
261 
262  return intval($version) + 1;
263  }
264 
265  function LastModifiedBy()
266  {
267  $mgr = new UserManager();
268  return $mgr->getUser($this->last_modified_by);
269  }
270 
271  function ApprovedBy()
272  {
273  $mgr = new UserManager();
274  return $mgr->getUser($this->approved_by);
275  }
276 }
277 ?>
static approvedVersions($target, $includeContent=false)
Returns an array of previously approved versions of this content item.
static saveDraft($target)
Saves a draft version of the target object to the content version table.
static loadVersion($target, $version)
Update the versioned fields on the target object to the specified version.
static deleteDraft($target)
Delete the current draft changes for the specified target (if they exist)
static getVersion($target, $version)
static hasDraft($target)
Determine whether the specified target content item has a draft version outstanding.
static loadDraft($target)
Update the versioned fields on target object to the current draft version.
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53
Provides the interface to the user model for the application.
global $user