root/form/import_profile.php

Revision 288, 13.8 kB (checked in by kevin, 8 months ago)

Corrected a bug in managing personal and community profile fields that have the same name. Also added the ability to receive form submit notifications.

Line 
1 <?php
2
3 /*
4  * Elgg Forms
5  * Kevin Jardine
6  * Radagast Solutions
7  * http://radagast.biz
8  *
9  * Reads the current profile.config.php file
10  * and stores the result in "profile" and
11  * "registration" forms.
12  *
13  */
14  
15 // Load Elgg framework
16 @require_once("../../includes.php");
17     
18 // Define context
19 define("context","form");
20
21 $title = __gettext("Profile import");
22
23 $return_bit = '<p>'.__gettext('You can return to the').' <a href="'.$CFG->wwwroot.'mod/form/index.php">'.__gettext('main form page by clicking here').'</a>.</p>';
24
25 if (isloggedin() && run("users:flags:get", array("admin", $_SESSION['userid']))) {
26
27     if (record_exists('forms','name','profile')) {
28         $body = '<p>'.__gettext('The form "profile" already exists. Please').' <a href="'.$CFG->wwwroot.'mod/form/index.php">'
29             .__gettext('rename or delete it').'</a> '.__gettext('before running this profile import.')."</p>\n".$return_bit;
30     } else if (record_exists('forms','name','registration')) {
31         $body = '<p>'.__gettext('The form "registration" already exists. Please').' <a href="'.$CFG->wwwroot.'mod/form/index.php">'
32             .__gettext('rename or delete it').'</a> '.__gettext('before running this profile import.')."</p>\n".$return_bit;
33     } else if (record_exists('forms','name','community_profile')) {
34         $body = '<p>'.__gettext('The form "community_profile" already exists. Please').' <a href="'.$CFG->wwwroot.'mod/form/index.php">'
35             .__gettext('rename or delete it').'</a> '.__gettext('before running this profile import.')."</p>\n".$return_bit;
36     } else if (record_exists('forms','name','join')) {
37         $body = '<p>'.__gettext('The form "join" already exists. Please').' <a href="'.$CFG->wwwroot.'mod/form/index.php">'
38             .__gettext('rename or delete it').'</a> '.__gettext('before running this profile import.')."</p>\n".$return_bit;
39     } else {
40         require_once($CFG->dirroot.'mod/profile/profile.config.php');
41         
42         // check for duplicate field names
43         $name_list = '';
44         $existing_field_list = '';
45         $field_list = '';
46         if (isset($data['profile:details']) && count($data['profile:details']) > 0) {
47             foreach($data['profile:details'] AS $field) {
48                 $field_list .= ",'{$field->internal_name}'";
49             }
50             $field_list = substr($field_list,1);
51             $results = get_records_sql("SELECT * FROM {$CFG->prefix}form_fields WHERE internal_name in ($field_list)");
52             if ($results) {
53                 foreach($results AS $field) {
54                     $existing_field_list .= ", {$field->internal_name}";
55                 }
56                 $existing_field_list  = substr($existing_field_list,2);
57             }
58         }
59         
60         if ($existing_field_list) {
61             $body = '<p>'.__gettext('The profile import cannot be carried out because the following profile fields are already defined'
62             .' in the database:').'</p><p>'.$existing_field_list.'</p>'."\n".$return_bit;
63         } else {           
64             $user_id = $_SESSION['userid'];
65             
66             // insert profile form
67             $form = new stdClass;
68             $form->name = 'profile';
69             $form->access = 'user'.$user_id;
70             $form->owner = $user_id;
71             $form->created = time();
72             $form->title = __gettext('Profile');
73             $form->description = __gettext('You can use this form for testing your profile fields - it should never be directly made public.');
74             $form->response_text = __gettext('Thanks for testing these profile fields.');
75             $profile_form_id = insert_record('forms',$form);
76             
77             // insert profile form
78             $form = new stdClass;
79             $form->name = 'community_profile';
80             $form->access = 'user'.$user_id;
81             $form->owner = $user_id;
82             $form->created = time();
83             $form->title = __gettext('Community Profile');
84             $form->description = __gettext('You can use this form for testing your community profile fields - it should never be directly made public.');
85             $form->response_text = __gettext('Thanks for testing these community profile fields.');
86             $community_profile_form_id = insert_record('forms',$form);
87             
88             // insert join form
89             $form = new stdClass;
90             $form->name = 'join';
91             $form->access = 'user'.$user_id;
92             $form->owner = $user_id;
93             $sitename = $CFG->sitename;
94             $form->created = time();
95             $form->title = __gettext('Join').' '.$sitename;
96             
97             $sitename = sitename;
98             $partOne = sprintf(__gettext("Thank you for registering for an account with %s! Registration is completely free, but before you fill in your details, please take a moment to read the following documents:"),$sitename); // gettext variable
99             $terms = __gettext("terms and conditions"); // gettext variable
100             $privacy = __gettext("Privacy policy"); // gettext variable
101             $partFour = __gettext("When you fill in the details below, we will send an \"invitation code\" to your email address in order to validate it. You must then click on this within seven days to create your account."); // gettext variable
102                             
103             $form->description = <<< END
104     <p>
105         $partOne
106     </p>
107     <ul>
108         <li><a href="{$CFG->wwwroot}content/terms.php" target="_blank">$sitename $terms</a></li>
109         <li><a href="{$CFG->wwwroot}content/privacy.php" target="_blank">$privacy</a></li>
110     </ul>
111     <p>
112         $partFour
113     </p>               
114 END;
115             $form->response_text = '';
116             insert_record('forms',$form);
117             
118             // insert registration form
119             $form = new stdClass;
120             $form->name = 'registration';
121             $form->access = 'user'.$user_id;
122             $form->owner = $user_id;
123             $sitename = $CFG->sitename;
124             $form->created = time();
125             $form->title = __gettext('Join').' '.$sitename;
126             $thankYou = sprintf(__gettext("Thank you for registering for an account with %s! Registration is completely free, but before you confirm your details, please take a moment to read the following documents:"), $sitename);
127             $terms = __gettext("terms and conditions"); // gettext variable
128             $privacy = __gettext("Privacy policy"); // gettext variable
129             $age = __gettext("Submitting the form below indicates acceptance of these terms. Please note that currently you must be at least 13 years of age to join the site."); // gettext variable
130             $form->description .= <<< END
131                 
132             <p>
133                 $thankYou
134             </p>
135             <ul>
136                 <li><a href="{$CFG->wwwroot}content/terms.php" target="_blank">$sitename $terms</a></li>
137                 <li><a href="{$CFG->wwwroot}content/privacy.php" target="_blank">$privacy</a></li>
138             </ul>
139             <p>
140                 $age
141             </p>
142 END;
143             $form->response_text = __gettext("Your account was created! You can now log in using the username and password you supplied. You have been sent an email containing these details for reference purposes.");
144         
145             $registration_form_id = insert_record('forms',$form);
146             
147             // insert the fields
148             
149             form_import_insert_fields($profile_form_id, $community_profile_form_id, $registration_form_id);
150
151             $body = '<p>'.__gettext('Your profile has been successfully imported into the forms "profile", "community_profile", "join" and "registration".').'</p>';
152             $body .= "\n".$return_bit;
153         }
154     }
155 } else {
156     $body = __gettext("You need to be logged-in as a site administrator to access this page.");
157 }
158
159
160 // Output to the screen
161 $body = templates_draw(array(
162                 'context' => 'contentholder',
163                 'title' => $title,
164                 'body' => $body
165             )
166 );
167
168 echo templates_page_draw( array(
169                 $title, $body
170             )
171 );
172
173     
174 function startsWith( $str, $sub ) {
175    return ( substr( $str, 0, strlen( $sub ) ) === $sub );
176 }
177             
178 function endsWith( $str, $sub ) {
179    return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
180 }
181
182 function form_import_insert_fields($profile_form_id, $community_profile_form_id, $registration_form_id) {
183     
184     global $CFG, $data;
185     
186     $profile_field_count = 0;
187     $community_profile_field_count = 0;
188     $registration_field_count = 0;
189           
190     if (isset($data['profile:details']) && count($data['profile:details']) > 0) {
191         foreach($data['profile:details'] AS $field) {
192             $fd = new stdClass;
193             $fd->internal_name = $field->internal_name;
194             $fd->original_internal_name = $field->internal_name;
195             $fd->title = $field->name;
196             $fd->description = $field->description;
197             $fd->category = $field->category;
198             $fd->default_access = isset($field->default_access)?$field->default_access:'';
199             $fd->required = isset($field->required)?$field->required:false;
200             $fd->invisible = isset($field->invisible)?$field->invisible:false;
201             $fd->profile = 1;
202             if (isset($field->col1) && $field->col1) {
203                 $fd->col = 1;
204             } else if (isset($field->col2) && $field->col2) {
205                 $fd->col = 2;
206             } else {
207                 $fd->col = 0;
208             }
209                 
210             if (isset($CFG->prfext_data) && isset($CFG->prfext_data[$field->field_type])) {
211                 $fd->field_type = 'choices';
212                 $d = $CFG->prfext_data[$field->field_type];
213                 $display_type = $d->display_type;
214                 $fd->is_keyword_tag = $d->is_keyword_tag;
215                 $fd->default_value = $d->default_value;
216                 
217                 if (endsWith($display_type,'radio')) {
218                     $fd->choice_type = 'radio';
219                     if (startsWith($display_type, 'profile_vertical')) {
220                         $fd->orientation = 'vertical';
221                     } else {
222                         $fd->orientation = 'horizontal';
223                     }
224                 } else if (endsWith($display_type,'checkbox')) {
225                     $fd->choice_type = 'checkbox';
226                     if (startsWith($display_type, 'profile_vertical')) {
227                         $fd->orientation = 'vertical';
228                     } else {
229                         $fd->orientation = 'horizontal';
230                     }
231                 } else {
232                     $fd->choice_type = 'checkbox';
233                     $fd->orientation = 'vertical';
234                 }
235             } else {
236                 $fd->field_type = $field->field_type;
237                 $fd->is_keyword_tag = 0;
238                 $fd->default_value = '';
239                 $fd->orientation = 'horizontal';
240             }
241             
242             // rename community-specific field to avoid potential conflict
243             // with person fields
244                           
245             if (!empty($field->user_type) && $field->user_type == 'community') {
246                 $fd->internal_name = 'community_'.$fd->internal_name;
247             }
248             $field_id = insert_record('form_fields',$fd);
249             
250             // set up field / form maps
251             
252             if (empty($field->user_type) || $field->user_type == 'person') {
253                 $map = new stdClass;           
254                 $map->form_id = $profile_form_id;
255                 $map->field_id = $field_id;
256                 $map->display_order = 10*($profile_field_count+1);
257                 insert_record('form_fields_map',$map);
258                 $profile_field_count++;
259             }
260             
261             if (empty($field->user_type) || $field->user_type == 'community') {
262                 $map = new stdClass;
263                 $map->form_id = $community_profile_form_id;
264                 $map->field_id = $field_id;
265                 $map->display_order = 10*($community_profile_field_count+1);
266                 insert_record('form_fields_map',$map);
267                 $community_profile_field_count++;
268             }
269             
270             if (isset($field->registration) && $field->registration) {           
271                 $map = new stdClass;
272                 $map->form_id = $registration_form_id;
273                 $map->field_id = $field_id;
274                 $map->display_order = 10*($registration_field_count+1);
275                 insert_record('form_fields_map',$map);
276                 $registration_field_count++;
277             }
278             
279             // insert options, if any
280             if ($fd->field_type == 'choices') {
281                 if (isset($d->options) and count($d->options) > 0) {
282                     if (prfext_array_is_associative ($d->options)) {
283                         foreach ($d->options AS $value => $label) {
284                             $option = new stdClass;
285                             $option->field_id = $field_id;
286                             $option->value = $value;
287                             $option->label = $label;
288                             insert_record('form_fields_choice',$option);
289                         }
290                     } else {
291                         foreach ($d->options AS $value) {
292                             $option = new stdClass;
293                             $option->field_id = $field_id;
294                             $option->value = $value;
295                             $option->label = '';
296                             insert_record('form_fields_choice',$option);
297                         }
298                     }
299                 }
300             }
301         }
302     }
303 }
304
305 ?>
Note: See TracBrowser for help on using the browser.