Automad
 All Classes Functions Variables Pages
move_page.php
1 <?php
2 /*
3  * ....
4  * .: '':.
5  * :::: ':..
6  * ::. ''..
7  * .:'.. ..':.:::' . :. '':.
8  * :. '' '' '. ::::.. ..:
9  * ::::. ..':.. .'''::::: .
10  * :::::::.. '..:::: :. :::: :
11  * ::'':::::::. ':::.'':.:::: :
12  * :.. ''::::::....': '':: :
13  * :::::. '::::: : .. '' .
14  * .''::::::::... ':::.'' ..'' :.''''.
15  * :..:::''::::: :::::...:'' :..:
16  * ::::::. ':::: :::::::: ..:: .
17  * ::::::::.:::: :::::::: :'':.:: .''
18  * ::: '::::::::.' ''::::: :.' '': :
19  * ::: :::::::::..' :::: ::...' .
20  * ::: .:::::::::: :::: :::: .:'
21  * '::' ''::::::: :::: : :: :
22  * ':::: :::: :'' .:
23  * :::: :::: ..''
24  * :::: ..:::: .:''
25  * '''' '''''
26  *
27  *
28  * AUTOMAD
29  *
30  * Copyright (c) 2014 by Marc Anton Dahmen
31  * http://marcdahmen.de
32  *
33  * Licensed under the MIT license.
34  * http://automad.org/license
35  */
36 
37 
38 namespace Automad\Core;
39 
40 
41 defined('AUTOMAD') or die('Direct access not permitted!');
42 
43 
44 /*
45  * Move a page to an existing parent page.
46  */
47 
48 
49 $output = array();
50 
51 
52 // Validation of $_POST.
53 // To avoid all kinds of unexpected trouble, the URL and the destination must exist in the Automad's collection and a title must be present.
54 if (isset($_POST['url']) && isset($_POST['title']) && isset($_POST['destination']) && array_key_exists($_POST['url'], $this->collection) && array_key_exists($_POST['destination'], $this->collection) && $_POST['title']) {
55 
56  // The home page can't be moved!
57  if ($_POST['url'] != '/') {
58 
59  $Page = $this->collection[$_POST['url']];
60  $dest = $this->collection[$_POST['destination']];
61 
62  // Check if new parent directory is writable.
63  if (is_writable(AM_BASE_DIR . AM_DIR_PAGES . $dest->path)) {
64 
65  // Check if the current page's directory and parent directory is writable.
66  if (is_writable(dirname($this->pageFile($Page))) && is_writable(dirname(dirname($this->pageFile($Page))))) {
67 
68  // Move page
69  $newPagePath = $this->movePage($Page->path, $dest->path, $this->extractPrefixFromPath($Page->path), $_POST['title']);
70 
71  // Clear the cache to make sure, the changes get reflected on the website directly.
72  $Cache = new Cache();
73  $Cache->clear();
74 
75  // Rebuild Automad object, since the file structure has changed.
76  $Automad = new Automad(false);
77  $collection = $Automad->getCollection();
78 
79  // Find new URL and return redirect query string.
80  foreach ($collection as $key => $page) {
81 
82  if ($page->path == $newPagePath) {
83 
84  $output['redirect'] = '?context=edit_page&url=' . urlencode($key);
85  break;
86 
87  }
88 
89  }
90 
91  } else {
92 
93  $output['error'] = $this->tb['error_permission'] . '<p>' . dirname(dirname($this->pageFile($Page))) . '</p>';
94 
95  }
96 
97  } else {
98 
99  $output['error'] = $this->tb['error_permission'] . '<p>' . AM_BASE_DIR . AM_DIR_PAGES . rtrim($dest->path, '/') . '</p>';
100 
101  }
102 
103  }
104 
105 } else {
106 
107  $output['error'] = $this->tb['error_page_not_found'];
108 
109 }
110 
111 
112 // Echo JSON
113 echo json_encode($output);
114 
115 
116 ?>