1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class Wicked_Page_MergeOrRename extends Wicked_Page {
14:
15: 16: 17: 18: 19:
20: public $supportedModes = array(
21: Wicked::MODE_EDIT => true,
22: Wicked::MODE_REMOVE => true,
23: Wicked::MODE_DISPLAY => true);
24:
25: 26: 27: 28: 29:
30: protected $_referrer = null;
31:
32: 33: 34: 35: 36:
37: protected $_errors = array();
38:
39: public function __construct($referrer)
40: {
41: $this->_referrer = $referrer;
42: }
43:
44: 45: 46: 47: 48: 49: 50: 51: 52: 53:
54: public function allows($mode)
55: {
56: if ($mode == Wicked::MODE_EDIT) {
57: if (!parent::allows(Wicked::MODE_REMOVE)) {
58: return false;
59: }
60: $page = Wicked_Page::getPage($this->referrer());
61: if ($page->isLocked(Wicked::lockUser())) {
62: return false;
63: }
64: }
65: return parent::allows($mode);
66: }
67:
68: 69: 70: 71: 72:
73: public function getPermissions()
74: {
75: return parent::getPermissions($this->referrer());
76: }
77:
78: 79: 80: 81: 82:
83: public function display()
84: {
85: global $wicked, $registry, $notification;
86:
87: $template = $GLOBALS['injector']->createInstance('Horde_Template');
88: $template->setOption('gettext', true);
89:
90: $referrer = $this->referrer();
91: $template->set('pageName', 'MergeOrRename');
92: $template->set('formAction', Wicked::url('MergeOrRename'));
93: $template->set('referrer', $referrer);
94:
95: $template->set('referrerLink', Wicked::url($referrer));
96:
97: $requiredMarker = Horde::img('required.png', '*');
98: $template->set('requiredMarker', $requiredMarker);
99:
100: $references = $wicked->getBackLinks($referrer);
101:
102: foreach ($references as $key => $page) {
103: $references[$key]['page_url'] = htmlspecialchars(Wicked::url($page['page_name']));
104: $references[$key]['page_name'] = htmlspecialchars($page['page_name']);
105:
106:
107:
108:
109: $references[$key]['checkbox'] = preg_replace('/([^a-zA-Z_0-9 ])/e', '"=" . str_pad(dechex(ord(\'\\1\')), 2, \'0\', STR_PAD_LEFT)', $page['page_name']);
110: }
111:
112: $template->set('references', $references);
113: $template->set('referenceCount', sprintf(_("This page is referenced from %d other page(s)."), count($references)));
114: $template->set('formInput', Horde_Util::formInput());
115:
116:
117: foreach (array('new_name', 'collision') as $elt) {
118: if (!isset($this->_errors[$elt])) {
119: $this->_errors[$elt] = '';
120: }
121: }
122: $template->set('errors', $this->_errors);
123:
124: $template->set('new_name', Horde_Util::getFormData('new_name'));
125:
126: Horde::addScriptFile('stripe.js', 'horde', true);
127: echo $template->fetch(WICKED_TEMPLATES . '/display/MergeOrRename.html');
128: return true;
129: }
130:
131: public function pageName()
132: {
133: return 'MergeOrRename';
134: }
135:
136: public function pageTitle()
137: {
138: return sprintf(_("Merge/Rename: %s"), $this->referrer());
139: }
140:
141: public function referrer()
142: {
143: return $this->_referrer;
144: }
145:
146: 147: 148:
149: public function handleAction()
150: {
151: global $wicked, $notification, $registry;
152:
153: if (Horde_Util::getFormData('submit') == _("Cancel")) {
154: Wicked::url($this->referrer(), true)->redirect();
155: }
156:
157: $referrer = $this->referrer();
158:
159: $new_name = Horde_Util::getFormData('new_name');
160: if (empty($new_name)) {
161: $this->_errors['new_name'] = _("This is a required field.");
162: } elseif ($new_name == $referrer) {
163: $this->_errors['new_name'] = _("New name is the same as old name.");
164: }
165: $collision = Horde_Util::getFormData('collision');
166: if (empty($collision)) {
167: $this->_errors['collision'] = _("This is a required field.");
168: }
169:
170: if (count($this->_errors)) {
171: return;
172: }
173:
174: $sourcePage = Wicked_Page::getPage($referrer);
175: if (!$this->allows(Wicked::MODE_EDIT)) {
176: throw new Wicked_Exception(sprintf(_("You do not have permission to edit \"%s\""),
177: $referrer));
178: }
179:
180: $destPage = Wicked_Page::getPage($new_name);
181: if (!($destPage instanceof Wicked_Page_AddPage)) {
182:
183: if ($collision != 'merge') {
184:
185: throw new Wicked_Exception(sprintf(_("Page \"%s\" already exists."),
186: $new_name));
187: }
188: if (!$destPage->allows(Wicked::MODE_EDIT)) {
189: throw new Wicked_Exception(sprintf(_("You do not have permission to edit \"%s\""),
190: $new_name));
191: }
192:
193:
194: $newText = $destPage->getText() . "\n----\n" . $sourcePage->getText();
195: $changelog = sprintf(_("Merged from %s"), $referrer);
196: $wicked->updateText($new_name, $newText, $changelog);
197: $wicked->removeAllVersions($referrer);
198:
199: $notification->push(sprintf(_("Merged \"%s\" into \"%s\"."), $referrer, $new_name), 'horde.success');
200:
201: $url = Wicked::url($new_name, true, -1);
202: $message = sprintf(_("Merged \"%s\" into \"%s\". New page: %s\n"), $referrer, $new_name, $url);
203: Wicked::mail($message, array(
204: 'Subject' => '[' . $registry->get('name') . '] merged: ' . $referrer . ', ' . $new_name));
205: } else {
206:
207: $wicked->renamePage($referrer, $new_name);
208: $notification->push(sprintf(_("Renamed \"%s\" to \"%s\"."), $referrer, $new_name), 'horde.success');
209:
210: $url = Wicked::url($new_name, true, -1);
211: $message = sprintf(_("Renamed \"%s\" to \"%s\". New page: %s\n"), $referrer, $new_name, $url);
212: Wicked::mail($message, array(
213: 'Subject' => '[' . $registry->get('name') . '] renamed: ' . $referrer . ', ' . $new_name));
214: }
215:
216: $wikiWord = '/^' . Wicked::REGEXP_WIKIWORD . '$/';
217:
218:
219:
220:
221:
222: $references = Horde_Util::getFormData('ref', array());
223: foreach ($references as $name => $value) {
224: $page_name = quoted_printable_decode($name);
225:
226:
227: if ($page_name == $referrer) {
228: $page_name = $new_name;
229: }
230:
231: try {
232: $refPage = $wicked->retrieveByName($page_name);
233: } catch (Wicked_Exception $e) {
234: $notification->push(sprintf(_("Error retrieving %s: %s"),
235: $page_name, $e->getMessage()),
236: 'horde.error');
237: continue;
238: }
239:
240: $changelog = sprintf(_("Changed references from %s to %s"),
241: $referrer, $new_name);
242:
243: if (preg_match($wikiWord, $new_name)) {
244: $replaceWith = $new_name;
245: } else {
246: $replaceWith = '((' . $new_name . '))';
247: }
248:
249: $from = array('/\(\(' . preg_quote($referrer, '/') . '\)\)/');
250: $to = array($replaceWith);
251:
252:
253: if (preg_match($wikiWord, $referrer)) {
254: $from[] = '/\b' . preg_quote($referrer, '/') . '\b/';
255: $to[] = $replaceWith;
256: }
257:
258: $newText = preg_replace($from, $to, $refPage['page_text']);
259: $wicked->updateText($page_name, $newText, $changelog);
260: }
261:
262: Wicked::url($new_name, true)->redirect();
263: }
264:
265: }
266: