+ return unserialize($data);
+ }
+
+ // }}}
+}
+
+/** Virtual class for profile related validation.
+ */
+abstract class ProfileValidate extends Validate
+{
+ // {{{ properties
+
+ public $profile;
+ public $profileOwner;
+ public $userIsProfileOwner;
+
+ // }}}
+ // {{{ 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();
+ if (!is_null($this->profileOwner) && $this->profileOwner->id() == $this->user->id()) {
+ $this->userIsProfileOwner = true;
+ } else {
+ $this->userIsProfileOwner = false;
+ }
+ }
+
+ // }}}
+ // {{{ 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);