+ return unserialize($data);
+ }
+
+ // }}}
+
+ /** Return an iterator over the validation concerning the given type
+ * and the given user.
+ *
+ * @param type The type of the validations to fetch, null mean "any type"
+ * @param applyTo A User or a Profile object the validation applies to.
+ */
+ public static function iterate($type = null, $applyTo = null)
+ {
+ function toValidation($elt)
+ {
+ list($result, $stamp) = $elt;
+ $result = Validate::unserialize($result);
+ $result->stamp = $stamp;
+ return $result;
+ }
+
+ $where = array();
+ if ($type) {
+ $where[] = XDB::format('type = {?}', $type);
+ }
+ if ($applyTo) {
+ if ($applyTo instanceof User) {
+ $where[] = XDB::format('uid = {?}', $applyTo->id());
+ } else if ($applyTo instanceof Profile) {
+ $where[] = XDB::format('pid = {?}', $applyTo->id());
+ }
+ }
+ if (!empty($where)) {
+ $where = 'WHERE ' . implode('AND', $where);
+ }
+ $it = XDB::iterRow('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
+ FROM requests
+ ' . $where . '
+ ORDER BY stamp');
+ return PlIteratorUtils::map($it, 'toValidation');
+ }
+}
+
+/** Virtual class for profile related validation.
+ */
+abstract class ProfileValidate extends Validate
+{
+ // {{{ properties
+
+ public $profile;
+ public $profileOwner;
+ public $userIsProfileOwner;
+ public $ownerIsRegistered;
+
+ // }}}
+ // {{{ constructor
+
+ /** Constructor
+ * @param $_user: user object that required the validation.
+ * @param $_profile: profile object that is to be modified,
+ * its owner (if exists) can differ from $_user.
+ * @param $_unique: set to false if a profile can have multiple requests of this type.
+ * @param $_type: request's type.
+ */
+ public function __construct(User $_user, Profile $_profile, $_unique, $_type)
+ {
+ parent::__construct($_user, $_unique, $_type);
+ $this->profile = &$_profile;
+ $this->profileOwner = $this->profile->owner();
+ $this->userIsProfileOwner = (!is_null($this->profileOwner)
+ && $this->profileOwner->id() == $this->user->id());
+ $this->ownerIsRegistered = $this->profile->isActive();
+ }
+
+ // }}}
+ // {{{ function submit()
+
+ /** Sends data to validation.
+ * It also deletes multiple requests for a couple (profile, type)
+ * when $this->unique is set to true.
+ */
+ public function submit()
+ {
+ if ($this->unique) {
+ XDB::execute('DELETE FROM requests
+ WHERE pid = {?} AND type = {?}',
+ $this->profile->id(), $this->type);
+ }
+
+ $this->stamp = date('YmdHis');
+ XDB::execute('INSERT INTO requests (uid, pid, type, data, stamp)
+ VALUES ({?}, {?}, {?}, {?}, {?})',
+ $this->user->id(), $this->profile->id(), $this->type, $this, $this->stamp);
+
+ global $globals;
+ $globals->updateNbValid();
+ return true;
+ }
+
+ // }}}
+ // {{{ function update()
+
+ protected function update()
+ {
+ XDB::execute('UPDATE requests
+ SET data = {?}, stamp = stamp
+ WHERE pid = {?} AND type = {?} AND stamp = {?}',
+ $this, $this->profile->id(), $this->type, $this->stamp);
+ return true;
+ }
+
+ // }}}
+ // {{{ function clean()
+
+ /** Deletes request from 'requests' table.
+ * If $this->unique is set, it deletes every requests of this type.
+ */
+ public function clean()
+ {
+ global $globals;
+
+ if ($this->unique) {
+ $success = XDB::execute('DELETE FROM requests
+ WHERE pid = {?} AND type = {?}',
+ $this->profile->id(), $this->type);
+ } else {
+ $success = XDB::execute('DELETE FROM requests
+ WHERE pid = {?} AND type = {?} AND stamp = {?}',
+ $this->profile->id(), $this->type, $this->stamp);
+ }
+ $globals->updateNbValid();
+ return $success;
+ }
+
+ // }}}
+ // {{{ function sendmail
+
+ protected function sendmail($isok)
+ {
+ // Only sends email if the profile's owner exists and is registered.
+ if ($this->ownerIsRegistered) {
+ global $globals;
+
+ $mailer = new PlMailer();
+ $mailer->setSubject($this->_mail_subj());
+ $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
+ $mailer->addTo("\"{$this->profile->fullName()}\" <{$this->profileOwner->bestEmail()}>");
+ $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
+ $body = ($this->profile->isFemale() ? "Chère camarade,\n\n" : "Cher camarade,\n\n")
+ . $this->_mail_body($isok)
+ . (Env::has('comm') ? "\n\n" . Env::v('comm') : '')
+ . "\n\nCordialement,\n-- \nL'équipe de Polytechnique.org\n"
+ . $this->_mail_ps($isok);
+ $mailer->setTxtBody(wordwrap($body));
+ $mailer->send();