/[drupal]/contributions/modules/og_settings/og_settings.module
ViewVC logotype

Contents of /contributions/modules/og_settings/og_settings.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Tue Jun 3 20:33:22 2008 UTC (17 months, 3 weeks ago) by ferdi
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-6--1
File MIME type: text/x-php
initial release
1 <?php
2
3 // $Id: og_settings.module,v 1.4 2008/05/28 16:18:06 falimadh Exp $
4
5 /**
6 * menu hook
7 */
8 function og_settings_menu($may_cache) {
9 $gid = arg(1);
10 if ($may_cache) {
11 //
12 }
13 else {
14 global $og_settings_conf;
15 $og_settings_conf = _variable_init(isset($og_settings_conf) ? $og_settings_conf : array());
16 }
17 }
18
19 /**
20 * gets the value of a variable for a certain group
21 *
22 * @param unknown_type $gid
23 */
24 function og_settings_variable_get($gid, $name, $default) {
25 global $og_settings_conf;
26 //dpm('variable_get');
27
28 return isset($og_settings_conf[$gid][$name]) ? $og_settings_conf[$gid][$name] : $default;
29 }
30
31 /**
32 * sets a variable for a certain group
33 *
34 * @param unknown_type $gid
35 * @param unknown_type $name
36 * @param unknown_type $value
37 */
38 function og_settings_variable_set($gid, $name, $value) {
39
40 //dpm('variable_set');
41 global $og_settings_conf;
42
43 db_lock_table('og_settings');
44 db_query("DELETE FROM {og_settings} WHERE nid = %d AND name = '%s'", $gid, $name);
45 db_query("INSERT INTO {og_settings} (nid, name, value) VALUES (%d, '%s', '%s')", $gid, $name, serialize($value));
46 db_unlock_tables();
47
48 cache_clear_all('og_settings_variables', 'cache');
49
50 $og_settings_conf[$gid][$name] = $value;
51
52 }
53
54 /**
55 * Delete a variable
56 *
57 * @param int $gid
58 * @param string $name
59 */
60 function og_settings_variable_del($gid, $name) {
61 //dpm('variable_del');
62 global $og_settings_conf;
63
64 db_query("DELETE FROM {og_settings} WHERE nid = %d AND name = '%s'", $gid, $name);
65 cache_clear_all('og_settings_variables', 'cache');
66
67 unset($og_settings_conf[$gid][$name]);
68 }
69
70 function og_settings_variable_del_all($gid) {
71 //dpm('variable_del');
72 global $og_settings_conf;
73
74 db_query("DELETE FROM {og_settings} WHERE nid = %d", $gid);
75 cache_clear_all('og_settings_variables', 'cache');
76
77 unset($og_settings_conf[$gid]);
78 }
79
80 /**
81 * the group settings form
82 */
83 function og_settings_form($form, $gid) {
84 $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
85 $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') );
86 $form['og_settings_gid'] = array('#type' => 'hidden', '#value' => $gid);
87
88 if (!empty($_POST) && form_get_errors()) {
89 drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
90 }
91 $form['#base'] = 'og_settings_form';
92 return $form;
93 }
94
95 /**
96 * setting form submission
97 */
98 function og_settings_form_submit($form_id, $form_values) {
99 $gid = $form_values['og_settings_gid'];
100
101 $op = isset($form_values['op']) ? $form_values['op'] : '';
102
103 // Exclude unnecessary elements.
104 unset($form_values['submit'], $form_values['reset'], $form_values['form_id'], $form_values['op'], $form_values['form_token']);
105 unset($form_values['og_settings_gid'] );
106
107 foreach ($form_values as $key => $value) {
108 if ($op == t('Reset to defaults')) {
109 og_settings_variable_del($gid, $key);
110 }
111 else {
112 if (is_array($value) && isset($form_values['array_filter'])) {
113 $value = array_keys(array_filter($value));
114 }
115 og_settings_variable_set($gid, $key, $value);
116 }
117 }
118 if ($op == t('Reset to defaults')) {
119 drupal_set_message(t('The configuration options have been reset to their default values.'));
120 }
121 else {
122 drupal_set_message(t('The configuration options have been saved.'));
123 }
124
125 menu_rebuild(); // ???
126 }
127
128
129 /**
130 * Initializes the array of variables
131 */
132 function _variable_init($og_settings_conf = array()) {
133 //dpm('variable init');
134 if ($cached = cache_get('og_settings_variables', 'cache')) {
135 $variables = unserialize($cached->data);
136 }
137 else {
138 $result = db_query('SELECT * FROM {og_settings}');
139 while ($variable = db_fetch_object($result)) {
140 $variables[$variable->nid][$variable->name] = unserialize($variable->value);
141 }
142 cache_set('og_settings_variables', 'cache', serialize($variables));
143
144 }
145
146 foreach ($og_settings_conf as $nid => $array) {
147 $variables[$nid] = $array;
148 }
149
150 return $variables;
151 }

  ViewVC Help
Powered by ViewVC 1.1.2