source: trunk/services/versions.php @ 767

Last change on this file since 767 was 767, checked in by anonymous, 23 months ago

Add App param ‘template_ext’ used to inform services where to find header and footer templates. Minor fixes.

File size: 6.0 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2012 Strangecode, LLC
6 *
7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/**
24 * versions.php
25 */
26
27// require_once dirname(__FILE__) . '/_config.inc.php';
28
29$auth->requireLogin();
30
31require_once 'codebase/lib/Version.inc.php';
32require_once 'codebase/lib/Lock.inc.php';
33require_once 'codebase/lib/HTML.inc.php';
34
35/******************************************************************************
36 * CODE CONFIG
37 *****************************************************************************/
38
39// Since we're using the singleton pattern we can instantiate a Version object earlier with custom parameters.
40$version =& Version::getInstance($auth);
41$version->setParam(array(
42    // If true, makes an exact comparison of saved vs. live table schemas. If false, just checks that the saved columns are available.
43    'db_schema_strict' => false,
44));
45
46// Query arguments to retain their values between page requests.
47$app->carryQuery('record_table');
48$app->carryQuery('record_key');
49$app->carryQuery('record_val');
50$app->carryQuery('version_title');
51
52// Titles and navigation header.
53$nav->add(_("Versions"), null);
54$nav->set('id', 'versions');
55
56/********************************************************************
57* MAIN
58********************************************************************/
59
60// Request arguments.
61$version_id = getFormData('version_id');
62$record_table = getFormData('record_table');
63$record_key = getFormData('record_key');
64$record_val = getFormData('record_val');
65
66if ('' == $version_id && ('' == $record_table || '' == $record_key || '' == $record_val)) {
67    $app->raiseMsg(_("Record not specified for versioning."), MSG_WARNING, __FILE__, __LINE__);
68    $app->logMsg('Record not specified for versioning.', LOG_WARNING, __FILE__, __LINE__);
69    $app->dieBoomerangURL();
70}
71
72if (getFormData('boomerang', false) && isset($_SERVER['HTTP_REFERER'])) {
73    // We remember which page we came from so we can go back there.
74    $app->setBoomerangURL($_SERVER['HTTP_REFERER'], 'versions');
75}
76
77// What action to take.
78switch (getFormData('op')) {
79
80case 'cancel' :
81    if ($app->validBoomerangURL('versions')) {
82        // Display boomerang page.
83        $app->dieBoomerangURL('versions');
84    }
85    // Display default page.
86    $app->dieURL(false, false);
87    break;
88
89case 'view' :
90    $data = $version->getData($version_id);
91    $versionrecord = $version->getVerson($version_id);
92    $nav->add(sprintf(_("%s <small>Version %s (%s)</small>"), $versionrecord['version_title'], $version_id, date(sprintf('%s %s', $app->getParam('date_format'), $app->getParam('time_format')), strtotime($versionrecord['version_datetime']))));
93    $main_template = 'versions_view.ihtml';
94    break;
95
96case 'diff' :
97    $data = $version->getData($version_id);
98    $versionrecord = $version->getVerson($version_id);
99    $current = $version->getCurrent($record_table, $record_key, $record_val);
100    if (serialize($data) == serialize($current)) {
101        $app->raiseMsg(sprintf(_("Version %s is identical to the current record"), $version_id), MSG_NOTICE, __FILE__, __LINE__);
102    }
103    $nav->add(sprintf(_("%s <small>Version %s (%s)</small>"), $versionrecord['version_title'], $version_id, date(sprintf('%s %s', $app->getParam('date_format'), $app->getParam('time_format')), strtotime($versionrecord['version_datetime']))));
104    $main_template = 'versions_diff.ihtml';
105    break;
106
107case 'restore' :
108    if (!isset($lock) || !($lock instanceof \Lock)) {
109        $lock =& Lock::getInstance($auth);
110    }
111    $lock->select($record_table, $record_key, $record_val);
112    if ($lock->isLocked() && !$lock->isMine()) {
113        $lock->dieErrorPage();
114    }
115
116    if ($v = $version->restore($version_id)) {
117        // Create version of this restored record as the "current" version.
118        $version->create($record_table, $record_key, $record_val, $v['version_title']);
119        $app->raiseMsg(sprintf(_("The record <em>%s</em> has been replaced with <em>%s</em> version <em>%s</em> from <em>%s</em>."), getFormData('version_title'), $v['version_title'], $version_id, $v['version_datetime']), MSG_SUCCESS, __FILE__, __LINE__);
120        $app->dieBoomerangURL('versions', array('break_list_cache'=>'true', false));
121    } else {
122        $app->raiseMsg(_("Version restoration failed."), MSG_ERR, __FILE__, __LINE__);
123        $app->dieURL($_SERVER['PHP_SELF']);
124    }
125    break;
126
127default :
128    $versions = $version->getList($record_table, $record_key, $record_val);
129    if (is_array($versions) && !empty($versions)) {
130        $_POST['version_title'] = $versions[0]['version_title'];
131        $nav->add(sprintf(_("%s versions of <em>%s</em>"), sizeof($versions), $versions[0]['version_title']));
132        $main_template = 'versions_list.ihtml';
133    } else {
134        $app->raiseMsg(sprintf(_("No saved versions available"), null), MSG_NOTICE, __FILE__, __LINE__);
135        $app->dieBoomerangURL('versions');
136    }
137}
138
139
140/******************************************************************************
141 * TEMPLATE INITIALIZATION
142 *****************************************************************************/
143
144include 'header.' . $app->getParam('template_ext');
145include 'codebase/services/templates/' . $main_template;
146include 'footer.' . $app->getParam('template_ext');
147
Note: See TracBrowser for help on using the repository browser.