<?php  /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 2; -*-

       */

require_once('class.dkg.broadcast.php');

if (!class_exists('JPDD_Broadcast')) {
  class JPDD_Broadcast extends DKG_Broadcast {

	// add to the list of broadcast selectors available:
    static function getSelectors() {
	  global $jpdd;
      // override to return an array that maps internal keys to
      // visible labels and SQL statements.  For example:
      $roles = $jpdd->getAll('role');
      $rsel = array(); // the per-role selectors
      reset($roles);
      while(list(,$role) = each($roles)) {
        $rsel['role:'.$role->getID()] = array('description' => 'All people acting as '.$role->getTitle(),
                                              'sql' => 'SELECT person.* FROM person JOIN person_role ON (person_id = person.id) WHERE role_id = '.$role->getID().
                                              ' AND event_id = '.$jpdd->getActiveEventID());
      }
      // get a list of all categories which have applications in the current event:
      $csel = array();
      $cats = $jpdd->getActiveApplicationCategories($jpdd->getActiveEventID());
      reset($cats);
      while (list($id,$cat) = each($cats)) {
        $id = (int)$id;
        $csel['category:'.$id] = array('description' => 'All people signed up under '.$cat,
                                       'sql' => 'SELECT person.* FROM person JOIN application ON (person_id = person.id) WHERE category_id = '.$id.
                                       ' AND event_id = '.$jpdd->getActiveEventID());
      }
      $special = array('presenters' => array('description' => 'Workshop '.ucfirst($jpdd->getPresenterNamePlural()),
                                             'sql' => 'SELECT person.* FROM person JOIN presenter ON (person.id = presenter.person_id) JOIN workshop ON (workshop_id = workshop.id) WHERE event_id = '.$jpdd->getActiveEventID()),
                       'attendees' => array('description' => 'Workshop Attendees',
                                            'sql' => 'SELECT person.* FROM person JOIN audience ON (person.id = audience.person_id) JOIN workshop ON (workshop_id = workshop.id) WHERE event_id = '.$jpdd->getActiveEventID()),
                       'newteachers' => array('description' => 'People signed up since planning began',
                                              'sql' => 'SELECT person.* FROM person WHERE created_on >= (SELECT start_planning FROM event WHERE id = '.$jpdd->getActiveEventID().')'),
                       'allaffiliated' => array('description' => 'Everyone associated with this event',
                                                'sql' => 'SELECT person.* FROM person JOIN ('.
                                                'SELECT person_id FROM attendance JOIN workshop ON (workshop_id = workshop.id) WHERE event_id = '.$jpdd->getActiveEventID().
                                                ' UNION SELECT person_id FROM person_role WHERE event_id = '.$jpdd->getActiveEventID().
                                                ' UNION SELECT person_id FROM application WHERE event_id = '.$jpdd->getActiveEventID().
                                                ') AS selector ON (person_id = person.id)'));
                       
      if (count($csel) > 1) 
        $special['applicants'] = array('description' => 'All people signed up under any subject',
                                       'sql' => 'SELECT person.* FROM person JOIN application ON (person.id = person_id) WHERE event_id = '.$jpdd->getActiveEventID());
      if (count($rsel) > 1) 
        $special['allroles'] = array('description' => 'People in any role ('.join(', ', array_map(create_function('$r', 'return $r->getTitle();'), $roles)).')',
                                     'sql' => 'SELECT person.* FROM person JOIN person_role ON (person.id = person_id) WHERE event_id = '.$jpdd->getActiveEventID());
      
        
      

      return array_merge(array(// a goofy test here:
                               'jpdd_admins' => array('description' => 'People named Gillmor or Tashlik',
                                                      'sql' => 'SELECT * FROM person WHERE last_name IN (\'Gillmor\', \'Tashlik\')')),
                         $special,
                         $rsel,
                         $csel,
                         parent::getSelectors()
                         );
    }

    static function getAllowedTemplates() {
      // override to return an array that maps selectors to
      // array('description' = "human readable", 'function' => actual
      // php function)
      return array_merge(parent::getAllowedTemplates(),
                         // FIXME: these are pretty broken, actually, particularly if there is more than one workshop being attended.
                         array('[[WORKSHOP]]' => array('description' => 'The workshop the person is attending (or "no workshop")',
                                                       'function' => create_function('$x', 'global $jpdd; $ws = $x->getM2MPeers("workshop", "attendance", "AND event_id = ".$jpdd->getActiveEventID()); if (count($ws) == 0) return "no workshop"; return join(", ", array_map(create_function(\'$x\', \'return html_entity_decode($x->getTitle());\'), $ws));')),
                               '[[ROOM]]' => array('description' => 'The room number of the attended workshop (e.g. "Room 143" or "no room")',
                                                   'function' => create_function('$x', 'global $jpdd; $ws = $x->getM2MPeers("workshop", "attendance", "AND event_id = ".$jpdd->getActiveEventID()); if (count($ws) == 0) return "no room"; return join(", ", array_map(create_function(\'$x\', \'$rr = $x->getAssignedRoom(); return (is_null($rr) ? "no room" : "Room ".$rr->getTitle());\'), $ws));')))
                         );
    }



  }
 }
?>
