source: trunk/jpdd/class.jpdd.person.php @ 1366

Last change on this file since 1366 was 1366, checked in by Daniel Kahn Gillmor, 7 years ago

adding explicit constructors

File size: 14.3 KB
Line 
1<?php  /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 2; -*-
2
3       */
4
5require_once('class.dkg.person.php');
6
7if (!class_exists('JPDD_Person')) {
8
9  class JPDD_Person extends DKG_Person {
10    class JPDD_Person($args = array()) {
11      $this->DKG_Person($args);
12    }
13   
14    function getCreatePrivilege() {
15          return array('Edit People');
16        }
17       
18        function getEditPrivilege() {
19          return array('Edit People');
20        }
21
22        function getListPrivilege() {
23          return array('List People');
24        }
25
26        function getTitle() {
27          $title = join(' ', array_filter(array($this->_first_name, $this->_middle_name, $this->_last_name), create_function('$x', 'global $jpdd; return !$jpdd->isEmpty($x);')));
28      return ('' == $title ? '*no name*' : $title) ;
29        }
30
31    function getSalutation() {
32      global $jpdd;
33
34      // if no workshops are set up at all, people should be able to
35      // apply to the workshop by indicating a preferred category:
36      $wc = $jpdd->getValueFromSQL('SELECT COUNT(*) AS foo FROM workshop WHERE event_id = '.$jpdd->getActiveEventID(), 'foo');
37      if ($wc == 0) {
38        // FIXME: is this person already applied?  if not, should they
39        // be able to make an application on their own?  should we
40        // report the category their application is under?
41
42        // this person a liaison for this event?  if so, direct them to their
43        $lc = $jpdd->getValueFromSQL('SELECT COUNT(*) AS foo FROM person_role JOIN role ON (role.id = role_id) WHERE person_id = '.
44                                     $this->getID().' AND event_id = '.$jpdd->getActiveEventID().' AND role.title = '.$jpdd->escStr('liaison'),
45                                     'foo');
46        if ($lc > 0) {
47          // FIXME: present some sort of simplified reporting here?
48          // (like "all done")?  provide direct links to liaison
49          // pages?
50          return 'Please check in on your organization'.($lc > 1 ? 's':'').' through the links below';
51        } else {
52          return 'No workshops have been created yet.  You will be notified when workshops have been created.';
53        }
54      } else {
55        $wt = $this->getWorkshopText();
56        if ('' == $wt) {
57          return 'You are not signed up for any workshop.'.($jpdd->isSignupClosed() ? '' : ' Please <a href="'.$jpdd->Path('category').'">choose one</a>.');
58        } else {
59          return 'You will be '.$wt;
60        }
61      }
62    }
63
64    function getAttendanceCount($eventid = NULL) {
65      global $jpdd;
66      if (is_null($eventid))
67        $eventid = $jpdd->getActiveEventID();
68      return $jpdd->getValueFromSQL('SELECT COUNT(*) AS foo FROM attendance JOIN workshop ON (workshop_id = workshop.id) WHERE event_id = '.(int)$eventid.' AND person_id = '.(int)$this->getID(), 'foo');
69    }
70   
71    function isAttendingWorkshop($wid, $flavor) {
72      global $jpdd;
73      return 0 < (int)($jpdd->getValueFromSQL('SELECT COUNT(*) AS foo FROM attendance WHERE person_id = '.(int)$this->getID().' AND workshop_id = '.(int)$wid.' AND flavor = '.$jpdd->escStr($flavor), 'foo'));
74    }
75
76        function getAttendingWorkshops($flavor) {
77          global $jpdd;
78          $jpdd->prepClass('workshop');
79      $sql = 'SELECT workshop.* FROM workshop JOIN attendance ON (workshop.id = attendance.workshop_id) WHERE person_id = '.$this->getID().' AND flavor = '.$jpdd->escStr($flavor).' AND event_id = '.$jpdd->getActiveEventID();
80          return $jpdd->getSeriesFromSQL($sql, 'JPDD_Workshop');
81        }
82
83    function getEmail() {
84      global $jpdd;
85      // FIXME: why is this blank?
86    }
87
88    function canBeEditedBy($person) {
89      return ($person->getID() == $this->getID()) ||
90        (($person->getID() == $this->_created_by) &&
91         (is_null($this->_pass)));
92    }
93   
94   
95    // at the moment, this is only used in the lists generated in the
96    // liaison view (hence the deaffiliate/delete link).
97    function getListItem($class, $val, $extra='') {
98      global $jpdd; 
99
100      return '<li class="'.$class.'" person_id="'.$this->getID().'"><span class="identifier">'.
101        $this->getTitle().' '.$this->getPrintableEmail().'</span><input type="hidden" name="person_'.$this->getID().'" value="'.$val.'" /> '.
102        ($jpdd->_authenticated_user->canEdit($this) ?
103         '<a class="edit" title="edit '.$this->getTitle().'" target="_blank" href="'.$jpdd->Path('edit/person/'.$this->getID()).'">'.
104         '[edit]</a> ' : '').
105        $extra.
106        '</li>';
107    }
108
109    function getRoleOrgs($roleid) {
110      global $jpdd;
111      $jpdd->prepClass('organization');
112      $sql = 'SELECT organization.* FROM organization JOIN person_role ON (organization.id = person_role.organization_id) WHERE person_id = '.$this->getID().
113        ' AND role_id = '.$roleid.' AND event_id = '.$jpdd->getActiveEventID();
114      return $jpdd->getSeriesFromSQL($sql, 'JPDD_Organization');
115    }
116
117    function getFormInnards() {
118      global $jpdd;
119      $emailerr = is_null($this->_email) ? NULL : $jpdd->isValid('e-mail',$this->_email);
120      $editaffiliations = '';
121      $editaudiences = '';
122      // we'll use 'Edit Organizations' as a decent proxy for being able to massage affiliations directly.
123      if ($jpdd->_authenticated_user->HasAnyOfThesePrivileges('Edit Organizations')) {
124        // this is the traditional way to show the affiliations:
125        $editaffiliations = $this->getM2MEditView('organization', 'affiliation', 'Affiliated With', 'Add Affiliation');
126      }
127      // but we also want to include every role with each affiliation, if possible.
128      // you must be able to manage signups to add people here:
129      if ($jpdd->_authenticated_user->HasAnyOfThesePrivileges('Manage Signups')) {
130        $editaudiences = $this->getM2MEditView('workshop', 'audience', 'Attending', 'Sign up for', NULL, create_function('$x', 'global $jpdd; $full = false; '.($jpdd->_authenticated_user->HasAnyOfThesePrivileges('Manage Signups') ? ' if ($x->isFull()) $x->_title .= " (FULL)"; ' : '$full = $x->isFull(); ').' return $x->_event_id == $jpdd->getActiveEventID() && !$full;'));
131      }
132      return '<div>
133'.$this->getTitle().' '.$this->getLinkedEmail().'<hr/>
134<label>First name:<br/><input type="text" name="first_name" value="'.htmlentities($this->_first_name).'"/></label><br/>
135<label>Middle name:<br/><input type="text" name="middle_name" value="'.htmlentities($this->_middle_name).'"/></label><br/>
136<label>Last name:<br/><input type="text" name="last_name" value="'.htmlentities($this->_last_name).'"/></label><br/>
137<label>E-mail Address:<br/>'.(is_null($emailerr)?'':'<span class="error">'.$emailerr.'</span><br/>').'<input type="text" name="email" '.(is_null($emailerr)?'':'class="error" ').'value="'.htmlentities($this->_email).'"/></label><br/>
138'.$editaffiliations.'
139'.$editaudiences.'
140</div>';
141    }
142
143    function displayM2MEditPeerDetails($peer, $jointable) {
144      global $jpdd; 
145      // if we're looking at affiliated organizations:
146      if (($jointable == 'affiliation') &&
147          (get_class($peer) == 'JPDD_Organization')) {
148        $roles = $jpdd->getAll("role"); 
149        return join("\n", array_map(create_function('$r', "return '<label>[<input type=\"checkbox\" name=\"role_'.\$r->getID().'[]\" value=\"".$peer->getID()."\" '.(\$r->isPersonOrgActive(".$this->getID().','.$peer->getID().") ? 'checked' : '' ).' />'.\$r->getTitle().']</label>';"), $roles));
150      } else {
151        return parent::displayM2MEditPeerDetails($peer, $jointable);
152      }
153    }
154
155    function applyPostForm() {
156      global $jpdd;
157      $this->_first_name = $jpdd->nonEmptyString($_POST['first_name']);
158      $this->_middle_name = $jpdd->nonEmptyString($_POST['middle_name']);
159      $this->_last_name = $jpdd->nonEmptyString($_POST['last_name']);
160      $this->_email = $jpdd->nonEmptyString($_POST['email']);
161    }
162
163    // returns NULL if everything looks OK.
164    function getInvalidError() {
165      global $jpdd;
166      if ($jpdd->isEmpty($this->_last_name)) {
167        return 'Last Name is empty.';
168      }
169      return NULL;
170    }
171
172    function storeNewInDatabase() {
173      global $jpdd;
174      $res = $jpdd->executeSQL('INSERT INTO '.$this->getSingletonTable().' (first_name, middle_name, last_name, email, created_by) VALUES ('.
175                               $jpdd->stringOrDefault($this->_first_name).', '.
176                               $jpdd->stringOrDefault($this->_middle_name).', '.
177                               $jpdd->stringOrDefault($this->_last_name).', '.
178                               $jpdd->stringOrDefault($this->_email).', '.
179                               $jpdd->intOrDefault($jpdd->_authenticated_user->getID()).
180                               ')', false);
181      if (false !== $res) {
182                $this->JPDD_Person(array('data' => $jpdd->getSingletonFromSQL('SELECT * from '.$this->getSingletonTable().' WHERE id = currval(\''.$this->getSingletonTable().'_id_seq\')')));
183      }
184      return $res;
185    }
186
187    function handleCreation() {
188      global $jpdd;
189      $this->applyPostForm();
190      $errors = $this->getInvalidError();
191      if ($errors)
192        return $errors;
193      $res = $this->storeNewInDatabase();
194          if (false === $res) {
195        $logid = $jpdd->log('create person failed with pg error: '.pg_last_error($jpdd->_db));
196        // check if the e-mail address is already listed:
197        $other = $jpdd->getSeriesFromSQL('SELECT * from person WHERE email = '.$jpdd->escStr($this->_email), 'JPDD_Person');
198        if (count($other) > 0)
199          return 'Maybe you meant this person: '.array_shift($other)->getLinkedTitle();
200                return 'database insertion failed for some reason (error '.(int)$logid.')';
201          } else {
202        $this->addM2MPeers('organization', 'affiliation', $jpdd->getPostedIDs('add_affiliation'));
203        $this->addM2MPeers('workshop', 'audience', $jpdd->getPostedIDs('add_audience'));
204                return NULL;
205          }
206    }
207
208    function handleEdit() {
209      global $jpdd;
210      $this->applyPostForm();
211      $errors = $this->getInvalidError();
212      if (!is_null($errors))
213        return $errors;
214      $res = $jpdd->executeSQL('UPDATE '.$this->getSingletonTable().' SET '.
215                               'first_name = '.$jpdd->stringOrDefault($this->_first_name).', '.
216                               'middle_name = '.$jpdd->stringOrDefault($this->_middle_name).', '.
217                               'last_name = '.$jpdd->stringOrDefault($this->_last_name).', '.
218                               'email = '.$jpdd->stringOrDefault($this->_email).' '.
219                               'WHERE id = '.$this->getID());
220          if (false === $res) {
221        $logid = $jpdd->log('edit person '.(int)$this->_id.' db update failed with pg error: '.pg_last_error($jpdd->_db));
222                return 'editing this person failed during insertion into the database for some reason (error: '.(int)$logid.')';
223          } else {
224                $this->JPDD_Person(array('id' => $this->getID()));
225        if ($jpdd->_authenticated_user->HasAnyOfThesePrivileges('Assign Roles')) {
226          $removeaff = $jpdd->getPostedIDs('remove_affiliation');
227          $this->addM2MPeers('organization', 'affiliation', $jpdd->getPostedIDs('add_affiliation'));
228          $this->dropM2MPeers('organization', 'affiliation', $removeaff);
229          $this->addM2MPeers('workshop', 'audience', $jpdd->getPostedIDs('add_audience'));
230          $this->dropM2MPeers('workshop', 'audience', $jpdd->getPostedIDs('remove_audience'));
231         
232          // and for the roles?
233          // first, drop all roles for this user for this event:
234          $jpdd->executeSQL('DELETE FROM person_role WHERE person_id = '.$this->getID().' AND event_id = '.$jpdd->getActiveEventID());
235          // then, add in the proper roles for this user based on the input:
236          $newroles = $jpdd->getPostedIDMap('role');
237          reset($newroles);
238          while(list($rid,$orgids) = each($newroles)) {
239            reset($orgids);
240            while(list(,$oid) = each($orgids)) {
241              if (is_numeric($oid) && !in_array($oid, $removeaff)) {
242                $jpdd->executeSQL('INSERT INTO person_role (person_id,event_id,organization_id,role_id) VALUES ('.
243                                  $this->getID().', '.$jpdd->getActiveEventID().', '.(int)$oid.', '.(int)$rid.')');
244              }
245            }
246          }
247        }
248                return NULL;
249          }
250    }
251
252
253    function getNameWithOrgs() {
254      $orgs = $this->getM2MPeers('organization', 'affiliation');
255
256      return $this->getLinkedTitle().(count($orgs) ? ' - '.join('/', array_map(create_function('$x', 'return $x->getLinkedTitle();'), $orgs)):'');
257    }
258
259    function getAffiliationString() {
260      $orgs = $this->getM2MPeers('organization', 'affiliation');
261      return join('/', array_map(create_function('$x', 'return $x->getTitle();'), $orgs));
262    }
263
264    function getReversedName() {
265      global $jpdd;
266      $starter = join(' ', array_filter(array($this->_first_name, $this->_middle_name), create_function('$x', 'global $jpdd; return !$jpdd->isEmpty($x);')));
267      if (!$jpdd->isEmpty($starter)) 
268        return $this->_last_name.', '.$starter;
269      else
270        return $this->_last_name;
271    }
272
273    function getWorkshopText($usedivs = true) {
274      $ret = '';
275          $presenting = $this->getAttendingWorkshops('presenter');
276          // show workshops presenting
277          if (count($presenting)) {
278                $ret .= ($usedivs ? '<div>' : '').'Presenting: '.join(', ', array_map(create_function('$xx', 'return $xx->getLinkedTitle();'), $presenting)).($usedivs ? '</div>' : '');
279          }
280         
281          $attending = $this->getAttendingWorkshops('audience');
282          // show workshops being attended
283          if (count($attending)) {
284                $ret .= ($usedivs ? '<div>' : '').'Attending: '.join(', ', array_map(create_function('$xx', 'return $xx->getLinkedTitle();'), $attending)).($usedivs ? '</div>' : '');
285          }
286          return $ret;
287    }
288
289        function getDetailInnards() {
290      global $jpdd;
291
292      $ret = $this->getWorkshopText();
293
294
295      // only show privileges if the visiting user is an authenticated user anyway:
296      if ($jpdd->isAuthenticated()) {
297        if ($jpdd->_authenticated_user->HasAnyOfThesePrivileges('Edit People')) {
298          // show privileges and email:
299          $ret .= 'e-mail: '.$this->getLinkedEmail().$this->getM2MPeerList('privilege', 'person_privilege', 'Privileges');
300          $ret .= $this->getM2MPeerList('role', 'person_role', 'Roles');
301        }
302      }
303
304          $ret .= $this->getM2MPeerList('organization', 'affiliation', 'Affiliated With');
305
306          return $ret;
307        }
308
309    function getPDF($pdfname) {
310      require_once('class.jpdd.pdf.php');
311      $ret = new JPDD_PDF();
312      if ($pdfname == 'workshop.pdf') {
313        $ret->addParticipantPage($this);
314      } else {
315        return NULL;
316      }
317      return $ret;
318    }
319
320  }
321
322}
Note: See TracBrowser for help on using the repository browser.