示例#1
0
 public static function post()
 {
     $candidateId = self::pullPostInteger('candidateId');
     $candidate = self::build($candidateId, new Resource());
     $type = self::pullPostString('type');
     if ($type == 'ticket') {
         $candidate->setTicketId((int) self::pullPostInteger('ticketId'));
     } elseif ($type === 'multiple') {
         $candidate->setMultipleId((int) self::pullPostInteger('multipleId'));
     } else {
         throw new \Exception('Unknown candidate type');
     }
     $ticket_id = $candidate->getTicketId();
     $multiple_id = $candidate->getMultipleId();
     if ((int) $ticket_id === 0 && (int) $multiple_id === 0) {
         throw new \Exception('Missing candidate foreign key');
     }
     /**
      * If this is a new candidate make sure the election
      * is not ongoing or the person doesn't have rights.
      */
     if (!$candidateId) {
         if ($ticket_id) {
             $election_id = Ticket::getElectionId($ticket_id);
         } else {
             $election_id = Multiple::getElectionId($multiple_id);
         }
         if (!Election::allowChange($election_id)) {
             throw new \Exception('Cannot create new candidate in active election');
         }
     }
     $candidate->setFirstName(ucfirst(self::pullPostString('firstName')));
     $candidate->setLastName(ucfirst(self::pullPostString('lastName')));
     $candidate->setTitle(self::pullPostString('title'));
     if (!empty($_FILES)) {
         $picture = self::savePicture($_FILES[0], $candidate);
         if ($candidate->getId()) {
             self::deletePicture($candidate);
         }
         $candidate->setPicture($picture);
     }
     self::saveResource($candidate);
     return true;
 }
示例#2
0
 public function post(\Request $request)
 {
     if (!$request->isVar('command')) {
         throw new \Exception('Unknown Ticket command');
     }
     $command = $request->getVar('command');
     switch ($command) {
         case 'save':
             Factory::post();
             break;
         case 'delete':
             Factory::delete(Factory::pullPostInteger('ticketId'));
             break;
         default:
             throw new \Exception('Unknown Ticket command');
     }
     $view = new \View\JsonView(array('success' => true));
     $response = new \Response($view);
     return $response;
 }
示例#3
0
 /**
  * Returns a list of single ballots with included tickets.
  * 
  * Tickets ARE REQUIRED. A ballot without tickets will be unset.
  * 
  * @param type $electionId
  * @param type $addCandidates
  * @return array
  */
 public static function getListWithTickets($electionId, $addCandidates = true, $randomize = ELECTION_RANDOMIZE_TICKETS)
 {
     $singleList = self::getList($electionId);
     if (empty($singleList)) {
         return array();
     }
     foreach ($singleList as $key => &$value) {
         $tickets = null;
         if ($addCandidates) {
             $tickets = Ticket::getListWithCandidates($value['id'], $randomize);
         } else {
             $tickets = Ticket::getList($value['id'], $randomize);
         }
         if (empty($tickets)) {
             unset($singleList[$key]);
         } else {
             $value['tickets'] = $tickets;
         }
     }
     return $singleList;
 }