root/releases/0.8rc1/profile/edit.php

Revision 1054, 7.5 kB (checked in by ben, 2 years ago)

Profile edit fix.

  • Property svn:eol-style set to native
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 $body = templates_draw(array( 'context' => 'contentholder',
57                               'title' => $title,
58                               'body' => $body   ));
59
60 print templates_page_draw(array($title, $body));
61
62
63
64 function profile_update($profile_new) {
65
66     global $CFG;
67     global $data;
68     global $messages;
69     global $page_owner;
70     global $profile_name;
71         
72     $profiledetails = optional_param('profiledetails',array());
73     if (count($profiledetails) > 0) {
74         // delete_records('profile_data','owner',$page_owner);
75         
76         $insertvalues = array();
77         $requiredmissing = array();
78         
79         foreach($profiledetails as $field => $value) {
80             $field = trim($field);
81             $value = trim($value);
82           
83             if (!empty($value)) {
84                 //TODO get rid of variable duplication here. (Penny)
85                 if (!empty($data['profile:details'][$field]->invisible)) {
86                     $access = 'user' . $page_owner;
87                 } else {
88                     $access = $_POST['profileaccess'][$field];
89                 }
90
91                 $pd = new StdClass;
92                 $pd->name   = $field;
93                 $pd->value  = $value;
94                 $pd->access = $access;
95                 $pd->owner  = $page_owner;
96
97                 // $insert_id  = insert_record('profile_data',$pd);
98                 $insertvalues[] = $pd;
99                 
100             } else {
101                 foreach($data['profile:details'] as $datatype) {
102                     if (is_array($datatype)) {
103                         $fname  = !empty($datatype[1]) ? $datatype[1] : '';
104                         $flabel = !empty($field[0]) ? $field[0] : '';
105                         $frequired = false;
106                         $fcat = __gettext("Main");
107                     // Otherwise map things the new way!
108                     } else {
109                         $fname = $datatype->internal_name;
110                         $flabel = $datatype->name;
111                         $frequired = $datatype->required;
112                         if (empty($datatype->category)) {
113                             $fcat = __gettext("Main");
114                         } else {
115                             $fcat = $datatype->category;
116                         }
117                     }
118                     if ($fname == $field) {
119                         if ($frequired == true) {
120                             $requiredmissing[] = sprintf(__gettext("%s (in category %s)"),$flabel,$fcat);
121                         } else {
122                             delete_records('profile_data','owner',$page_owner,'name',$fname);
123                         }
124                     }
125                 }
126             }
127         }
128         if (sizeof($requiredmissing) == 0) {
129             
130             $updatedok = true;
131             
132             foreach($insertvalues as $insertvalue) {
133                 delete_records('profile_data','owner',$page_owner,'name',$insertvalue->name);
134                 $insertvalue = plugin_hook("profile_data","create",$insertvalue);
135                 if (!empty($insertvalue)) {
136                     $insert_id  = insert_record('profile_data',$insertvalue);
137                     $insertvalue->ident = $insert_id;
138                     plugin_hook("profile_data","publish",$insertvalue);
139                     foreach($data['profile:details'] as $datatype) {
140                         if (is_array($datatype)) {
141                             $fname = !empty($datatype[1]) ? $datatype[1] : '';
142                             $ftype = !empty($datatype[2]) ? $datatype[2] : '';
143                         // Otherwise map things the new way!
144                         } else {
145                             $fname = $datatype->internal_name;
146                             $ftype = $datatype->field_type;
147                         }
148                         if ($fname == $insertvalue->name && $ftype == "keywords") {
149                             delete_records('tags', 'tagtype', $insertvalue->name, 'owner', $page_owner);
150                             $value = insert_tags_from_string ($insertvalue->value, $insertvalue->name, $insert_id, $insertvalue->access, $page_owner);
151                         }
152                         if (isset($CFG->display_field_module[$ftype])) {
153                             $callback = $CFG->display_field_module[$ftype] . "_validate_input_field";
154                             $updatedok = $callback($insertvalue);
155                         }
156                     }
157                 }
158             }
159             $messages[] = __gettext("Profile updated.");
160         } else {
161             
162             $savedata = array();
163             
164             foreach($insertvalues as $insertvalue) {
165                 $savedata['profile:preload'][$insertvalue->name] = $insertvalue->value;
166                 $savedata['profile:preload:access'][$insertvalue->name] = $insertvalue->access;
167             }
168             foreach($requiredmissing as $key=> $missinglabel) {
169                 $message = "";
170                 if ($key > 0) {
171                     $message .= ", ";
172                 }
173                 $message .= $missinglabel;
174             }
175             
176             $messages[] = sprintf(__gettext("You need to fill in the following required fields: %s"),$message);
177             
178             $updatedok = false;
179             $_SESSION['profile:preload'] = $savedata['profile:preload'];
180             $_SESSION['profile:preload:access'] = $savedata['profile:preload:access'];
181         }
182     }
183
184     // Changes saved successfully, update RSS feeds
185     $rssresult = run("weblogs:rss:publish", array(1, false));
186     $rssresult = run("profile:rss:publish", array(1, false));
187
188     $_SESSION['messages'] = $messages;
189     
190     // redirect("{$CFG->wwwroot}{$profile_name}", get_string("changessaved"));
191     if ($updatedok) {
192         redirect("{$CFG->wwwroot}{$profile_name}/profile/", "");
193     } else {
194         redirect("{$CFG->wwwroot}profile/edit.php?profile_id=".$page_owner, "");
195     }
196 }
197
198 ?>
Note: See TracBrowser for help on using the browser.