root/elgg/trunk/profile/edit.php

Revision 33, 7.3 kB (checked in by hopsonro, 2 years ago)

Import Elgg 0.9.1 release.

Line 
1 <?php
2
3 //    ELGG profile edit page
4
5 global $profile_name, $profile_id, $messages;
6
7 // Run includes
8 require_once(dirname(dirname(__FILE__))."/includes.php");
9 require_once($CFG->dirroot . "profile/profile.class.php");
10
11 // define what profile to show
12 $profile_name = optional_param('profile_name', '', PARAM_ALPHANUM);
13 if (!empty($profile_name)) {
14     $profile_id = user_info_username('ident', $profile_name);
15 }
16 if (empty($profile_id)) {
17     // fetch from GET/POST param
18     $profile_id = page_owner();
19
20     // if it wasn't in GET/POST but we have a valid session, use it
21     if ($profile_id === -1 && isset($_SESSION['userid'])) {
22         $profile_id = $_SESSION['userid'];
23     }
24 }
25 $profile_name = user_info('username', $profile_id);
26
27 // init library
28 $profile = new ElggProfile($profile_id);
29
30 define("context", "profile");
31
32 protect(1);
33
34 global $page_owner, $metatags, $CFG, $data;
35
36 if (isset($_SESSION['profile:preload'])) {
37     $data['profile:preload'] = $_SESSION['profile:preload'];
38     unset($_SESSION['profile:preload']);
39 }
40 if (isset($_SESSION['profile:preload:access'])) {
41     $data['profile:preload:access'] = $_SESSION['profile:preload:access'];
42     unset($_SESSION['profile:preload:access']);
43 }
44
45 $title = user_name($page_owner) . " :: ". __gettext("Edit profile") ."";
46 templates_page_setup();
47
48 $metatags .= "<script type=\"text/javascript\" src=\"" . $CFG->wwwroot . "mod/profile/tabber/tabber.js\"></script>";
49 $metatags .= "<link rel=\"stylesheet\" href=\"" . $CFG->wwwroot . "mod/profile/tabber/example.css\" type=\"text/css\" media=\"screen\" />";
50
51 if ($profile_new = data_submitted()) {
52     $body = profile_update($profile_new);
53 } else {
54     $body = $profile->display_form();
55 }   
56
57 templates_page_output($title, $body);
58
59 function profile_update($profile_new) {
60
61     global $CFG;
62     global $data;
63     global $messages;
64     global $page_owner;
65     global $profile_name;
66         
67     $profiledetails = optional_param('profiledetails',array());
68     if (count($profiledetails) > 0) {
69         // delete_records('profile_data','owner',$page_owner);
70         
71         $insertvalues = array();
72         $requiredmissing = array();
73         
74         foreach($profiledetails as $field => $value) {
75             $field = trim($field);
76             $value = trim($value);
77           
78             if (!empty($value)) {
79                 //TODO get rid of variable duplication here. (Penny)
80                 if (!empty($data['profile:details'][$field]->invisible)) {
81                     $access = 'user' . $page_owner;
82                 } else {
83                     $access = $_POST['profileaccess'][$field];
84                 }
85
86                 $pd = new StdClass;
87                 $pd->name   = $field;
88                 $pd->value  = $value;
89                 $pd->access = $access;
90                 $pd->owner  = $page_owner;
91
92                 // $insert_id  = insert_record('profile_data',$pd);
93                 $insertvalues[] = $pd;
94                 
95             } else {
96                 foreach($data['profile:details'] as $datatype) {
97                     if (is_array($datatype)) {
98                         $fname  = !empty($datatype[1]) ? $datatype[1] : '';
99                         $flabel = !empty($field[0]) ? $field[0] : '';
100                         $frequired = false;
101                         $fcat = __gettext("Main");
102                     // Otherwise map things the new way!
103                     } else {
104                         $fname = $datatype->internal_name;
105                         $flabel = $datatype->name;
106                         $frequired = $datatype->required;
107                         if (empty($datatype->category)) {
108                             $fcat = __gettext("Main");
109                         } else {
110                             $fcat = $datatype->category;
111                         }
112                     }
113                     if ($fname == $field) {
114                         if ($frequired == true) {
115                             $requiredmissing[] = sprintf(__gettext("%s (in category %s)"),$flabel,$fcat);
116                         } else {
117                             delete_records('profile_data','owner',$page_owner,'name',$fname);
118                         }
119                     }
120                 }
121             }
122         }
123         if (sizeof($requiredmissing) == 0) {
124             
125             $updatedok = true;
126             
127             foreach($insertvalues as $insertvalue) {
128                 delete_records('profile_data','owner',$page_owner,'name',$insertvalue->name);
129                 $insertvalue = plugin_hook("profile_data","create",$insertvalue);
130                 if (!empty($insertvalue)) {
131                     $insert_id  = insert_record('profile_data',$insertvalue);
132                     $insertvalue->ident = $insert_id;
133                     plugin_hook("profile_data","publish",$insertvalue);
134                     foreach($data['profile:details'] as $datatype) {
135                         if (is_array($datatype)) {
136                             $fname = !empty($datatype[1]) ? $datatype[1] : '';
137                             $ftype = !empty($datatype[2]) ? $datatype[2] : '';
138                         // Otherwise map things the new way!
139                         } else {
140                             $fname = $datatype->internal_name;
141                             $ftype = $datatype->field_type;
142                         }
143                         if ($fname == $insertvalue->name && $ftype == "keywords") {
144                             delete_records('tags', 'tagtype', $insertvalue->name, 'owner', $page_owner);
145                             $value = insert_tags_from_string ($insertvalue->value, $insertvalue->name, $insert_id, $insertvalue->access, $page_owner);
146                         }
147                         if (isset($CFG->display_field_module[$ftype])) {
148                             $callback = $CFG->display_field_module[$ftype] . "_validate_input_field";
149                             $updatedok = $callback($insertvalue);
150                         }
151                     }
152                 }
153             }
154             $messages[] = __gettext("Profile updated.");
155         } else {
156             
157             $savedata = array();
158             
159             foreach($insertvalues as $insertvalue) {
160                 $savedata['profile:preload'][$insertvalue->name] = $insertvalue->value;
161                 $savedata['profile:preload:access'][$insertvalue->name] = $insertvalue->access;
162             }
163             foreach($requiredmissing as $key=> $missinglabel) {
164                 $message = "";
165                 if ($key > 0) {
166                     $message .= ", ";
167                 }
168                 $message .= $missinglabel;
169             }
170             
171             $messages[] = sprintf(__gettext("You need to fill in the following required fields: %s"),$message);
172             
173             $updatedok = false;
174             $_SESSION['profile:preload'] = $savedata['profile:preload'];
175             $_SESSION['profile:preload:access'] = $savedata['profile:preload:access'];
176         }
177     }
178
179     // Changes saved successfully, update RSS feeds
180     $rssresult = run("weblogs:rss:publish", array(1, false));
181     $rssresult = run("profile:rss:publish", array(1, false));
182
183     $_SESSION['messages'] = $messages;
184     
185     // redirect("{$CFG->wwwroot}{$profile_name}", get_string("changessaved"));
186     if ($updatedok) {
187         redirect("{$CFG->wwwroot}{$profile_name}/profile/", "");
188     } else {
189         redirect("{$CFG->wwwroot}profile/edit.php?profile_id=".$page_owner, "");
190     }
191 }
192
193 ?>
Note: See TracBrowser for help on using the browser.