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: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:
42:
43: class fieldExtender {
44:
45: 46: 47: 48: 49: 50: 51: 52: 53:
54: function constructor($me, $newfields) {
55: $previous = getSerializedArray(getOption(get_class($this) . '_addedFields'));
56: $current = $fields = array();
57: if (extensionEnabled($me)) {
58: foreach ($newfields as $newfield) {
59: $current[$newfield['table']][$newfield['name']] = true;
60: unset($previous[$newfield['table']][$newfield['name']]);
61: switch (strtolower($newfield['type'])) {
62: default:
63: $dbType = strtoupper($newfield['type']);
64: break;
65: case 'int':
66: case 'varchar':
67: $dbType = strtoupper($newfield['type']) . '(' . min(255, $newfield['size']) . ')';
68: break;
69: }
70: $sql = 'ALTER TABLE ' . prefix($newfield['table']) . ' ADD COLUMN `' . $newfield['name'] . '` ' . $dbType;
71: if (query($sql, false) && in_array($newfield['table'], array('albums', 'images', 'news', 'news_categories', 'pages')))
72: $fields[] = strtolower($newfield['name']);
73: }
74: setOption(get_class($this) . '_addedFields', serialize($current));
75: } else {
76: purgeOption(get_class($this) . '_addedFields');
77: }
78:
79: $set_fields = array_flip(explode(',', getOption('search_fields')));
80: foreach ($previous as $table => $orpahed) {
81: foreach ($orpahed as $field => $v) {
82: unset($set_fields[$field]);
83: $sql = 'ALTER TABLE ' . prefix($table) . ' DROP `' . $field . '`';
84: query($sql, false);
85: }
86: }
87: $set_fields = array_unique(array_merge($fields, array_flip($set_fields)));
88: setOption('search_fields', implode(',', $set_fields));
89: }
90:
91: 92: 93: 94: 95:
96: static function _addToSearch($list, $fields) {
97: foreach ($fields as $newfield) {
98: if (in_array($newfield['table'], array('albums', 'images', 'news', 'news_categories', 'pages'))) {
99: $list[strtolower($newfield['name'])] = $newfield['desc'];
100: }
101: }
102: return $list;
103: }
104:
105: 106: 107: 108: 109: 110: 111: 112: 113:
114: static function _adminSave($updated, $userobj, $i, $alter, $fields) {
115: if ($userobj->getValid()) {
116: foreach ($fields as $field) {
117: if (isset($_POST[$field['name'] . '_' . $i])) {
118: if ($field['table'] == 'administrators') {
119: $olddata = $userobj->get($field['name']);
120: $userobj->set($field['name'], $newdata = $_POST[$field['name'] . '_' . $i]);
121: if ($olddata != $newdata) {
122: $updated = true;
123: }
124: }
125: }
126: }
127: }
128: return $updated;
129: }
130:
131: 132: 133: 134: 135: 136: 137: 138: 139: 140:
141: static function _adminEdit($html, $userobj, $i, $background, $current, $fields) {
142: $list = array();
143: foreach ($fields as $field) {
144: if ($field['table'] == 'administrators') {
145: $input = '<fieldset>' .
146: '<legend>' . $field['desc'] . '</legend>';
147: if (in_array(strtolower($field['type']), array('varchar', 'int', 'tinytext'))) {
148: $input .= '<input name = "' . $field['name'] . '_' . $i . '" type = "text" size = "' . TEXT_INPUT_SIZE . '" value = "' . html_encode($userobj->get($field['name'])) . '" />';
149: } else {
150: $input .= '<textarea name = "' . $field['name'] . '_' . $i . '" cols = "' . TEXTAREA_COLUMNS . '"rows = "1">' . html_encode($userobj->get($field['name'])) . '</textarea>';
151: }
152:
153: $input .='</fieldset>';
154: $list[] = $input;
155: }
156: }
157: if (($count = count($list)) % 2) {
158: $list[] = '';
159: }
160:
161: if (!empty($list)) {
162: for ($key = 0; $key < $count; $key = $key + 2) {
163: $html .=
164: '<tr' . ((!$current) ? ' style = "display:none;"' : '') . ' class = "userextrainfo">' .
165: '<td width = "20%"' . ((!empty($background)) ? ' style = "' . $background . '"' : '') . ' valign = "top">' .
166: $list[$key] .
167: '</td>' .
168: '<td ' . ((!empty($background)) ? ' style = "' . $background . '"' : '') . ' valign = "top">' .
169: $list[$key + 1] .
170: '</td>' .
171: '</tr>';
172: }
173: }
174: return $html;
175: }
176:
177: 178: 179: 180: 181:
182: static function _mediaItemSave($object, $i, $fields) {
183: foreach ($fields as $field) {
184: if ($field['table'] == $object->table) {
185: $olddata = $object->get($field['name']);
186: $object->set($field['name'], $newdata = $_POST[$field['name'] . '_' . $i]);
187: if ($olddata != $newdata) {
188: $updated = true;
189: }
190: }
191: }
192: }
193:
194: 195: 196: 197: 198: 199: 200: 201:
202: static function _mediaItemEdit($html, $object, $i, $fields) {
203: foreach ($fields as $field) {
204: if ($field['table'] == $object->table) {
205: $html .= '<tr><td>' . $field['desc'] . '</td><td>';
206: if (in_array(strtolower($field['type']), array('varchar', 'int', 'tinytext'))) {
207: $html .= '<input name = "' . $field['name'] . '_' . $i . '" type = "text" style = "width:100%;" value = "' . html_encode($object->get($field['name'])) . '" />';
208: } else {
209: $html .= '<textarea name = "' . $field['name'] . '_' . $i . '" style = "width:100%;" rows = "6">' . html_encode($object->get($field['name'])) . '</textarea>';
210: }
211:
212: $html .='</td></tr>';
213: }
214: }
215: return $html;
216: }
217:
218: 219: 220: 221: 222: 223: 224:
225: static function _zenpageItemSave($custom, $object, $fields) {
226: foreach ($fields as $field) {
227: if ($field['table'] == $object->table) {
228: $olddata = $object->get($field['name']);
229: $object->set($field['name'], $newdata = $_POST[$field['name']]);
230: if ($olddata != $newdata) {
231: $updated = true;
232: }
233: }
234: }
235: return $custom;
236: }
237:
238: 239: 240: 241: 242: 243: 244:
245: static function _zenpageItemEdit($html, $object, $fields) {
246: foreach ($fields as $field) {
247: if ($field['table'] == $object->table) {
248: $html .= '<tr><td>' . $field['desc'] . '</td><td>';
249: if (in_array(strtolower($field['type']), array('varchar', 'int', 'tinytext'))) {
250: $html .= '<input name="' . $field['name'] . '" type="text" style = "width:97%;"
251: value="' . html_encode($object->get($field['name'])) . '" />';
252: } else {
253: $html .= '<textarea name = "' . $field['name'] . '" style = "width:97%;" "rows="6">' . html_encode($object->get($field['name'])) . '</textarea>';
254: }
255: }
256: }
257: return $html;
258: }
259:
260: 261: 262:
263: static function _register($me, $fields) {
264: zp_register_filter('searchable_fields', "$me::addToSearch");
265: $items = array();
266: foreach ($fields as $field) {
267: $items[$field['table']] = true;
268: }
269: if (isset($items['albums'])) {
270: zp_register_filter("save_album_utilities_data", "$me::mediaItemSave");
271: zp_register_filter("edit_album_custom_data", "$me::mediaItemEdit");
272: }
273: if (isset($items['images'])) {
274: zp_register_filter("save_image_utilities_data", "$me::mediaItemSave");
275: zp_register_filter("edit_image_custom_data", "$me::mediaItemEdit");
276: }
277: if (isset($items['administrators'])) {
278: zp_register_filter("save_admin_custom_data", "$me::adminSave");
279: zp_register_filter("edit_admin_custom_data", "$me::adminEdit");
280: }
281: if (isset($items['news'])) {
282: zp_register_filter("save_article_custom_data", "$me::zenpageItemSave");
283: zp_register_filter("edit_article_custom_data", "$me::zenpageItemEdit");
284: }
285: if (isset($items['news_categories'])) {
286: zp_register_filter("save_category_custom_data", "$me::zenpageItemSave");
287: zp_register_filter("edit_category_custom_data", "$me::zenpageItemEdit");
288: }
289: if (isset($items['pages'])) {
290: zp_register_filter("save_page_custom_data", "$me::zenpageItemSave");
291: zp_register_filter("edit_page_custom_data", "$me::zenpageItemEdit");
292: }
293: if (OFFSET_PATH && !getOption($me . "_addedFields")) {
294: zp_register_filter('admin_note', "$me::adminNotice");
295: }
296: }
297:
298: 299: 300: 301: 302: 303: 304:
305: static function _adminNotice($tab, $subtab, $me) {
306: echo '<p class="notebox">' . sprintf(gettext('You will need to run <a href="%1$s">setup</a> to update the database with the custom fields defined by the <em>%2$s</em> plugin.'), FULLWEBPATH . '/' . ZENFOLDER . '/setup.php', $me) . '</p>';
307: return $tab;
308: }
309:
310: 311: 312: 313: 314: 315:
316: static function _getCustomData($obj, $fields) {
317: $result = array();
318: foreach ($fields as $element) {
319: if ($element['table'] == $obj->table) {
320: $result[$element['name']] = $obj->get($element['name']);
321: }
322: }
323: return $result;
324: }
325:
326: static function _setCustomData($obj, $values) {
327: foreach ($values as $field => $value) {
328: $obj->set($field, $value);
329: }
330: }
331:
332: }
333:
334: ?>
335: