+ /** Overload PlUser::promo(): there no promo defined for a user in the current
+ * schema. The promo is a field from the profile.
+ */
+ public function promo()
+ {
+ if (!$this->hasProfile()) {
+ return '';
+ }
+ return $this->profile()->promo();
+ }
+
+ public function firstName()
+ {
+ if (!$this->hasProfile()) {
+ return $this->displayName();
+ }
+ return $this->profile()->firstName();
+ }
+
+ public function lastName()
+ {
+ if (!$this->hasProfile()) {
+ return '';
+ }
+ return $this->profile()->lastName();
+ }
+
+ /** Return the main profile attached with this account if any.
+ */
+ public function profile()
+ {
+ if (!$this->_profile_fetched) {
+ $this->_profile_fetched = true;
+ $this->_profile = Profile::get($this);
+ }
+ return $this->_profile;
+ }
+
+ /** Return true if the user has an associated profile.
+ */
+ public function hasProfile()
+ {
+ return !is_null($this->profile());
+ }
+
+ /** Check if the user can edit to given profile.
+ */
+ public function canEdit(Profile $profile)
+ {
+ // XXX: Check permissions (e.g. secretary permission)
+ // and flags from the profile
+ return XDB::fetchOneCell('SELECT pid
+ FROM account_profiles
+ WHERE uid = {?} AND pid = {?}',
+ $this->id(), $profile->id());
+ }
+
+ /** Get the email alias of the user.
+ */
+ public function emailAlias()
+ {
+ global $globals;
+ $data = $this->emailAliases($globals->mail->alias_dom);
+ if (count($data) > 0) {
+ return array_pop($data);
+ }
+ return null;
+ }
+
+ /** Get all the aliases the user belongs to.
+ */
+ public function emailAliases($domain = null, $type = 'user', $sub_state = false)
+ {
+ $join = XDB::format('(vr.redirect = {?} OR vr.redirect = {?}) ',
+ $this->forlifeEmail(), $this->m4xForlifeEmail());
+ $where = '';
+ if (!is_null($domain)) {
+ $where = XDB::format('WHERE v.alias LIKE CONCAT("%@", {?})', $domain);
+ }
+ if (!is_null($type)) {
+ if (empty($where)) {
+ $where = XDB::format('WHERE v.type = {?}', $type);
+ } else {
+ $where .= XDB::format(' AND v.type = {?}', $type);
+ }
+ }
+ if ($sub_state) {
+ return XDB::fetchAllAssoc('alias', 'SELECT v.alias, vr.redirect IS NOT NULL AS sub
+ FROM virtual AS v
+ LEFT JOIN virtual_redirect AS vr ON (v.vid = vr.vid AND ' . $join . ')
+ ' . $where);
+ } else {
+ return XDB::fetchColumn('SELECT v.alias
+ FROM virtual AS v
+ INNER JOIN virtual_redirect AS vr ON (v.vid = vr.vid AND ' . $join . ')
+ ' . $where);
+ }
+ }
+
+ /** Get the alternative forlife email
+ * TODO: remove this uber-ugly hack. The issue is that you need to remove
+ * all @m4x.org addresses in virtual_redirect first.
+ * XXX: This is juste to make code more readable, to be remove as soon as possible
+ */
+ public function m4xForlifeEmail()
+ {
+ global $globals;
+ trigger_error('USING M4X FORLIFE', E_USER_NOTICE);
+ return $this->login() . '@' . $globals->mail->domain2;
+ }
+
+
+ /** Get marketing informations
+ */
+ private function fetchMarketingData()
+ {
+ if (isset($this->last_known_email)) {
+ return;
+ }
+ $infos = XDB::fetchOneAssoc('SELECT IF (MAX(m.last) > p.relance, MAX(m.last), p.relance) AS last_relance,
+ p.email AS last_known_email
+ FROM register_pending AS p
+ LEFT JOIN register_marketing AS m ON (p.uid = m.uid)
+ WHERE p.uid = {?}
+ GROUP BY p.uid', $this->id());
+ if (!$infos) {
+ $infos = array('last_relance' => null, 'last_known_email' => null);
+ }
+ $this->fillFromArray($infos);
+ }
+
+ public function lastMarketingRelance()
+ {
+ $this->fetchMarketingData();
+ return $this->last_relance;
+ }
+
+ public function lastKnownEmail()
+ {
+ $this->fetchMarketingData();
+ return $this->last_known_email;
+ }
+
+
+ /** Get watch informations
+ */
+ private function fetchWatchData()
+ {
+ if (isset($this->watch_actions)) {
+ return;
+ }
+ $watch = XDB::fetchOneAssoc('SELECT flags AS watch_flags, actions AS watch_actions,
+ UNIX_TIMESTAMP(last) AS watch_last
+ FROM watch
+ WHERE uid = {?}', $this->id());
+ $watch['watch_flags'] = new PlFlagSet($watch['watch_flags']);
+ $watch['watch_actions'] = new PlFlagSet($watch['watch_actions']);
+ $watch['watch_promos'] = XDB::fetchColumn('SELECT promo
+ FROM watch_promo
+ WHERE uid = {?}', $this->id());
+ $watch['watch_users'] = XDB::fetchColumn('SELECT ni_id
+ FROM watch_nonins
+ WHERE uid = {?}', $this->id());
+ $this->fillFromArray($watch);
+ }
+
+ public function watchType($type)