Cheetah
match.inc.php
Go to the documentation of this file.
1 <?php
2 
8 require_once(CH_DIRECTORY_PATH_CLASSES . 'ChWsbProfileFields.php');
9 
10 function getMatchFields()
11 {
13 
14  return $oDb->fromCache('sys_profile_match_fields', 'getAllWithKey',
15  'SELECT `ID`, `Name`, `MatchField`, `MatchPercent`, `Type` FROM `sys_profile_fields` WHERE `MatchPercent` > 0',
16  'ID');
17 }
18 
19 function getMatchProfiles($iProfileId, $bForce = false, $sSort = 'none')
20 {
21  $aResult = array();
22  if (!getParam('enable_match')) {
23  return $aResult;
24  }
25 
27 
28  if (!(int)$iProfileId) {
29  return $aResult;
30  }
31 
32  if (!$bForce) {
33  $aMatch = $oDb->getRow("SELECT `profiles_match` FROM `sys_profiles_match` WHERE `profile_id` = ? AND `sort` = ?",
34  [$iProfileId, $sSort]);
35  if (!empty($aMatch)) {
36  return unserialize($aMatch['profiles_match']);
37  }
38  } else {
39  $oDb->query("DELETE FROM `sys_profiles_match` WHERE `profile_id` = $iProfileId");
40  }
41 
42  $aProf = getProfileInfo($iProfileId);
43 
44  if (empty($aProf)) {
45  return $aResult;
46  }
47 
48  $aMathFields = getMatchFields();
49  $iAge = (int)$oDb->getOne("SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(), '{$aProf['DateOfBirth']}')), '%Y') + 0 AS age");
50 
51  foreach ($aMathFields as $sKey => $aFields) {
52  // TODO: pdo dynamic bindings
53  $aMathFields[$sKey]['profiles'] = array();
54 
55  if ($aProf[$aFields['Name']] && $aMathFields[$aFields['MatchField']]['Name']) {
56  if ($aMathFields[$aFields['MatchField']]['Name'] == 'DateOfBirth') {
57  if ($iAge) {
58  $sCond = "(DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(), `DateOfBirth`)), '%Y') + 0) = $iAge";
59  }
60  } elseif ($aMathFields[$aFields['MatchField']]['Type'] == 'select_set' && $aFields['Type'] != 'select_set') {
61  $sCond = "FIND_IN_SET('" . process_db_input($aProf[$aFields['Name']], CH_TAGS_NO_ACTION,
62  CH_SLASHES_NO_ACTION) . "', `{$aMathFields[$aFields['MatchField']]['Name']}`)";
63  } elseif ($aMathFields[$aFields['MatchField']]['Type'] != 'select_set' && $aFields['Type'] == 'select_set') {
64  $sCond = "FIND_IN_SET(`{$aMathFields[$aFields['MatchField']]['Name']}`, '" . process_db_input($aProf[$aFields['Name']],
66  } elseif ($aMathFields[$aFields['MatchField']]['Type'] == 'select_set' && $aFields['Type'] == 'select_set') {
67  $a = explode(',', $aProf[$aFields['Name']]);
68  if ($a) {
69  $sCond = '(';
70  foreach ($a as $sVal) {
71  $sCond .= "FIND_IN_SET('" . process_db_input($sVal, CH_TAGS_NO_ACTION,
72  CH_SLASHES_NO_ACTION) . "', `{$aMathFields[$aFields['MatchField']]['Name']}`) OR ";
73  }
74  $sCond = rtrim($sCond, ' OR');
75  $sCond .= ')';
76  }
77  } elseif ($aMathFields[$aFields['MatchField']]['Name']) {
78  $sCond = "`{$aMathFields[$aFields['MatchField']]['Name']}` = '" . process_db_input($aProf[$aFields['Name']],
80  }
81  if ($sCond) {
82  $aMathFields[$sKey]['profiles'] = $oDb->getAllWithKey("SELECT `ID` FROM `Profiles` WHERE `Status` = 'Active' AND `ID` != ? AND $sCond",
83  'ID', [$iProfileId]);
84  }
85  }
86  }
87 
88  $sCondSort = '';
89  if ($sSort == 'activity') {
90  $sCondSort = 'ORDER BY `DateLastNav` DESC';
91  } else {
92  if ($sSort == 'date_reg') {
93  $sCondSort = 'ORDER BY `DateReg` DESC';
94  }
95  }
96 
97  $iPercentThreshold = getParam('match_percent');
98  $aProfiles = $oDb->getColumn("SELECT `ID` FROM `Profiles` WHERE `Status` = 'Active' AND `ID` != $iProfileId $sCondSort");
99  foreach ($aProfiles as $iProfId) {
100  $iPercent = 0;
101 
102  foreach ($aMathFields as $sKey => $aFields) {
103  if (isset($aFields['profiles'][$iProfId])) {
104  $iPercent += (int)$aFields['MatchPercent'];
105  }
106  }
107 
108  if ($iPercent >= $iPercentThreshold) {
109  $aResult[] = $iProfId;
110  }
111  }
112 
113  $oDb->query("INSERT INTO `sys_profiles_match`(`profile_id`, `sort`, `profiles_match`) VALUES(?, ?, ?)", [
114  $iProfileId,
115  $sSort,
116  serialize($aResult)
117  ]);
118 
119  return $aResult;
120 }
121 
122 function getProfilesMatch($iPID1 = 0, $iPID2 = 0)
123 {
124  if (!getParam('enable_match')) {
125  return 0;
126  }
127 
128  $iPID1 = (int)$iPID1;
129  $iPID2 = (int)$iPID2;
130 
131  if (!$iPID1 or !$iPID2) {
132  return 0;
133  }
134 
135  if ($iPID1 == $iPID2) {
136  return 0;
137  }
138 
139  $aProf1 = getProfileInfo($iPID1);
140  $aProf2 = getProfileInfo($iPID2);
141 
142  if (empty($aProf1) || empty($aProf2)) {
143  return 0;
144  }
145 
146  $iMatch = 0;
147  $aMathFields = getMatchFields();
148  foreach ($aMathFields as $sKey => $aFields) {
149  $bRes = false;
150 
151  if ($aProf1[$aFields['Name']]) {
152  if ($aMathFields[$aFields['MatchField']]['Name'] == 'DateOfBirth') {
153  $bRes = age($aProf1['DateOfBirth']) == age($aProf2['DateOfBirth']);
154  } elseif ($aMathFields[$aFields['MatchField']]['Type'] == 'select_set' || $aFields['Type'] == 'select_set') {
155  $a1 = explode(',', $aProf1[$aFields['Name']]);
156  $a2 = explode(',', $aProf2[$aMathFields[$aFields['MatchField']]['Name']]);
157  $bRes = array_intersect($a1, $a2);
158  } else {
159  $bRes = $aProf1[$aFields['Name']] == $aProf2[$aMathFields[$aFields['MatchField']]['Name']];
160  }
161  }
162 
163  if ($bRes) {
164  $iMatch += (int)$aFields['MatchPercent'];
165  }
166  }
167 
168  return $iMatch;
169 }
process_db_input
process_db_input($sText, $iStripTags=0)
Definition: utils.inc.php:256
$aResult
$aResult
Definition: index.php:19
getProfilesMatch
getProfilesMatch($iPID1=0, $iPID2=0)
Definition: match.inc.php:122
php
CH_SLASHES_NO_ACTION
const CH_SLASHES_NO_ACTION
Definition: utils.inc.php:30
$aFields
$aFields
Definition: preValues.php:19
$sSort
$sSort
Definition: browse.php:27
getParam
getParam($sParamName, $bUseCache=true)
Definition: db.inc.php:130
getMatchProfiles
getMatchProfiles($iProfileId, $bForce=false, $sSort='none')
Definition: match.inc.php:19
$oDb
global $oDb
Definition: db.inc.php:39
CH_TAGS_NO_ACTION
const CH_TAGS_NO_ACTION
Definition: utils.inc.php:21
age
age( $birth_date)
Definition: profiles.inc.php:119
getProfileInfo
getProfileInfo($iProfileID=0, $checkActiveStatus=false, $forceCache=false)
Definition: profiles.inc.php:249
getMatchFields
getMatchFields()
Definition: match.inc.php:10
$iProfId
$iProfId
Definition: short_profile_info.php:22
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChWsbDb\getInstance
static getInstance()
Definition: ChWsbDb.php:82
$iProfileId
if( $sMembersList) $iProfileId
Definition: communicator.php:29
or
Voluntary License Schemes The Licensor waives the right to collect whether individually or
Definition: license.txt:37