source: extensions/AdditionalPages/trunk/admin/add_page.inc.php @ 28649

Last change on this file since 28649 was 28649, checked in by mistic100, 10 years ago

use trigger_change

File size: 7.6 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5if (!isset($edited_page))
6{
7  $page_title = l10n('ap_create');
8
9  $edited_page = array(
10    'id'         => 0,
11    'title'      => '',
12    'permalink'  => '',
13    'lang'       => 'ALL',
14    'homepage'   => false,
15    'standalone' => false,
16    'level'      => 0,
17    'users'      => array('guest', 'generic', 'normal', 'admin', 'webmaster'),
18    'groups'     => array(),
19    'content'    => '',
20  );
21}
22
23// load template
24if (isset($_GET['load_template']))
25{
26  if (file_exists(AP_DISTRIBUED . $_GET['load_template']))
27  {
28    $distribued = load_ap_template($_GET['load_template'], true);
29  }
30  else
31  {
32    $distribued = trigger_change('load_ap_template', array(), $_GET['load_template']);
33  }
34  $edited_page = array_merge($edited_page, $distribued);
35  $template->assign('template_selected', $_GET['load_template']);
36}
37
38// Submit form
39if (isset($_POST['save']))
40{
41  if (empty($_POST['title']))
42  {
43    array_push($page['errors'], l10n('ap_no_name'));
44  }
45  if (!empty($_POST['permalink']))
46  {
47    $permalink = trim($_POST['permalink'], ' /');
48    $permalink = str_replace(array(' ', '/'), '_',$permalink);
49
50    $query ='
51SELECT id FROM '.ADD_PAGES_TABLE.'
52WHERE permalink = "'.$permalink.'"
53  AND id <> '.$edited_page['id'].'
54;';
55    $ids = array_from_query($query, 'id');
56    if (!empty($ids))
57    {
58      array_push($page['errors'], sprintf(l10n('ap_permalink_already_used'), $permalink, $ids[0]));
59    }
60    $permalink = '"'.$permalink.'"';
61  }
62  else
63  {
64    $permalink = 'NULL';
65  }
66
67  $language = (empty($_POST['lang']) or $_POST['lang'] == 'ALL') ? 'NULL' : '"'.$_POST['lang'].'"';
68  $group_access = !empty($_POST['groups']) ? '"'.implode(',', $_POST['groups']).'"' : 'NULL';
69  $standalone = isset($_POST['standalone']) ? '"true"' : '"false"';
70
71  $user_access = 'NULL';
72  if ($conf['AP']['user_perm'])
73  {
74    $user_access = !empty($_POST['users']) ? '"'.implode(',', $_POST['users']).'"' : '""';
75  }
76
77  $level_access = !empty($_POST['level']) ? $_POST['level'] : 0;
78
79  if (empty($page['errors']))
80  {
81    if ($page['tab'] == 'edit_page')
82    {
83      $query = '
84UPDATE '.ADD_PAGES_TABLE.'
85SET lang = '.$language.',
86  title = "'.$_POST['title'].'",
87  content = "'.$_POST['ap_content'].'",
88  users = '.$user_access.',
89  groups = '.$group_access.',
90  level = '.$level_access.',
91  permalink = '.$permalink.',
92  standalone = '.$standalone.'
93WHERE id = '.$edited_page['id'] .'
94;';
95      pwg_query($query);
96    }
97    else
98    {
99      $query = 'SELECT MAX(ABS(pos)) AS pos FROM ' . ADD_PAGES_TABLE . ';';
100      list($position) = array_from_query($query, 'pos');
101
102      $query = '
103INSERT INTO '.ADD_PAGES_TABLE.' ( pos , lang , title , content , users , groups , level , permalink, standalone)
104VALUES (
105  '.($position+1).',
106  '.$language.',
107  "'.$_POST['title'].'",
108  "'.$_POST['ap_content'].'",
109  '.$user_access.',
110  '.$group_access.',
111  '.$level_access.',
112  '.$permalink.',
113  '.$standalone.'
114);';
115      pwg_query($query);
116      $edited_page['id'] = pwg_db_insert_id(ADD_PAGES_TABLE, 'id');
117    }
118
119    // Homepage
120    if (isset($_POST['homepage']) xor $conf['AP']['homepage'] == $edited_page['id'])
121    {
122      $conf['AP']['homepage'] = isset($_POST['homepage']) ? $edited_page['id'] : null;
123      conf_update_param('additional_pages', pwg_db_real_escape_string(serialize($conf['AP'])));
124    }
125
126    // Backup file
127    mkgetdir(AP_BACKUP_DIR, MKGETDIR_PROTECT_HTACCESS&~MKGETDIR_DIE_ON_ERROR);
128    $sav_file = @fopen(AP_BACKUP_DIR . $edited_page['id'] . '.txt', "w");
129    @fwrite($sav_file, "Title: ".stripslashes($_POST['title'])."\nPermalink: ".stripslashes($_POST['permalink'])."\n\n".stripslashes($_POST['ap_content']));
130    @fclose($sav_file);
131
132    // Redirect to admin pannel or additional page
133    if (isset($_GET['redirect']))
134    {
135      redirect(make_index_url(array('section'=>'page')).'/'.$edited_page['id']);
136    }
137    redirect($my_base_url.'&page_saved=');
138  }
139
140  $edited_page['title'] = stripslashes($_POST['title']);
141  $edited_page['permalink'] = stripslashes($_POST['permalink']);
142  $edited_page['content'] = stripslashes($_POST['ap_content']);
143  $edited_page['lang'] = !empty($_POST['lang']) ? $_POST['lang'] : 'ALL';
144  $edited_page['groups'] = !empty($_POST['groups']) ? $_POST['groups'] : array();
145  $edited_page['users'] = !empty($_POST['users']) ? $_POST['users'] :  array();
146  $edited_page['level'] = !empty($_POST['level']) ? $_POST['level'] :  0;
147  $edited_page['homepage'] = isset($_POST['homepage']);
148  $edited_page['standalone'] = isset($_POST['standalone']);
149}
150
151// Language options
152if ($conf['AP']['language_perm'])
153{
154  $languages = get_languages();
155  $options = array('ALL' => l10n('ap_all_lang'));
156  foreach ($languages as $language_code => $language_name)
157  {
158    $options[$language_code] = $language_name;
159  }
160  $template->assign(array(
161    'lang' => $options,
162    'selected_lang' => $edited_page['lang'],
163    )
164  );
165}
166
167// Groups options
168if ($conf['AP']['group_perm'])
169{
170        $query = 'SELECT id, name FROM '.GROUPS_TABLE.' ORDER BY name ASC;';
171  $result = pwg_query($query);
172  $groups = array();
173  while ($row = pwg_db_fetch_assoc($result))
174  {
175    $groups[$row['id']] = $row['name'];
176  }
177  $template->assign(array(
178    'groups' => $groups,
179    'selected_groups' => $edited_page['groups'],
180    )
181  );
182}
183
184// Users options
185if ($conf['AP']['user_perm'])
186{
187  $users_id = array('guest', 'generic', 'normal', 'admin', 'webmaster');
188  $users = array();
189  foreach ($users_id as $id)
190  {
191    $users[$id] = l10n('user_status_'.$id);
192  }
193  $template->assign(array(
194    'users' => $users,
195    'selected_users' => $edited_page['users'],
196    )
197  );
198}
199
200// User level options
201if ($conf['AP']['level_perm'])
202{
203  foreach ($conf['available_permission_levels'] as $level)
204  {
205    $level_options[$level] = l10n(sprintf('Level %d', $level));
206  }
207  $template->assign(array(
208    'level_perm' => $level_options,
209    'level_selected' => $edited_page['level']
210    )
211  );
212}
213
214// Available templates
215if (!isset($_GET['edit']))
216{
217  $distribued = array();
218  $dh = opendir(AP_DISTRIBUED);
219  if ($dh)
220  {
221    while (($dir = readdir($dh)) !== false)
222    {
223      if ( is_dir(AP_DISTRIBUED.$dir) and $dir!='.' and $dir!='..' and $dir!='.svn' )
224      {
225        array_push($distribued, load_ap_template($dir, false));
226      }
227    }
228    closedir($dh);
229  }
230  $distribued = trigger_change('load_ap_templates_list', $distribued); // external plugins can add templates
231  $template->assign('TEMPLATES', $distribued);
232}
233
234// template output
235$template->assign(array(
236  'AP_TITLE' => $page_title,
237  'NAME' => htmlspecialchars($edited_page['title']),
238  'PERMALINK' => htmlspecialchars($edited_page['permalink']),
239  'HOMEPAGE' => $edited_page['homepage'],
240  'STANDALONE' => $edited_page['standalone'],
241  'CONTENT' => htmlspecialchars($edited_page['content'])
242  )
243);
244
245$template->set_filename('plugin_admin_content', dirname(__FILE__) . '/template/add_page.tpl');
246$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
247
248
249function load_ap_template($dir, $with_content=true)
250{
251  $path = AP_DISTRIBUED . $dir . '/';
252 
253  // default template
254  $template_conf = array(
255    'name'       => $dir,
256    'title'      => '',
257    'permalink'  => '',
258    'lang'       => 'ALL',
259    'homepage'   => false,
260    'standalone' => false,
261    'level'      => 0,
262    'users'      => array('guest', 'generic', 'normal', 'admin', 'webmaster'),
263    'groups'     => array(),
264    'content'    => '',
265  );
266 
267  // load config
268  if (file_exists($path.'config.php'))
269  {
270    include($path.'config.php');
271  }
272 
273  // load content
274  if ( $with_content and file_exists($path.'content.tpl') )
275  {
276    $template_conf['content'] = file_get_contents($path.'content.tpl');
277  }
278 
279  $template_conf['tpl_id'] = $dir;
280  return $template_conf;
281}
282
283?>
Note: See TracBrowser for help on using the repository browser.