1<?php
2
3/*
4 * This plugin is used to extend the administrator table to add user address fields.
5 *
6 * <b>NOTE:</b> you must run setup after enabling or disabling this plugin to cause changes to
7 * be made to the database. (Database changes should not be made on an active site.
8 * You should close the site when you run setup.) If you disable the plugin all data
9 * contained in the fields will be discarded.
10 *
11 * @author Stephen Billard (sbillard)
12 * @package plugins
13 * @subpackage useraddressfields
14 *
15 */
16$plugin_is_filter = 5 | CLASS_PLUGIN;
17$plugin_description = gettext('Adds user address fields');
18$plugin_author = "Stephen Billard (sbillard)";
19$plugin_category = gettext('Users');
20
21require_once(SERVERPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER . '/common/fieldExtender.php');
22
23class userAddressFields extends fieldExtender {
24
25	function __construct() {
26		global $_userAddressFields;
27		$firstTime = extensionEnabled('userAddressFields') && is_null(getOption('userAddressFields_addedFields'));
28		parent::constructor('userAddressFields', self::fields());
29		if ($firstTime) { //	migrate the custom data user data
30			$result = query('SELECT * FROM ' . prefix('administrators') . ' WHERE `valid`!=0');
31			if ($result) {
32				while ($row = db_fetch_assoc($result)) {
33					$custom = getSerializedArray($row['custom_data']);
34					if (!empty($custom)) {
35						$sql = 'UPDATE ' . prefix('administrators') . ' SET ';
36						foreach ($custom as $field => $val) {
37							$sql.= '`' . $field . '`=' . db_quote($val) . ',';
38						}
39						$sql .= '`custom_data`=NULL WHERE `id`=' . $row['id'];
40						query($sql);
41					}
42				}
43				db_free_result($result);
44			}
45		}
46	}
47
48	static function fields() {
49		return array(
50						array('table' => 'administrators', 'name' => 'street', 'desc' => gettext('Street'), 'type' => 'tinytext'),
51						array('table' => 'administrators', 'name' => 'website', 'desc' => gettext('Website'), 'type' => 'tinytext'),
52						array('table' => 'administrators', 'name' => 'city', 'desc' => gettext('City'), 'type' => 'tinytext'),
53						array('table' => 'administrators', 'name' => 'country', 'desc' => gettext('Country'), 'type' => 'tinytext'),
54						array('table' => 'administrators', 'name' => 'state', 'desc' => gettext('State'), 'type' => 'tinytext'),
55						array('table' => 'administrators', 'name' => 'postal', 'desc' => gettext('Postal code'), 'type' => 'tinytext')
56		);
57	}
58
59	static function addToSearch($list) {
60		return parent::_addToSearch($list, self::fields());
61	}
62
63	static function adminSave($updated, $userobj, $i, $alter) {
64		parent::_adminSave($updated, $userobj, $i, $alter, self::fields());
65	}
66
67	static function adminEdit($html, $userobj, $i, $background, $current) {
68		return parent::_adminEdit($html, $userobj, $i, $background, $current, self::fields());
69	}
70
71	static function mediaItemSave($object, $i) {
72		return parent::_mediaItemSave($object, $i, self::fields());
73	}
74
75	static function mediaItemEdit($html, $object, $i) {
76		return parent::_mediaItemEdit($html, $object, $i, self::fields());
77	}
78
79	static function zenpageItemSave($custom, $object) {
80		return parent::_zenpageItemSave($custom, $object, self::fields());
81	}
82
83	static function zenpageItemEdit($html, $object) {
84		return parent::_zenpageItemEdit($html, $object, self::fields());
85	}
86
87	static function register() {
88		parent::_register('userAddressFields', self::fields());
89	}
90
91	static function adminNotice($tab, $subtab) {
92		parent::_adminNotice($tab, $subtab, 'userAddressFields');
93	}
94
95	static function getCustomData($obj) {
96		return parent::_getCustomData($obj, self::fields());
97	}
98
99	static function setCustomData($obj, $values) {
100		parent::_setCustomData($obj, $values);
101	}
102
103}
104
105if (OFFSET_PATH == 2) { // setup call: add the fields into the database
106	setOptionDefault('zp_plugin_userAddressFields', $plugin_is_filter);
107	new userAddressFields;
108} else {
109	userAddressFields::register();
110}
111?>
112