Automad
 All Classes Functions Variables Pages
shared_data.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  * All ajax requests regarding the site's shared data file get processed here.
46  * Basically that means "Saving" as the first option and "Loading" as the second option.
47  *
48  * When "$_POST['data']" exists, that means, that a form with "edited" page information got submitted and the data gets processed to be written into the data file.
49  *
50  * NOTE: Only the inner elements of the form are returned. To keep the outer form information lose form the processing here, there must be an outer form existing on the page to wrap that HTML output.
51  */
52 
53 
54 // Array for returned JSON data.
55 $output = array();
56 
57 
58 if (isset($_POST['data'])) {
59 
60 
61  // If the posted form contains any "data", save the form's data to the page file.
62  $data = array_filter($_POST['data']);
63 
64 
65  // Build file content to be written to the txt file.
66  $pairs = array();
67 
68  foreach ($data as $key => $value) {
69  $pairs[] = $key . AM_PARSE_PAIR_SEPARATOR . ' ' . $value;
70  }
71 
72  $content = implode("\r\n\r\n" . AM_PARSE_BLOCK_SEPARATOR . "\r\n\r\n", $pairs);
73 
74 
75  // Write file.
76  $old = umask(0);
77 
78  if (!@file_put_contents(AM_FILE_SITE_SETTINGS, $content)) {
79  $output['error'] = $this->tb['error_permission'] . '<p>' . AM_FILE_SITE_SETTINGS . '</p>';
80  }
81 
82  umask($old);
83 
84 
85  // Clear the cache.
86  $Cache = new Cache();
87  $Cache->clear();
88 
89 
90 } else {
91 
92 
93  // Else get the data from the .txt file and return a form's inner HTML containing its information.
94  $data = Parse::textFile(AM_FILE_SITE_SETTINGS);
95 
96  // Set main properties
97  $data[AM_KEY_SITENAME] = $this->siteName();
98 
99  if (isset($this->siteData[AM_KEY_THEME])) {
100  $data[AM_KEY_THEME] = $this->siteData[AM_KEY_THEME];
101  } else {
102  $data[AM_KEY_THEME] = false;
103  }
104 
105  // Get available themes.
106  $themes = glob(AM_BASE_DIR . AM_DIR_THEMES . '/*', GLOB_ONLYDIR);
107 
108  // Array of the standard variable keys, which are always needed.
109  $standardKeys = array(AM_KEY_SITENAME, AM_KEY_THEME);
110 
111  // Collect all keys of all shared site variables, which are found in the template files.
112  $themesKeys = array_diff($this->getSiteVarsInThemes(), $standardKeys);
113 
114  // Start buffering the HTML.
115  ob_start();
116 
117 
118  ?>
119 
120  <div class="form-group">
121  <label for="input-data-sitename"><?php echo ucwords(AM_KEY_SITENAME); ?></label>
122  <input id="input-data-sitename" class="form-control input-lg" type="text" name="data[<?php echo AM_KEY_SITENAME; ?>]" value="<?php echo str_replace('"', '&quot;', $data[AM_KEY_SITENAME]); ?>" onkeypress="return event.keyCode != 13;" />
123  </div>
124 
125  <div class="form-group">
126  <label for="input-data-theme">Theme</label>
127  <select id="input-data-theme" class="form-control" name="data[<?php echo AM_KEY_THEME; ?>]">
128  <?php
129 
130  foreach ($themes as $theme) {
131 
132  echo '<option';
133 
134  if (basename($theme) == $data[AM_KEY_THEME]) {
135  echo ' selected';
136  }
137 
138  echo ' value="' . basename($theme) . '">' . ucwords(str_replace('_', ' ', basename($theme))) . '</option>';
139 
140  }
141 
142  ?>
143  </select>
144  </div>
145 
146  <hr>
147 
148  <h3><?php echo $this->tb['shared_vars_used']; ?></h3>
149  <?php
150  // Add textareas for all variables in $data, which are used in the currently installed themes and are not part of the $standardKeys array
151  // and create empty textareas for those keys found in the themes, but are not defined in $data.
152  foreach ($themesKeys as $key) {
153  if (isset($data[$key])) {
154  echo $this->varTextArea($key, $data[$key]);
155  } else {
156  echo $this->varTextArea($key, '');
157  }
158  }
159  ?>
160 
161  <hr>
162 
163  <h3><?php echo $this->tb['shared_vars_unused']; ?></h3>
164  <div id="automad-custom-variables">
165  <?php
166  // All unused site-wide variables.
167  foreach (array_diff(array_keys($data), $standardKeys, $themesKeys) as $key) {
168  echo $this->varTextArea($key, $data[$key], true);
169  }
170  ?>
171  </div>
172  <br />
173  <a class="btn btn-default" href="#" data-toggle="modal" data-target="#automad-add-variable-modal"><span class="glyphicon glyphicon-plus"></span> <?php echo $this->tb['btn_add_var']; ?></a>
174 
175  <hr>
176 
177  <div class="btn-group btn-group-justified">
178  <div class="btn-group"><a class="btn btn-danger" href=""><span class="glyphicon glyphicon-remove"></span> <?php echo $this->tb['btn_discard']; ?></a></div>
179  <div class="btn-group"><button type="submit" class="btn btn-success" data-loading-text="<?php echo $this->tb['btn_loading']; ?>"><span class="glyphicon glyphicon-ok"></span> <?php echo $this->tb['btn_save']; ?></button></div>
180  </div>
181 
182  <!-- Add Variable Modal -->
183  <div id="automad-add-variable-modal" class="modal fade">
184  <div class="modal-dialog">
185  <div class="modal-content">
186  <div class="modal-header">
187  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
188  <h3 class="modal-title"><?php echo $this->tb['btn_add_var']; ?></h3>
189  </div>
190  <div class="modal-body">
191  <div class="form-group">
192  <label for="automad-add-variable-name"><?php echo $this->tb['shared_var_name']; ?></label>
193  <input type="text" class="form-control" id="automad-add-variable-name" onkeypress="return event.keyCode != 13;" />
194  </div>
195  </div>
196  <div class="modal-footer">
197  <div class="btn-group btn-group-justified">
198  <div class="btn-group">
199  <button type="button" class="btn btn-default" data-dismiss="modal">
200  <span class="glyphicon glyphicon-remove"></span> <?php echo $this->tb['btn_close']; ?>
201  </button>
202  </div>
203  <div class="btn-group">
204  <button type="button" class="btn btn-primary" id="automad-add-variable-button" data-automad-error-exists="<?php echo $this->tb['error_var_exists']; ?>" data-automad-error-name="<?php echo $this->tb['error_var_name']; ?>">
205  <span class="glyphicon glyphicon-plus"></span> <?php echo $this->tb['btn_add']; ?>
206  </button>
207  </div>
208  </div>
209  </div>
210  </div>
211  </div>
212  </div>
213 
214  <?php
215 
216 
217  // Save buffer to JSON array.
218  $output['html'] = ob_get_contents();
219  ob_end_clean();
220 
221 
222 }
223 
224 
225 echo json_encode($output);
226 
227 
228 ?>
static textFile($file)
Definition: parse.php:473