+// }}}
+
+// {{{ class UFC_Address
+abstract class UFC_Address implements UserFilterCondition
+{
+ /** Valid address type ('hq' is reserved for company addresses)
+ */
+ const TYPE_HOME = 1;
+ const TYPE_PRO = 2;
+ const TYPE_ANY = 3;
+
+ /** Text for these types
+ */
+ protected static $typetexts = array(
+ self::TYPE_HOME => 'home',
+ self::TYPE_PRO => 'pro',
+ );
+
+ protected $type;
+
+ /** Flags for addresses
+ */
+ const FLAG_CURRENT = 0x0001;
+ const FLAG_TEMP = 0x0002;
+ const FLAG_SECOND = 0x0004;
+ const FLAG_MAIL = 0x0008;
+ const FLAG_CEDEX = 0x0010;
+
+ // Binary OR of those flags
+ const FLAG_ANY = 0x001F;
+
+ /** Text of these flags
+ */
+ protected static $flagtexts = array(
+ self::FLAG_CURRENT => 'current',
+ self::FLAG_TEMP => 'temporary',
+ self::FLAG_SECOND => 'secondary',
+ self::FLAG_MAIL => 'mail',
+ self::FLAG_CEDEX => 'cedex',
+ );
+
+ protected $flags;
+
+ public function __construct($type = null, $flags = null)
+ {
+ $this->type = $type;
+ $this->flags = $flags;
+ }
+
+ protected function initConds($sub)
+ {
+ $conds = array();
+ $types = array();
+ foreach (self::$typetexts as $flag => $type) {
+ if ($flag & $this->type) {
+ $types[] = $type;
+ }
+ }
+ if (count($types)) {
+ $conds[] = $sub . '.type IN ' . XDB::formatArray($types);
+ }
+
+ if ($this->flags != self::FLAG_ANY) {
+ foreach(self::$flagtexts as $flag => $text) {
+ if ($flag & $this->flags) {
+ $conds[] = 'FIND_IN_SET(' . XDB::format('{?}', $text) . ', ' . $sub . '.flags)';
+ }
+ }
+ }
+ return $conds;
+ }
+
+}
+// }}}
+
+// {{{ class UFC_AddressText
+/** Select users based on their address, using full text search
+ * @param $text Text for filter in fulltext search
+ * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
+ * @param $type Filter on address type
+ * @param $flags Filter on address flags
+ * @param $country Filter on address country
+ * @param $locality Filter on address locality
+ */
+class UFC_AddressText extends UFC_Address
+{
+
+ private $text;
+ private $textSearchMode;
+
+ public function __construct($text = null, $textSearchMode = XDB::WILDCARD_CONTAINS,
+ $type = null, $flags = self::FLAG_ANY, $country = null, $locality = null)
+ {
+ parent::__construct($type, $flags);
+ $this->text = $text;
+ $this->textSearchMode = $textSearchMode;
+ $this->country = $country;
+ $this->locality = $locality;
+ }
+
+ private function mkMatch($txt)
+ {
+ return XDB::formatWildcards($this->textSearchMode, $txt);
+ }
+
+ public function buildCondition(PlFilter &$uf)
+ {
+ $sub = $uf->addAddressFilter();
+ $conds = $this->initConds($sub);
+ if ($this->text != null) {
+ $conds[] = $sub . '.text' . $this->mkMatch($this->text);
+ }
+
+ if ($this->country != null) {
+ $subc = $uf->addAddressCountryFilter();
+ $subconds = array();
+ $subconds[] = $subc . '.country' . $this->mkMatch($this->country);
+ $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country);
+ $conds[] = implode(' OR ', $subconds);
+ }
+
+ if ($this->locality != null) {
+ $subl = $uf->addAddressLocalityFilter();
+ $conds[] = $subl . '.name' . $this->mkMatch($this->locality);
+ }
+
+ return implode(' AND ', $conds);
+ }
+}
+// }}}
+
+// {{{ class UFC_AddressField
+/** Filters users based on their address,
+ * @param $val Either a code for one of the fields, or an array of such codes
+ * @param $fieldtype The type of field to look for
+ * @param $type Filter on address type
+ * @param $flags Filter on address flags
+ */
+class UFC_AddressField extends UFC_Address
+{
+ const FIELD_COUNTRY = 1;
+ const FIELD_ADMAREA = 2;
+ const FIELD_SUBADMAREA = 3;
+ const FIELD_LOCALITY = 4;
+ const FIELD_ZIPCODE = 5;
+
+ /** Data of the filter
+ */
+ private $val;
+ private $fieldtype;
+
+ public function __construct($val, $fieldtype, $type = null, $flags = self::FLAG_ANY)
+ {
+ parent::__construct($type, $flags);
+
+ if (!is_array($val)) {
+ $val = array($val);
+ }
+ $this->val = $val;
+ $this->fieldtype = $fieldtype;
+ }
+
+ public function buildCondition(PlFilter &$uf)
+ {
+ $sub = $uf->addAddressFilter();
+ $conds = $this->initConds($sub);
+
+ switch ($this->fieldtype) {
+ case self::FIELD_COUNTRY:
+ $field = 'countryId';
+ break;
+ case self::FIELD_ADMAREA:
+ $field = 'administrativeAreaId';
+ break;
+ case self::FIELD_SUBADMAREA:
+ $field = 'subAdministrativeAreaId';
+ break;
+ case self::FIELD_LOCALITY:
+ $field = 'localityId';
+ break;
+ case self::FIELD_ZIPCODE:
+ $field = 'postalCode';
+ break;
+ default:
+ Platal::page()->killError('Invalid address field type: ' . $this->fieldtype);
+ }
+ $conds[] = $sub . '.' . $field . ' IN ' . XDB::formatArray($this->val);
+
+ return implode(' AND ', $conds);
+ }
+}
+// }}}
+
+// {{{ class UFC_Corps
+/** Filters users based on the corps they belong to
+ * @param $corps Corps we are looking for (abbreviation)
+ * @param $type Whether we search for original or current corps
+ */
+class UFC_Corps implements UserFilterCondition
+{
+ const CURRENT = 1;
+ const ORIGIN = 2;
+
+ private $corps;
+ private $type;
+
+ public function __construct($corps, $type = self::CURRENT)
+ {
+ $this->corps = $corps;
+ $this->type = $type;
+ }
+
+ public function buildCondition(PlFilter &$uf)
+ {
+ /** Tables shortcuts:
+ * pc for profile_corps,
+ * pceo for profile_corps_enum - orginal
+ * pcec for profile_corps_enum - current
+ */
+ $sub = $uf->addCorpsFilter($this->type);
+ $cond = $sub . '.abbreviation = ' . $corps;
+ return $cond;
+ }
+}
+// }}}
+
+// {{{ class UFC_Corps_Rank
+/** Filters users based on their rank in the corps
+ * @param $rank Rank we are looking for (abbreviation)
+ */
+class UFC_Corps_Rank implements UserFilterCondition
+{
+ private $rank;
+ public function __construct($rank)
+ {
+ $this->rank = $rank;
+ }
+
+ public function buildCondition(PlFilter &$uf)
+ {
+ /** Tables shortcuts:
+ * pcr for profile_corps_rank
+ */
+ $sub = $uf->addCorpsRankFilter();
+ $cond = $sub . '.abbreviation = ' . $rank;
+ return $cond;
+ }
+}
+// }}}
+
+// {{{ class UFC_Job_Company
+/** Filters users based on the company they belong to
+ * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
+ * @param $value The searched value
+ */
+class UFC_Job_Company implements UserFilterCondition
+{
+ const JOBID = 'id';
+ const JOBNAME = 'name';
+ const JOBACRONYM = 'acronym';
+
+ private $type;
+ private $value;
+
+ public function __construct($type, $value)
+ {
+ $this->assertType($type);
+ $this->type = $type;
+ $this->value = $value;
+ }
+
+ private function assertType($type)
+ {
+ if ($type != self::JOBID && $type != self::JOBNAME && $type != self::JOBACRONYM) {
+ Platal::page()->killError("Type de recherche non valide.");
+ }
+ }
+
+ public function buildCondition(PlFilter &$uf)
+ {
+ $sub = $uf->addJobCompanyFilter();
+ $cond = $sub . '.' . $this->type . ' = ' . XDB::format('{?}', $this->value);
+ return $cond;
+ }
+}
+// }}}
+
+// {{{ class UFC_Job_Sectorization
+/** Filters users based on the ((sub)sub)sector they work in
+ * @param $val The ID of the sector, or an array of such IDs
+ * @param $type The kind of search (subsubsector/subsector/sector)
+ */
+class UFC_Job_Sectorization implements UserFilterCondition
+{
+ private $val;
+ private $type;
+
+ public function __construct($val, $type = UserFilter::JOB_SECTOR)
+ {
+ self::assertType($type);
+ if (!is_array($val)) {
+ $val = array($val);
+ }
+ $this->val = $val;
+ $this->type = $type;
+ }
+
+ private static function assertType($type)
+ {
+ if ($type != UserFilter::JOB_SECTOR && $type != UserFilter::JOB_SUBSECTOR && $type != UserFilter::JOB_SUBSUBSECTOR) {
+ Platal::page()->killError("Type de secteur non valide.");
+ }
+ }
+
+ public function buildCondition(PlFilter &$uf)
+ {
+ $sub = $uf->addJobSectorizationFilter($this->type);
+ return $sub . '.id = ' . XDB::format('{?}', $this->val);
+ }
+}
+// }}}
+
+// {{{ class UFC_Job_Description
+/** Filters users based on their job description
+ * @param $description The text being searched for
+ * @param $fields The fields to search for (user-defined, ((sub|)sub|)sector)
+ */
+class UFC_Job_Description implements UserFilterCondition
+{
+
+ private $description;
+ private $fields;
+
+ public function __construct($description, $fields)
+ {
+ $this->fields = $fields;
+ $this->description = $description;
+ }
+
+ public function buildCondition(PlFilter &$uf)
+ {
+ $conds = array();
+ if ($this->fields & UserFilter::JOB_USERDEFINED) {
+ $sub = $uf->addJobFilter();
+ $conds[] = $sub . '.description ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
+ }
+ if ($this->fields & UserFilter::JOB_CV) {
+ $uf->requireProfiles();
+ $conds[] = 'p.cv ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
+ }
+ if ($this->fields & UserFilter::JOB_SECTOR) {
+ $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SECTOR);
+ $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
+ }
+ if ($this->fields & UserFilter::JOB_SUBSECTOR) {
+ $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSECTOR);
+ $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
+ }
+ if ($this->fields & UserFilter::JOB_SUBSUBSECTOR) {
+ $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSUBSECTOR);
+ $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
+ $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_ALTERNATES);
+ $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
+ }
+ return implode(' OR ', $conds);
+ }
+}
+// }}}
+
+// {{{ class UFC_Networking
+/** Filters users based on network identity (IRC, ...)
+ * @param $type Type of network (-1 for any)
+ * @param $value Value to search
+ */
+class UFC_Networking implements UserFilterCondition
+{
+ private $type;
+ private $value;
+
+ public function __construct($type, $value)
+ {
+ $this->type = $type;
+ $this->value = $value;
+ }
+
+ public function buildCondition(PlFilter &$uf)
+ {
+ $sub = $uf->addNetworkingFilter();
+ $conds = array();
+ $conds[] = $sub . '.address ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->value);
+ if ($this->type != -1) {
+ $conds[] = $sub . '.network_type = ' . XDB::format('{?}', $this->type);
+ }
+ return implode(' AND ', $conds);
+ }
+}
+// }}}
+
+// {{{ class UFC_Phone
+/** Filters users based on their phone number
+ * @param $num_type Type of number (pro/user/home)
+ * @param $phone_type Type of phone (fixed/mobile/fax)
+ * @param $number Phone number
+ */
+class UFC_Phone implements UserFilterCondition
+{
+ const NUM_PRO = 'pro';
+ const NUM_USER = 'user';
+ const NUM_HOME = 'address';
+ const NUM_ANY = 'any';
+
+ const PHONE_FIXED = 'fixed';
+ const PHONE_MOBILE = 'mobile';
+ const PHONE_FAX = 'fax';
+ const PHONE_ANY = 'any';
+
+ private $num_type;
+ private $phone_type;
+ private $number;
+
+ public function __construct($number, $num_type = self::NUM_ANY, $phone_type = self::PHONE_ANY)
+ {
+ require_once('profil.func.inc.php');
+ $this->number = $number;
+ $this->num_type = $num_type;
+ $this->phone_type = format_phone_number($phone_type);
+ }
+
+ public function buildCondition(PlFilter &$uf)
+ {
+ $sub = $uf->addPhoneFilter();
+ $conds = array();
+ $conds[] = $sub . '.search_tel = ' . XDB::format('{?}', $this->number);
+ if ($this->num_type != self::NUM_ANY) {
+ $conds[] = $sub . '.link_type = ' . XDB::format('{?}', $this->num_type);
+ }
+ if ($this->phone_type != self::PHONE_ANY) {
+ $conds[] = $sub . '.tel_type = ' . XDB::format('{?}', $this->phone_type);
+ }
+ return implode(' AND ', $conds);
+ }
+}
+// }}}
+
+// {{{ class UFC_Medal
+/** Filters users based on their medals
+ * @param $medal ID of the medal
+ * @param $grade Grade of the medal (null for 'any')
+ */
+class UFC_Medal implements UserFilterCondition
+{
+ private $medal;
+ private $grade;
+
+ public function __construct($medal, $grade = null)
+ {
+ $this->medal = $medal;
+ $this->grade = $grade;
+ }
+
+ public function buildCondition(PlFilter &$uf)
+ {
+ $conds = array();
+ $sub = $uf->addMedalFilter();
+ $conds[] = $sub . '.mid = ' . XDB::format('{?}', $this->medal);
+ if ($this->grade != null) {
+ $conds[] = $sub . '.gid = ' . XDB::format('{?}', $this->grade);
+ }
+ return implode(' AND ', $conds);
+ }
+}
+// }}}
+
+// {{{ class UFC_Mentor_Expertise
+/** Filters users by mentoring expertise
+ * @param $expertise Domain of expertise
+ */
+class UFC_Mentor_Expertise implements UserFilterCondition
+{
+ private $expertise;
+
+ public function __construct($expertise)
+ {
+ $this->expertise = $expertise;
+ }
+
+ public function buildCondition(PlFilter &$uf)
+ {
+ $sub = $uf->addMentorFilter(UserFilter::MENTOR_EXPERTISE);
+ return $sub . '.expertise ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->expertise);
+ }
+}
+// }}}
+
+// {{{ class UFC_Mentor_Country
+/** Filters users by mentoring country
+ * @param $country Two-letters code of country being searched
+ */
+class UFC_Mentor_Country implements UserFilterCondition
+{
+ private $country;
+
+ public function __construct($country)
+ {
+ $this->country = $country;
+ }
+
+ public function buildCondition(PlFilter &$uf)
+ {
+ $sub = $uf->addMentorFilter(UserFilter::MENTOR_COUNTRY);
+ return $sub . '.country = ' . XDB::format('{?}', $this->country);
+ }
+}
+// }}}
+
+// {{{ class UFC_Mentor_Sectorization
+/** Filters users based on mentoring (sub|)sector
+ * @param $sector ID of (sub)sector
+ * @param $type Whether we are looking for a sector or a subsector
+ */
+class UFC_Mentor_Sectorization implements UserFilterCondition
+{
+ const SECTOR = 1;
+ const SUBSECTOR = 2;
+ private $sector;
+ private $type;
+
+ public function __construct($sector, $type = self::SECTOR)
+ {
+ $this->sector = $sector;
+ $this->type = $type;
+ }
+
+ public function buildCondition(PlFilter &$uf)
+ {
+ $sub = $uf->addMentorFilter(UserFilter::MENTOR_SECTOR);
+ if ($this->type == self::SECTOR) {
+ $field = 'sectorid';
+ } else {
+ $field = 'subsectorid';
+ }
+ return $sub . '.' . $field . ' = ' . XDB::format('{?}', $this->sector);
+ }
+}
+// }}}