示例#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;
 }