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

Contents of /contributions/modules/profile_enforcer/profile_enforcer.module

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


Revision 1.4 - (show annotations) (download) (as text)
Sat Jun 9 19:08:52 2007 UTC (2 years, 5 months ago) by suydam
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +247 -247 lines
File MIME type: text/x-php
Initial commit of Multivendor-Multigateway ecommerce payment method module.  This is still very alpha and proprietary to the site for which it's being developed.  Public release coming ASAP.
1 <?php
2 // $Id: profile_enforcer.module,v 1.2 2007/05/30 02:26:14 suydam Exp $
3
4 /**
5 * @file
6 * Requires users to fill out their profile before proceeding
7 *
8 * Deploys a block that checks whether or not you've filled out your
9 * profile. If you have not, we direct you right to it.
10 */
11
12 /* hook_menu() */
13 function profile_enforcer_menu( $may_cache ) {
14 $items = array();
15 if ( $may_cache ) {
16 $items[] = array (
17 'path' => 'admin/user/profile_enforcer',
18 'title' => t('Profile Enforcer Settings'),
19 'description' => t('Displays a block that checks whether or not a profile (or other rquired fields) has been completed. Requires completion if it has not.'),
20 'callback' => 'drupal_get_form',
21 'callback arguments' => array('profile_enforcer_admin_settings'),
22 'access' => user_access('administer site configuration')
23 );
24 }
25 return $items;
26 }
27
28 function profile_enforcer_admin_settings() {
29 //drupal_set_message('FYI: You will also need to enable the block titled "Profile Enforcer" to get this module fully operational.');
30 if ( module_exists('profile') ) {
31 $form['profile_enforcer_profile'] = array(
32 '#type' => 'radios',
33 '#title' => t('Require User Profile (all users)?'),
34 '#options' => array(
35 '1' => t('Yes'),
36 '0' => t('No')
37 ),
38 '#description' => t( 'If Yes, the blocked titled "Profile Enforcer" will force users to have filled out all required fields in their profile before going anywhere else that has this block deployed.'),
39 '#default_value' => variable_get('profile_enforcer_profile','0')
40 );
41 $form['profile_enforcer_profile_url'] = array(
42 '#type' => 'textfield',
43 '#title' => t('URL for redirection if profile information is missing?'),
44 '#description' => t('Where should we send users who are missing profile information? (User %uid to signal uid)'),
45 '#default_value' => variable_get('profile_enforcer_profile_url','user/%uid/edit')
46 );
47 }
48
49 if ( module_exists('location') ) {
50 $form['profile_enforcer_location'] = array(
51 '#type' => 'radios',
52 '#title' => t('Require User to enter Location (all users)?'),
53 '#options' => array(
54 '1' => t('Yes'),
55 '0' => t('No')
56 ),
57 '#description' => t('Require users to enter a location for themselves?'),
58 '#default_value' => variable_get('profile_enforcer_location','0')
59 );
60 $form['profile_enforcer_location_url'] = array(
61 '#type' => 'textfield',
62 '#title' => t('URL for redirection if location information is missing?'),
63 '#description' => t('Where should we send users who are missing location information? (User %uid to signal uid)'),
64 '#default_value' => variable_get('profile_enforcer_location_url','user/%uid/edit')
65 );
66 }
67
68 $form['profile_enforcer_other_fields'] = array(
69 '#type' => 'textarea',
70 '#title' => 'Other Fields',
71 '#description' => t('List other required profile fields here with a URL for redirection (e.g. ec_address.street1|uid/%uidi/GZPaypalConfig). Separate fields with commas and separate the field and url with a | character. NOTE: Requires uid for key-link to $user->uid'),
72 '#cols' => 40,
73 '#rows' => 3,
74 '#resizable' => TRUE, '#default_value' => variable_get('profile_enforcer_other_fields','')
75 );
76
77 $form['profile_enforcer_debugger'] = array(
78 '#type' => 'radios',
79 '#title' => t('Enable debugging mode?'),
80 '#options' => array(
81 '1' => t('Yes'),
82 '0' => t('No')
83 ),
84 '#description' => t('I find it helpful to have this block show what it\'s doing sometimes. When debugging mode is
85 ON the block will output the results of its various tests. When debugging mode is off, the block
86 will stay completely silent (unless you override in <a href="admin/build/blocks">admin/build/blocks</a>)'),
87 '#default_value' => variable_get('profile_enforcer_debugger','1')
88 );
89
90 $form['profile_enforcer_profile_message'] = array(
91 '#type' => 'textarea',
92 '#title' => 'Error message (Profile Missing)',
93 '#description' => t('This error message is used when their profile is complete.'),
94 '#cols' => 40,
95 '#rows' => 3,
96 '#resizable' => TRUE, '#default_value' => variable_get('profile_enforcer_profile_message','')
97 );
98
99 return system_settings_form( $form );
100
101 }
102
103 function profile_enforcer_block( $op='list', $delta=0, $edit=array() ) {
104 switch( $op ) {
105 case 'list':
106 $blocks[0]['info'] = t('Profile Enforcer');
107 return $blocks;
108 case 'view':
109 if ( !isset($user) ) {
110 global $user;
111 }
112 if ( variable_get('profile_enforcer_debugger','0') ) {
113 $block['subject'] = t('PROFILE ENFORCER (DEBUGGING)');
114 }
115
116 // did they finish their profile?
117 if ( variable_get('profile_enforcer_profile','0') && module_exists('profile') && !profile_complete() ) {
118 if ( variable_get('profile_enforcer_debugger','0') ) {
119 $items[] = 'PROFILE IS BOUncING YOU';
120 }
121 prbouncer('profile', preg_replace('/%uid/',$user->uid,variable_get('profile_enforcer_profile_url','0')) );
122 }
123 // did they put in a location?
124 elseif ( variable_get('profile_enforcer_location','0') && module_exists('location') && !location_complete() ) {
125 if ( variable_get('profile_enforcer_debugger','0') ) {
126 $items[] = 'Location is bouncing you ';
127 }
128 //$items[] = "<PRE>" . sprint_r( location_load_locations( 'user',$user ) ) . "</pre>";
129 prbouncer('location information', preg_replace('/%uid/',$user->uid,variable_get('profile_enforcer_location_url','0')) );
130 }
131
132
133
134 // debugging output only
135 if ( variable_get('profile_enforcer_debugger','0') ) {
136 $items[] = 'PROFILE IS COMPLETE!';
137 }
138 if ( variable_get('profile_enforcer_debugger','0') ) {
139 $items[] = 'LOCATION INFO IS COMPLETE!';
140 }
141
142
143 // are there any other fields?
144 if ( variable_get('profile_enforcer_other_fields','0') != '' ) {
145 $fields = preg_split('/,/', variable_get('profile_enforcer_other_fields','0'));
146 foreach ($fields as $a_field) {
147 list($field,$url) = preg_split('/\|/', $a_field);
148 list($tab,$fld) = preg_split('/\./', $field);
149 $statement = "SELECT COUNT($fld) FROM $tab WHERE uid = " . $user->uid;
150 $sth = mysql_query( $statement ) or die('SQL ERROR: $statement<HR>' . mysql_error());
151 list($thecount) = mysql_fetch_row($sth);
152 if ( $thecount ) {
153 if ( variable_get('profile_enforcer_debugger','0') ) {
154 $items[] = "TEST PASSED FOR $tab.$fld";
155 }
156 }
157 else {
158 if ( variable_get('profile_enforcer_debugger','0') ) {
159 $items[] = "Required field is empty: $tab.$fld<BR>$statement<BR>C=$thecount";
160 }
161 prbouncer("$tab.$fld",$url);
162 }
163 }
164 }
165 if ( variable_get('profile_enforcer_debugger','0') ) {
166 $block['content'] = theme('item_list',$items);
167 }
168 return $block;
169 }
170 }
171
172 function location_complete() {
173 // apparently eid = uid (who knew?!)
174 global $user;
175 $locations = location_load_locations( 'user', $user );
176 foreach ($locations['locations'] as $location) {
177 if ( isset($location['street']) && isset( $location['city'] ) && $location['street'] != '' && $location['city'] != '' ) {
178 return 1;
179 }
180 }
181 return 0;
182
183
184 }
185 function profile_complete() {
186 // get all required fields from the profile
187 // check if they ALL exist for this userj
188 // 0 = incomplete profile
189 // >0 = complete
190
191 if ( !isset($user) ) {
192 global $user;
193 }
194
195 if ( module_exists( 'profile' ) ) {
196 $statement = "
197 SELECT COUNT(*)
198 FROM users u
199 LEFT JOIN profile_fields pf ON 1
200 LEFT JOIN profile_values pv ON ( pf.fid = pv.fid AND pv.uid = u.uid)
201 WHERE pf.required=1 AND u.uid=7 AND pv.value IS NULL";
202 $sth = mysql_query( $statement );
203 list( $thecount ) = mysql_fetch_row( $sth );
204 if ($thecount) {
205 // there are fiels not finished
206 return 0;
207 }
208 else {
209 return 1;
210 }
211 }
212 }
213
214 if (!function_exists('sprint_r')) {
215 function sprint_r($var) {
216 ob_start();
217 print_r($var);
218 $ret = ob_get_contents();
219 ob_end_clean();
220 return $ret;
221 }
222 }
223
224 // this internal functio handles the redirction for people who haven't filled in necessary information yet.
225 function prbouncer($req_info='profile', $dest_url='user' ) {
226 // bounce them if they're not on an edit page.
227 $current_url = preg_replace('/^\//','',$_SERVER['REQUEST_URI']);
228 if ( $dest_url == $current_url ) {
229 drupal_set_message( variable_get('profile_enforcer_profile_message','') ,'error');
230 }
231 // if they're on ANY user page leave'em alone
232 elseif ( preg_match('/user/',$current_url) ) {
233 drupal_set_message( variable_get('profile_enforcer_profile_message','') ,'error');
234 }
235 else {
236 global $user;
237 if ( variable_get('profile_enforcer_debugger','0') ) {
238 drupal_set_message("Profile Enforcer Debuggin: Requried information ($req_info) is not complete. Redir would go to $dest_url (but we're in debugging mode). You are here: $current_url",'error');
239 }
240 else {
241 drupal_goto($dest_url);
242 //drupal_set_message("Profile Enforcer Debuggin: Requried information ($req_info) is not complete. Redir would go to $dest_url (but we're in debugging mode) You are here: $current_url",'error');
243 }
244 }
245 }
246
247

  ViewVC Help
Powered by ViewVC 1.1.2