| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup Messenger Messenger
- * @ingroup UnaModules
- *
- * @{
- */
- /**
- * Database queries
- */
- class BxMessengerDb extends BxBaseModGeneralDb
- {
- private $CNF;
- function __construct(&$oConfig)
- {
- parent::__construct($oConfig);
- $this->CNF = &$oConfig->CNF;
- }
- /**
- * Get all lot by class name
- *@param string $sClass
- *@return array lot info
- */
- public function getLotByClass($sClass)
- {
- if (!$sClass)
- return '';
- $sQuery = $this->prepare("SELECT * FROM `{$this->CNF['TABLE_ENTRIES']}` WHERE `{$this->CNF['FIELD_CLASS']}` = ? LIMIT 1", $sClass);
- return $this->getRow($sQuery);
- }
- /**
- * Get all lot by page url
- *@param string $sUrl or the page
- *@return array lot info
- */
- public function getLotByUrl($sUrl)
- {
- $sQuery = $this->prepare("SELECT * FROM `{$this->CNF['TABLE_ENTRIES']}` WHERE `{$this->CNF['FIELD_URL']}` = ? LIMIT 1", $sUrl);
- return $this->getRow($sQuery);
- }
- /**
- * Get all lot by id
- *@param int $iId lot id
- *@return array lot info
- */
- public function getLotInfoById($iId)
- {
- $sQuery = $this->prepare("SELECT `l`.*, `g`.`{$this->CNF['FMGL_GROUP_ID']}` FROM `{$this->CNF['TABLE_ENTRIES']}` as `l`
- LEFT JOIN `{$this->CNF['TABLE_GROUPS_LOTS']}` as `g` ON `g`.`{$this->CNF['FMGL_LOT_ID']}` = `l`.`{$this->CNF['FIELD_ID']}`
- WHERE `l`.`{$this->CNF['FIELD_ID']}` = ? LIMIT 1", (int) $iId);
- return $this->getRow($sQuery);
- }
- /**
- * Get lot by id or url
- *@param int $iId lot id
- *@param string $sUrl url or url of the page with talk
- *@param int $iAuthor profile id
- *@return array lot info
- */
- public function getLotByIdOrUrl($iId, $sUrl, $iAuthorId = 0)
- {
- if ($iId)
- return $this->getLotInfoById($iId);
- if ($sUrl)
- return $this->getLotByUrl($sUrl);
- return array();
- }
- /**
- * Check if is the member author of the lot or jot
- *@param int $iId lot id
- *@param int $iAuthor profile id
- *@param boolean $bLotAuthor if true checks Lot, otherwise jot
- *@return boolean
- */
- public function isAuthor($iId, $iAuthorId, $bLotAuthor = true)
- {
- if (!$iAuthorId)
- return false;
- if ($bLotAuthor)
- $sQuery = $this->prepare("SELECT COUNT(*) FROM `{$this->CNF['TABLE_ENTRIES']}` WHERE `{$this->CNF['FIELD_ID']}` = ? AND `{$this->CNF['FIELD_AUTHOR']}` = ? LIMIT 1", (int) $iId, (int) $iAuthorId);
- else
- $sQuery = $this->prepare("SELECT COUNT(*) FROM `{$this->CNF['TABLE_MESSAGES']}` WHERE `{$this->CNF['FIELD_MESSAGE_ID']}` = ? AND `{$this->CNF['FIELD_MESSAGE_AUTHOR']}` = ? LIMIT 1", (int) $iId, (int) $iAuthorId);
- return $this->getOne($sQuery) == 1;
- }
- /**
- * Deletes lot by lot Id
- *@param int $iLotId lot id
- *@return int affected rows
- */
- public function deleteLot($iLotId)
- {
- $aJots = $this->getJotsByLotId(['lot' => $iLotId]);
- foreach ($aJots as $iKey => $aJot)
- $this->clearJotsConnections($aJot[$this->CNF['FIELD_MESSAGE_ID']], $iLotId);
- $this->query("DELETE FROM `{$this->CNF['TABLE_NEW_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_NEW_LOT']}`=:lot
- ", array('lot' => $iLotId));
- $this->query("DELETE FROM `{$this->CNF['TABLE_ENTRIES']}` WHERE `{$this->CNF['FIELD_ID']}` = :id", array('id' => $iLotId));
- $this->query("DELETE FROM `{$this->CNF['TABLE_LOT_SETTINGS']}` WHERE `{$this->CNF['FLS_ID']}` = :id", array('id' => $iLotId));
- $this->query("DELETE FROM `{$this->CNF['TABLE_USERS_INFO']}` WHERE `{$this->CNF['FIELD_INFO_LOT_ID']}` = :id", array('id' => $iLotId));
- $this->query("DELETE FROM `{$this->CNF['TABLE_MESSAGES']}` WHERE `{$this->CNF['FIELD_MESSAGE_FK']}` = :id", array('id' => $iLotId));
- $this->query("DELETE FROM `{$this->CNF['TABLE_MASS_TRACKER']}` WHERE `{$this->CNF['FIELD_MASS_CONVO_ID']}` = :id", array('id' => $iLotId));
- $this->removeNotifications(0, $iLotId);
- return true;
- }
- /**
- * Clear lot history
- *@param int $iLotId lot id
- *@return boolean result
- */
- public function clearLot($iLotId)
- {
- $aJots = $this->getJotsByLotId(['lot' => $iLotId]);
- foreach ($aJots as $iKey => $aJot)
- $this->clearJotsConnections($aJot[$this->CNF['FIELD_MESSAGE_ID']], $iLotId);
- return $this->query("DELETE FROM `{$this->CNF['TABLE_MESSAGES']}` WHERE `{$this->CNF['FIELD_MESSAGE_FK']}` = :id", array('id' => $iLotId));
- }
- /**
- * Deletes jot
- *@param int iJotId jot id
- *@param int iProfileId member who deleted jot
- *@param boolean $bCompletely if true totally remove from the site
- *@return int affected rows
- */
- public function deleteJot($iJotId, $iProfileId, $bCompletely = false)
- {
- $iJotId = (int) $iJotId;
- if ($bCompletely) {
- $this->clearJotsConnections($iJotId);
- return $this->query("DELETE FROM `{$this->CNF['TABLE_MESSAGES']}` WHERE `{$this->CNF['FIELD_MESSAGE_ID']}` = :id", array('id' => $iJotId));
- }
- return $this->query("UPDATE `{$this->CNF['TABLE_MESSAGES']}`
- SET
- `{$this->CNF['FIELD_MESSAGE_TRASH']}` = 1,
- `{$this->CNF['FIELD_MESSAGE_LAST_EDIT']}` = UNIX_TIMESTAMP(),
- `{$this->CNF['FIELD_MESSAGE_EDIT_BY']}` = :profile
- WHERE `{$this->CNF['FIELD_MESSAGE_ID']}` = :id", array('id' => $iJotId, 'profile' => $iProfileId));
- }
- public function clearJotsConnections($iJotId, $iLotId = 0)
- {
- $this->removeFilesByJotId($iJotId);
- $this->deleteJotReactions($iJotId);
- $this->deleteSavedJotItems($iJotId);
- if (!$iLotId)
- $iLotId = $this->getLotByJotId($iJotId, true);
- if ($iLotId) {
- $aUnreadJots = $this->getAll("SELECT *
- FROM `{$this->CNF['TABLE_NEW_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_NEW_JOT']}`<=:jot AND `{$this->CNF['FIELD_NEW_LOT']}`=:lot", array('jot' => $iJotId, 'lot' => $iLotId));
- foreach ($aUnreadJots as &$aUnreadJot) {
- $iProfileId = (int) $aUnreadJot[$this->CNF['FIELD_NEW_PROFILE']];
- $iUnreadJotId = (int) $aUnreadJot[$this->CNF['FIELD_NEW_JOT']];
- if ($iUnreadJotId && $iUnreadJotId < (int) $iJotId)
- $this->query("
- UPDATE `{$this->CNF['TABLE_NEW_MESSAGES']}`
- SET `{$this->CNF['FIELD_NEW_UNREAD']}`=`{$this->CNF['FIELD_NEW_UNREAD']}` - 1
- WHERE `{$this->CNF['FIELD_NEW_LOT']}`=:lot AND `{$this->CNF['FIELD_NEW_PROFILE']}`=:profile AND `{$this->CNF['FIELD_NEW_UNREAD']}` > 0
- ", array('lot' => $iLotId, 'profile' => $iProfileId));
- else
- $this->deleteNewJot($iProfileId, $iLotId, $iJotId);
- }
- }
- }
- public function updateJot($iJotId, $sField, $mixedValue)
- {
- return $this->query("UPDATE `{$this->CNF['TABLE_MESSAGES']}`
- SET
- `{$sField}` =:value
- WHERE `{$this->CNF['FIELD_MESSAGE_ID']}` = :id", array('id' => $iJotId, 'value' => $mixedValue));
- }
- /**
- * Removes participant from the participants list
- *@param int $iLotId lot id
- *@param int $iParticipant profile id
- *@return int/false
- */
- public function leaveLot($iLotId, $iParticipant)
- {
- $this->deleteNewJot($iParticipant, $iLotId);
- return $this->removeParticipant($iLotId, $iParticipant);
- }
- /**
- * Get Lot(Talk) settings for a member
- *@param int $iLotId lot id
- *@param int $iParticipant profile id
- *@param string $sName name the option
- *@param boolean $bParams get from params array, otherwise from table's field
- *@return mixed option value
- */
- private function getParams($iLotId, $iParticipant, $sName = 0, $bParams = false)
- {
- $sQuery = $this->prepare("SELECT * FROM `{$this->CNF['TABLE_USERS_INFO']}`
- WHERE `{$this->CNF['FIELD_INFO_LOT_ID']}` = ? AND `{$this->CNF['FIELD_INFO_USER_ID']}` = ?
- LIMIT 1", (int) $iLotId, (int) $iParticipant);
- $aInfo = $this->getRow($sQuery);
- if (empty($aInfo))
- return false;
- if (!$sName)
- return $aInfo;
- if (!$bParams) {
- $aParams = unserialize($aInfo[$this->CNF['FIELD_INFO_PARAMS']]);
- return isset($aParams[$sName]) ? $aParams[$sName] : 0;
- }
- return $aInfo[$sName];
- }
- /**
- * Save Lot(Talk) settings for a member
- *@param int $iLotId lot id
- *@param int $iParticipant profile id
- *@param array $aParams options list
- *@param boolean $bParams get from params array otherwise as table field
- *@return int affected rows
- */
- private function setParams($iLotId, $iParticipant, $sName, $sValue, $bParams = false)
- {
- $aParams = $this->getParams($iLotId, $iParticipant);
- if (!$bParams) {
- if (empty($aParams))
- return $this->query("INSERT INTO `{$this->CNF['TABLE_USERS_INFO']}` SET `{$this->CNF['FIELD_INFO_PARAMS']}` = :values, `{$this->CNF['FIELD_INFO_LOT_ID']}` = :id, `{$this->CNF['FIELD_INFO_USER_ID']}` = :user", array('user' => $iParticipant, 'id' => $iLotId, 'values' => serialize(array($sName => $sValue))));
- return $this->query("UPDATE `{$this->CNF['TABLE_USERS_INFO']}` SET `{$this->CNF['FIELD_INFO_PARAMS']}` = :values WHERE `{$this->CNF['FIELD_INFO_LOT_ID']}` = :id AND `{$this->CNF['FIELD_INFO_USER_ID']}` = :user", array('user' => $iParticipant, 'id' => $iLotId, 'values' => serialize(array($sName => $sValue))));
- }
- if (empty($aParams))
- return $this->query("INSERT INTO `{$this->CNF['TABLE_USERS_INFO']}` SET `{$sName}` = :value, `{$this->CNF['FIELD_INFO_LOT_ID']}` = :id, `{$this->CNF['FIELD_INFO_USER_ID']}` = :user", array('user' => $iParticipant, 'id' => $iLotId, 'value' => $sValue));
- return $this->query("UPDATE `{$this->CNF['TABLE_USERS_INFO']}` SET `{$sName}` = :value WHERE `{$this->CNF['FIELD_INFO_LOT_ID']}` = :id AND `{$this->CNF['FIELD_INFO_USER_ID']}` = :user", array('user' => $iParticipant, 'id' => $iLotId, 'value' => $sValue));
- }
- /**
- * Make the lot mute for a member
- *@param int $iLotId lot id
- *@param int $iParticipant profile id
- *@return int set value
- */
- public function muteLot($iLotId, $iParticipant)
- {
- $iCurrentNotification = (int) $this->getParams($iLotId, $iParticipant, 'notification');
- $iNotification = (int) !$iCurrentNotification;
- $this->setParams($iLotId, $iParticipant, 'notification', $iNotification);
- return $iNotification;
- }
- /**
- * Mark the lot with star
- *@param int $iLotId lot id
- *@param int $iParticipant profile id
- *@return int set
- */
- public function starLot($iLotId, $iParticipant)
- {
- $iCurrent = (int) $this->getParams($iLotId, $iParticipant, 'star', true);
- $iNew = (int) !$iCurrent;
- $this->setParams($iLotId, $iParticipant, 'star', $iNew, true);
- return $iNew;
- }
- /**
- * Talks from the list that the member marked with a star
- *@param int $iProfileId profile id
- *@param array $aLotIds lots ids
- *@return array lots ids
- */
- public function getStarredLots($iProfileId, $aLotIds)
- {
- $aLotIds = array_filter(array_map('intval', (array)$aLotIds));
- if (!$iProfileId || empty($aLotIds)) {
- return [];
- }
- return $this->getColumn("SELECT `{$this->CNF['FIELD_INFO_LOT_ID']}` FROM `{$this->CNF['TABLE_USERS_INFO']}`
- WHERE `{$this->CNF['FIELD_INFO_USER_ID']}` = :profile AND `{$this->CNF['FIELD_INFO_STAR']}` = 1
- AND `{$this->CNF['FIELD_INFO_LOT_ID']}` IN (" . implode(',', $aLotIds) . ")", ['profile' => (int)$iProfileId]);
- }
- public function isStarred($iLotId, $iParticipant)
- {
- return $this->getParams($iLotId, $iParticipant, 'star', true);
- }
- /**
- * Check if the lot is mute for a member
- *@param int $iLotId lot id
- *@param int $iParticipant profile id
- *@return boolean
- */
- public function isMuted($iLotId, $iParticipant)
- {
- return $this->getParams($iLotId, $iParticipant, 'notification');
- }
- /**
- /**
- * Save participants list for the lot
- *@param int $iLotId lot id
- *@param array $aParticipants
- *@return int affected rows
- */
- public function saveParticipantsList($iLotId, $aParticipants)
- {
- $sParticipants = '';
- if (!empty($aParticipants) && is_array($aParticipants)) {
- $aParticipants = array_map('intval', $aParticipants);
- $sParticipants = implode(',', $aParticipants);
- }
- return $this->query("UPDATE `{$this->CNF['TABLE_ENTRIES']}` SET `{$this->CNF['FIELD_PARTICIPANTS']}` = :parts WHERE `{$this->CNF['FIELD_ID']}` = :id", array('parts' => $sParticipants, 'id' => $iLotId));
- }
- public function findThePageOwner($mixedPage)
- {
- $iAuthorId = 0;
- if (!$mixedPage)
- return $iAuthorId;
- if (is_string($mixedPage))
- parse_str($mixedPage, $aUrl);
- else if (is_array($mixedPage))
- $aUrl = $mixedPage;
- if (empty($aUrl) || !isset($aUrl['id']) || !isset($aUrl['i']))
- return $iAuthorId;
- $sModule = $this->getOne("SELECT `module` FROM `sys_objects_page` WHERE LOWER(`uri`)=:uri", array('uri' => strtolower($aUrl['i'])));
- if (!$sModule)
- return $iAuthorId;
- $aTable = $this->getRow("SELECT `TriggerTable`, `TriggerFieldId`, `TriggerFieldAuthor`
- FROM `sys_objects_cmts`
- WHERE `Module`=:module LIMIT 1", array('module' => $sModule));
- if (empty($aTable))
- return $iAuthorId;
- if (isset($aTable['TriggerTable']) && isset($aTable['TriggerFieldId']) && isset($aTable['TriggerFieldAuthor']))
- return $this->getOne(
- "SELECT `{$aTable['TriggerFieldAuthor']}`
- FROM `{$aTable['TriggerTable']}`
- WHERE `{$aTable['TriggerFieldId']}`=:id",
- array('id' => $aUrl['id'])
- );
- return $iAuthorId;
- }
- function createLot($iProfileId, $aData)
- {
- $sUrl = isset($aData['url']) ? $aData['url'] : '';
- $aParticipants = isset($aData['participants']) ? $aData['participants'] : array();
- $iAuthorId = $this->findThePageOwner($sUrl);
- if (isset($aData['thread']) || !$iAuthorId)
- $iAuthorId = $iProfileId;
- $iLotId = $this->createNewLot($iAuthorId, $aData, $aParticipants);
- if (!$iLotId)
- return false;
- bx_alert($this->_oConfig->getObject('alert'), 'create_lot', $iLotId, $iProfileId, $this->getLotInfoById($iLotId));
- foreach ($aParticipants as &$iParticipant) {
- if ((int) $iParticipant === (int) $iProfileId)
- continue;
- bx_alert($this->_oConfig->getObject('alert'), 'add_part', $iParticipant, $iProfileId, array('lot_id' => $iLotId, 'author_id' => $iAuthorId));
- }
- return $iLotId;
- }
- /**
- * Save message for participants to database
- *@param array $aData lot settings
- *@param array $aParticipants participants list, if it empty then used default for lot
- *@return int affected rows
- */
- public function saveMessage($aData)
- {
- $iLotId = isset($aData['lot']) ? (int) $aData['lot'] : 0;
- $aLotInfo = $iLotId ? $this->getLotInfoById($iLotId) : [];
- if (empty($aLotInfo) && $aData['type'] == BX_IM_TYPE_PRIVATE && !$aData['title'])
- $aLotInfo = $this->findLotByParams($aData);
- if ($iLotId && $aData['type'] == BX_IM_TYPE_PRIVATE && !$this->isParticipant($iLotId, $aData['member_id']))
- return false;
- if (empty($aLotInfo)) {
- $iLotId = $this->createLot((int) $aData['member_id'], $aData);
- if (isset($aData['group_id']) && (int) $aData['group_id'] && (int) $iLotId)
- $this->addLotToGroup($iLotId, $aData['group_id']);
- } else
- $iLotId = $aLotInfo[$this->CNF['FIELD_ID']];
- if (($aData['type'] != BX_IM_TYPE_PRIVATE || $this->isAuthor($iLotId, $aData['member_id'])) && !$this->isParticipant($iLotId, $aData['member_id'], true)) {
- if ($this->addMemberToParticipantsList($iLotId, $aData['member_id']))
- bx_alert(
- $this->_oConfig->getObject('alert'),
- 'participant_joined',
- $iLotId,
- $aData['member_id'],
- array(
- 'url' => $aData['url'],
- 'object_author_id' => !empty($aLotInfo[$this->CNF['FIELD_AUTHOR']]) ? $aLotInfo[$this->CNF['FIELD_AUTHOR']] : $this->findThePageOwner($aData['url'])
- )
- );
- }
- $aData['message'] = clear_xss($aData['message']);
- return $this->addJot($iLotId, $aData['message'], $aData['member_id']);
- }
- /**
- * Find appropriate Talk using input params
- *@param array $aData params
- *@return array Lot info
- */
- public function findLotByParams($aData)
- {
- $sUrl = isset($aData['url']) ? $aData['url'] : '';
- $aParticipants = isset($aData['participants']) && is_array($aData['participants']) ? $aData['participants'] : [];
- $sClass = isset($aData['class']) ? $aData['class'] : '';
- $iType = isset($aData['type']) ? $aData['type'] : 0;
- $aWhere = $aWhereBindings = [];
- $aLots = [];
- if (!empty($aParticipants)) {
- $aItems = $this->getLotsByParticipantsList($aParticipants, $iType, true);
- foreach ($aItems as &$aItem)
- $aLots[] = $aItem[$this->CNF['FIELD_ID']];
- if (!empty($aLots))
- $aWhere[] = "`{$this->CNF['FIELD_ID']}` IN (" . implode(',', $aLots) . ")";
- else
- return [];
- }
- if ($sClass) {
- $aWhere[] = "`{$this->CNF['FIELD_CLASS']}` =:class";
- $aWhereBindings['class'] = $sClass;
- }
- if ($sUrl) {
- $aWhere[] = "`{$this->CNF['FIELD_URL']}` =:url";
- $aWhereBindings['url'] = $sUrl;
- }
- if ($iType) {
- $aWhere[] = "`{$this->CNF['FIELD_TYPE']}` =:type";
- $aWhereBindings['type'] = $iType;
- }
- return $this->getRow("SELECT * FROM `{$this->CNF['TABLE_ENTRIES']}`
- WHERE " . implode(' AND ', $aWhere) . " LIMIT 1", $aWhereBindings);
- }
- function getLotsByParticipantsList($aParticipants, $mixedType = false, $bGetAll = false)
- {
- $aParticipantsList = $aResult = [];
- if (empty($aParticipants))
- return $aResult;
- foreach ($aParticipants as &$iParticipant)
- $aParticipantsList[] = "(^|,)" . (int) $iParticipant . "(,|$)";
- if (empty($aParticipantsList))
- return $aResult;
- $aWhere[] = "`{$this->CNF['FIELD_PARTICIPANTS']}` REGEXP '" . implode('|', $aParticipantsList) . "'";
- if ($mixedType)
- $aWhere[] = "`{$this->CNF['FIELD_TYPE']}` = " . $mixedType;
- $aLots = $this->getAll("SELECT * FROM `{$this->CNF['TABLE_ENTRIES']}` WHERE " . implode(' AND ', $aWhere) . " ORDER BY `{$this->CNF['FIELD_ID']}` ASC ");
- if (empty($aLots))
- return $aResult;
- foreach ($aLots as &$aLot) {
- $aTalkParticipants = explode(',', $aLot[$this->CNF['FIELD_PARTICIPANTS']]);
- if (count($aTalkParticipants) != count($aParticipants))
- continue;
- sort($aTalkParticipants);
- sort($aParticipants);
- if (array_values($aTalkParticipants) == array_values($aParticipants))
- $aResult[] = $aLot;
- }
- return !$bGetAll && !empty($aResult) ? current($aResult) : $aResult;
- }
- /**
- * Mark message as read in lot history
- *@param int $iJotId message id
- *@param int iProfileId
- */
- public function readMessage($iJotId, $iProfileId)
- {
- $aJotInfo = $this->getJotById($iJotId);
- if (!empty($aJotInfo) && $this->deleteNewJot($iProfileId, $aJotInfo[$this->CNF['FIELD_MESSAGE_FK']], $iJotId))
- bx_alert($this->_oConfig->getObject('alert'), 'read_jot', $iJotId, $iProfileId, array('author_id' => $aJotInfo[$this->CNF['FIELD_MESSAGE_AUTHOR']], 'lot_id' => $aJotInfo[$this->CNF['FIELD_MESSAGE_FK']]));
- }
- /**
- * Mark all message as read in lot history for member
- * @param int $iLot lot id
- * @param int iProfileId
- * @return false
- */
- public function readAllMessages($iLot, $iProfileId)
- {
- $CNF = &$this->_oConfig->CNF;
- $aNew = $this->getNewJots($iProfileId, $iLot);
- if (empty($aNew))
- return false;
- bx_alert(
- $this->_oConfig->getObject('alert'),
- 'read_bulk_jot',
- $iLot,
- $iProfileId,
- array(
- 'first_jot' => $aNew[$CNF['FIELD_NEW_JOT']],
- 'count' => $aNew[$CNF['FIELD_NEW_JOT']],
- 'lot_id' => $iLot
- )
- );
- $this->deleteNewJot($iProfileId, $iLot);
- $this->markNotificationAsRead($iProfileId, $iLot);
- }
- /**
- *Add new new jot to database
- *@param int $iLotID lot id
- *@param string $sMessage posted message
- *@param int iProfile Id owner of the message
- *@return int affected rows
- */
- public function addJot($iLotId, $sMessage, $iProfileId)
- {
- $sQuery = $this->prepare("INSERT INTO `{$this->CNF['TABLE_MESSAGES']}`
- SET `{$this->CNF['FIELD_MESSAGE']}` = ?,
- `{$this->CNF['FIELD_MESSAGE_FK']}` = ?,
- `{$this->CNF['FIELD_MESSAGE_AUTHOR']}` = ?,
- `{$this->CNF['FIELD_MESSAGE_ADDED']}` = UNIX_TIMESTAMP()", $sMessage, $iLotId, $iProfileId);
- if (!$this->query($sQuery))
- return false;
- $iJotId = $this->lastId();
- $aParticipants = $this->getParticipantsList($iLotId, true, $iProfileId);
- $this->markAsNewJot($aParticipants, $iLotId, $iJotId);
- return $iJotId;
- }
- /**
- *Create new chat/lot with list of participants
- *@param int iProfileId owner of the lot
- *@param string $sTitle lot title
- *@param int $iType type of the lot
- *@param string $sUrl url of the page
- *@param array $aParticipants list of participants
- *@return int affected rows
- */
- public function createNewLot($iProfileId, $aData, $aParticipants = array())
- {
- $CNF = &$this->_oConfig->CNF;
- // before to create lot
- bx_alert($this->_oConfig->getObject('alert'), 'create_lot_before', 0, $iProfileId, array(
- 'data' => &$aData,
- 'participants' => &$aParticipants
- ));
- $sTitle = isset($aData['title']) ? $aData['title'] : '';
- $iType = isset($aData['type']) ? (int) $aData['type'] : BX_IM_TYPE_PRIVATE;
- $sUrl = isset($aData['url']) ? $aData['url'] : '';
- $sPage = isset($aData['page']) ? $aData['page'] : '';
- $sClass = isset($aData['class']) ? $aData['class'] : BX_MSG_TALK_CLASS_CUSTOM;
- $iThread = isset($aData['thread']) ? (int) $aData['thread'] : BX_ATT_TYPE_CUSTOM;
- $iVisibility = isset($aData['visibility']) ? (int) $aData['visibility'] : 0;
- $sHash = $this->_oConfig->generateConvoHash(time());
- $mixedParticipants = !empty($aParticipants) ? implode(',', $aParticipants) : '';
- $sQuery = $this->prepare(
- "INSERT INTO `{$this->CNF['TABLE_ENTRIES']}`
- SET `{$this->CNF['FIELD_TITLE']}` = ?,
- `{$this->CNF['FIELD_TYPE']}` = ?,
- `{$this->CNF['FIELD_AUTHOR']}` = ?,
- `{$this->CNF['FIELD_ADDED']}` = UNIX_TIMESTAMP(),
- `{$this->CNF['FIELD_PARTICIPANTS']}` = ?,
- `{$this->CNF['FIELD_VISIBILITY']}` = ?,
- `{$this->CNF['FIELD_CLASS']}` = ?,
- `{$this->CNF['FIELD_PARENT_JOT']}` = ?,
- `{$this->CNF['FIELD_URL']}` = ?,
- `{$this->CNF['FIELD_HASH']}` = ?",
- $sTitle,
- $iType,
- $iProfileId,
- $mixedParticipants,
- $iVisibility,
- $sClass,
- $iThread,
- $sUrl,
- $sHash
- );
- return $this->query($sQuery) ? $this->lastId() : false;
- }
- /**
- *Get list of types of member lots
- *@param int iProfileId
- *@param string $sParam keyword to filter lots
- *@return array found lots types with lots count per each type
- */
- public function getMemberLotsTypes($iProfileId, $sParam = '')
- {
- $sWhere = '';
- $aWhere = array();
- if ($iProfileId) {
- $aSWhere[] = "FIND_IN_SET(:profile, `{$this->CNF['FIELD_PARTICIPANTS']}`)";
- $aWhere = array('profile' => $iProfileId);
- }
- if ($sParam) {
- $sParam = "%{$sParam}%";
- $aSWhere[] = " (`{$this->CNF['FIELD_TITLE']}` LIKE :title OR `{$this->CNF['FIELD_URL']}` LIKE :url OR `{$this->CNF['FIELD_TYPE']}` LIKE :type)";
- $aWhere = array_merge($aWhere, array('title' => $sParam, 'url' => $sParam, 'type' => $sParam));
- }
- if (!empty($aWhere))
- $sWhere = "WHERE (" . implode(' AND ', $aSWhere) . ')';
- return $this->getPairs("SELECT `{$this->CNF['FIELD_TYPE']}`, COUNT(*) as `count`
- FROM `{$this->CNF['TABLE_ENTRIES']}`
- {$sWhere}
- GROUP BY `{$this->CNF['FIELD_TYPE']}`", $this->CNF['FIELD_TYPE'], 'count', $aWhere);
- }
- /**
- *Get list of all existed types
- *@return array types
- */
- public function getAllLotsTypes()
- {
- return $this->getAll("SELECT * FROM `{$this->CNF['TABLE_TYPES']}` ORDER BY `{$this->CNF['FIELD_TYPE_ID']}` ASC");
- }
- /**
- *Get list of all existed types as pairs: Id - Value
- *@return array types
- */
- public function getLotsTypesPairs()
- {
- return $this->getPairs("SELECT * FROM `{$this->CNF['TABLE_TYPES']}` ORDER BY `{$this->CNF['FIELD_TYPE_ID']}`", $this->CNF['FIELD_TYPE_ID'], $this->CNF['FIELD_TYPE_NAME']);
- }
- public function getLotTypeId($sType)
- {
- return $this->getOne("SELECT `{$this->CNF['FIELD_TYPE_ID']}` FROM `{$this->CNF['TABLE_TYPES']}` WHERE `{$this->CNF['FIELD_TYPE_NAME']}`=:name LIMIT 1", ['name' => $sType]);
- }
- /**
- * Check if this ype of lot exists
- *@param int $iType type of the lot
- *@return boolean
- */
- public function isLotType($iType)
- {
- return $this->getOne("SELECT COUNT(*) FROM `{$this->CNF['TABLE_TYPES']}` WHERE `{$this->CNF['FIELD_TYPE_ID']}` = :type LIMIT 1", array('type' => $iType)) == 1;
- }
- /**
- * Get lot's participant list
- *@param int $iLotId lot id
- *@param boolean $bArray if true, returns result as array, otherwise string with ids separated commas
- *@param int $bExcludeProfile allows to exclude profile from the list, usually it is owner
- *@return string/array
- */
- public function getParticipantsList($iLotId, $bArray = true, $bExcludeProfile = 0)
- {
- $sParticipants = $this->getOne("SELECT `{$this->CNF['FIELD_PARTICIPANTS']}` FROM `{$this->CNF['TABLE_ENTRIES']}` WHERE `{$this->CNF['FIELD_ID']}` = :value", array('value' => $iLotId));
- if (!$sParticipants)
- return [];
- $aParticipants = explode(',', $sParticipants);
- if ($bExcludeProfile && ($iId = array_search($bExcludeProfile, $aParticipants)) !== FALSE)
- unset($aParticipants[$iId]);
- return !$bArray ? implode(',', $aParticipants) : $aParticipants;
- }
- /**
- * Check if it is participant of the lot
- *@param int $iLotId lot id
- *@param int $iParticipantId profile id
- *@param boolean $bAsParticipant if true to check only participants list without lot owner
- *@return boolean
- */
- public function isParticipant($iLotId, $iParticipantId, $bAsParticipant = false)
- {
- $aParticipants = $this->getParticipantsList($iLotId);
- if (array_search($iParticipantId, $aParticipants) !== FALSE)
- return true;
- $aLotInfo = $this->getLotInfoById($iLotId);
- if (!empty($aLotInfo) && $aLotInfo[$this->CNF['FIELD_TYPE']] == BX_IM_TYPE_BROADCAST)
- return $this->isBroadcastParticipant($iLotId, $iParticipantId);
- return !$bAsParticipant ? $this->getOne("SELECT COUNT(*) FROM `{$this->CNF['TABLE_ENTRIES']}` WHERE `{$this->CNF['FIELD_ID']}` = :lot AND `{$this->CNF['FIELD_AUTHOR']}` = :author LIMIT 1", array('lot' => (int) $iLotId, 'author' => (int) $iParticipantId)) == 1 : false;
- }
- /**
- * Add profile to lot's participants list
- *@param int $iLotId lot id
- *@param int $iParticipantId profile id
- *@return int affected rows
- */
- public function addMemberToParticipantsList($iLotId, $iParticipantId)
- {
- $sParticipants = $this->getParticipantsList($iLotId, false /* as string list */);
- $sParticipants = $sParticipants ? "{$sParticipants},{$iParticipantId}" : $iParticipantId;
- return $this->query("UPDATE `{$this->CNF['TABLE_ENTRIES']}` SET `{$this->CNF['FIELD_PARTICIPANTS']}` = :parts WHERE `{$this->CNF['FIELD_ID']}` = :id", array('parts' => $sParticipants, 'id' => $iLotId));
- }
- /**
- * Remove profile from participants list
- *@param int $iLotId lot id
- *@param int $iParticipantId profile id
- *@return mixed false or affected rows
- */
- public function removeParticipant($iLotId, $iParticipantId)
- {
- $aParticipants = $this->getParticipantsList($iLotId);
- $iKey = array_search($iParticipantId, $aParticipants);
- if ($iKey !== FALSE) {
- unset($aParticipants[$iKey]);
- return $this->saveParticipantsList($iLotId, $aParticipants);
- }
- return false;
- }
- /**
- * Get jots list for specified lot
- * @param $aData
- * @return array of the jots
- */
- public function getJotsByLotId($aData)
- { /* $iLotId, $iStart = BX_IM_EMPTY, $sMode = 'new', $iLimit = BX_IM_EMPTY, $bInclude = false */
- $iLotId = isset($aData['lot']) ? (int)$aData['lot'] : BX_IM_EMPTY;
- $iStart = isset($aData['start']) ? (int)$aData['start'] : BX_IM_EMPTY;
- $sMode = $aData['mode'] ?? 'new';
- $iLimit = isset($aData['limit']) ? (int) $aData['limit'] : BX_IM_EMPTY;
- $bInclude = isset($aData['include']) && $aData['include'];
- $sArea = $aData['area'] ?? '';
- $sJoin = $sWhereJoin = $sLimit = '';
- $aSWhere[] = "`m`.`{$this->CNF['FIELD_MESSAGE_FK']}` = :lot_id ";
- $aBindings['lot_id'] = (int) $iLotId;
- $sInsideOrder = $sMode == 'all' ? 'ASC' : 'DESC';
- if ($iStart && $sMode !== 'all') {
- $sEqual = '';
- if ($bInclude)
- $sEqual = '=';
- $aSWhere[] = "`m`.`{$this->CNF['FIELD_MESSAGE_ID']}` " . ($sMode == 'new' ? '>' . $sEqual : '<' . $sEqual) . " :start ";
- $aBindings['start'] = (int) $iStart;
- if ($sMode == 'new')
- $sInsideOrder = 'ASC';
- }
- if ($iLimit) {
- $sLimit = "LIMIT :limit";
- $aBindings['limit'] = (int) $iLimit;
- }
- if (!empty($aBindings))
- $sWhere = 'WHERE ' . implode(' AND ', $aSWhere);
- if ($sArea)
- list($sJoin, $sWhereJoin, $sFields, $sGroupBy) = $this->getJotsByArea($sArea);
- $sQuery = "SELECT `m`.* FROM `{$this->CNF['TABLE_MESSAGES']}` as `m`
- {$sJoin}
- {$sWhere} {$sWhereJoin}
- ORDER BY `m`.`{$this->CNF['FIELD_MESSAGE_ID']}` {$sInsideOrder}
- {$sLimit}";
- return $this->getAll($iStart && $sMode == 'new' ? $sQuery : "({$sQuery}) ORDER BY `{$this->CNF['FIELD_MESSAGE_ID']}`", $aBindings);
- }
- function getJotsByArea($sArea)
- {
- $CNF = &$this->_oConfig->CNF;
- $sGroupBy = $sJoin = $sWhere = $sFields = '';
- $iProfileId = bx_get_logged_profile_id();
- switch ($sArea) {
- case BX_MSG_TALK_TYPE_MR:
- $sUrl = BxDolProfile::getInstance($iProfileId)->getUrl();
- $sJoin = "LEFT JOIN `{$CNF['TABLE_JOT_REACTIONS']}` as `r` ON `m`.`{$CNF['FIELD_MESSAGE_ID']}` = `r`.`{$CNF['FIELD_REACT_JOT_ID']}`";
- $sWhere = "AND ((`r`.`{$CNF['FIELD_REACT_JOT_ID']}` IS NOT NULL AND `m`.`{$CNF['FIELD_MESSAGE_AUTHOR']}`= '{$iProfileId}') OR `m`.`{$CNF['FIELD_MESSAGE']}` LIKE '\"%" . '/' . bx_ltrim_str($sUrl, BX_DOL_URL_ROOT) . "\"')";
- break;
- case BX_MSG_TALK_TYPE_THREADS:
- break;
- case BX_MSG_TALK_TYPE_SAVED:
- $sJoin = "INNER JOIN `{$CNF['TABLE_SAVED_JOTS']}` as `s` ON `s`.`{$CNF['FSJ_ID']}` = `m`.`{$CNF['FIELD_MESSAGE_ID']}` AND `s`.`{$CNF['FSJ_PROFILE_ID']}` = " . (int)$iProfileId;
- $sWhere = "AND `s`.`{$CNF['FSJ_ID']}` IS NOT NULL";
- break;
- case BX_MSG_TALK_TYPE_REPLIES:
- $sJoin = "LEFT JOIN `{$CNF['TABLE_MESSAGES']}` as `r` ON `r`.`{$CNF['FIELD_MESSAGE_ID']}` = `m`.`{$CNF['FIELD_MESSAGE_REPLY']}`";
- $sWhere = "AND `m`.`{$CNF['FIELD_MESSAGE_REPLY']}` <> 0 AND `r`.`{$CNF['FIELD_MESSAGE_AUTHOR']}`={$iProfileId}";
- }
- return [$sJoin, $sWhere, $sFields, $sGroupBy];
- }
- public function getUnreadMessages($iProfileId, $iLotId, $mixedLimit = 10)
- {
- $sWhere = "WHERE `{$this->CNF['FIELD_MESSAGE_FK']}` = :lot_id";
- $aWhere = array('lot_id' => (int) $iLotId);
- $aLotInfo = $this->getNewJots($iProfileId, $iLotId);
- if (empty($aLotInfo))
- return false;
- $sWhere .= " AND `{$this->CNF['FIELD_MESSAGE_ID']}` > :jot";
- $aWhere['jot'] = $aLotInfo[$this->CNF['FIELD_NEW_JOT']];
- $sLimit = '';
- if ($mixedLimit)
- $sLimit = "LIMIT " . (int) $mixedLimit;
- $sQuery = "SELECT * FROM `{$this->CNF['TABLE_MESSAGES']}`
- {$sWhere}
- ORDER BY `{$this->CNF['FIELD_MESSAGE_ID']}`
- {$sLimit}";
- return $this->getAll($sQuery, $aWhere);
- }
- public function getPrevJot($iLotId, $iStart)
- {
- $sWhere = "WHERE `{$this->CNF['FIELD_MESSAGE_FK']}` = :lot_id";
- $aWhere = array('lot_id' => (int) $iLotId);
- if ($iStart) {
- $sWhere .= " AND `{$this->CNF['FIELD_MESSAGE_ID']}` < :jot";
- $aWhere['jot'] = $iStart;
- }
- $sQuery = "SELECT `{$this->CNF['FIELD_MESSAGE_ID']}` FROM `{$this->CNF['TABLE_MESSAGES']}`
- {$sWhere}
- ORDER BY `{$this->CNF['FIELD_MESSAGE_ID']}` DESC
- LIMIT 1";
- return $this->getOne($sQuery, $aWhere);
- }
- public function getIntervalJotsCount($iLotId, $iJotId)
- {
- $sQuery = "SELECT COUNT(*)
- FROM `{$this->CNF['TABLE_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_MESSAGE_FK']}`=:lot_id
- AND `{$this->CNF['FIELD_MESSAGE_ADDED']}` > (UNIX_TIMESTAMP() - :interval)
- AND `{$this->CNF['FIELD_MESSAGE_ID']}` < :jot";
- return $this->getOne($sQuery, array('lot_id' => (int) $iLotId, 'interval' => $this->CNF['PARAM_MESSAGES_INTERVAL'] * 60, 'jot' => (int) $iJotId));
- }
- function getLotJotsCount($iLotId)
- {
- if (!$iLotId)
- return false;
- // find the user with most number of unread messages
- $iUnread = $this->getOne("SELECT `{$this->CNF['FIELD_NEW_UNREAD']}` FROM `{$this->CNF['TABLE_NEW_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_NEW_LOT']}`=:id
- ORDER BY `{$this->CNF['FIELD_NEW_UNREAD']}` DESC
- LIMIT 1", array('id' => $iLotId));
- $iTotal = $this->getOne("SELECT COUNT(*)
- FROM `{$this->CNF['TABLE_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_MESSAGE_FK']}`=:id
- ", array('id' => $iLotId));
- return array('unread' => $iUnread, 'read' => ($iTotal - $iUnread));
- }
- /**
- * Get lot id by jot id
- *@param int $iJotId jot id
- *@param boolean bIdOnly return id or array with info
- *@return mixed lot id or lot info in array
- */
- public function getLotByJotId($iJotId, $bIdOnly = true)
- {
- $iLotId = $this->getOne("SELECT `{$this->CNF['FIELD_MESSAGE_FK']}`
- FROM `{$this->CNF['TABLE_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_MESSAGE_ID']}` = :jot LIMIT 1", array('jot' => $iJotId));
- if (!$iLotId)
- return false;
- return $bIdOnly ? $iLotId : $this->getRow("SELECT * FROM `{$this->CNF['TABLE_ENTRIES']}`
- WHERE `{$this->CNF['FIELD_ID']}` = :lot LIMIT 1", array('lot' => $iLotId));
- }
- public function getLotByJotPairs(&$aJotsList)
- {
- $aJotsList = array_map('intval', $aJotsList);
- return !empty($aJotsList) ? $this->getAll("SELECT `{$this->CNF['FIELD_MESSAGE_FK']}`, `{$this->CNF['FIELD_MESSAGE_ID']}`
- FROM `{$this->CNF['TABLE_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_MESSAGE_ID']}` IN (" . implode(',', $aJotsList) . ")") : false;
- }
- /**
- * Get lot id by parent id
- *@param int $iJotId jot id
- *@return mixed lot id or lot info in array
- */
- public function getLotByParentId($iParentId)
- {
- return $this->getRow("SELECT * FROM `{$this->CNF['TABLE_ENTRIES']}`
- WHERE `{$this->CNF['FIELD_PARENT_JOT']}`=:id LIMIT 1", array('id' => $iParentId));
- }
- /**
- * Get the latest posted jot(message)
- *@param int $iLotId lot id
- *@param int $iProfileId if not specified then just latest jot of any member
- *@param boolean $bNotTrash don't show trash message
- *@return array with jot info
- */
- public function getLatestJot($iLotId, $iProfileId = 0, $bNotTrash = true)
- {
- $sWhere = '';
- $aWhere['lot'] = $iLotId;
- if ($iProfileId) {
- $sWhere = " AND `{$this->CNF['FIELD_MESSAGE_AUTHOR']}` = :profile";
- $aWhere['profile'] = $iProfileId;
- }
- if ($bNotTrash)
- $sWhere .= " AND `{$this->CNF['FIELD_MESSAGE_TRASH']}` = 0";
- return $this->getRow("SELECT *
- FROM `{$this->CNF['TABLE_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_MESSAGE_FK']}` = :lot {$sWhere}
- ORDER BY `{$this->CNF['FIELD_MESSAGE_ID']}` DESC
- LIMIT 1", $aWhere);
- }
- public function getLatestJotsAuthors($iLotId, $iExcludeProfileId = 0, $iLimit = 3)
- {
- $sWhere = '';
- $aWhere = ['lot' => $iLotId, 'limit' => $iLimit];
- if ($iExcludeProfileId) {
- $sWhere = " AND `{$this->CNF['FIELD_MESSAGE_ID']}` != :profile";
- $aWhere['profile'] = (int) $iExcludeProfileId;
- }
- return $this->getPairs("SELECT `{$this->CNF['FIELD_MESSAGE_AUTHOR']}`, MAX(`{$this->CNF['FIELD_MESSAGE_ADDED']}`) as `{$this->CNF['FIELD_MESSAGE_ADDED']}`
- FROM `{$this->CNF['TABLE_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_MESSAGE_FK']}` = :lot {$sWhere}
- GROUP BY `{$this->CNF['FIELD_MESSAGE_AUTHOR']}`
- ORDER BY `{$this->CNF['FIELD_MESSAGE_ADDED']}` DESC
- LIMIT :limit", $this->CNF['FIELD_MESSAGE_ADDED'], $this->CNF['FIELD_MESSAGE_AUTHOR'], $aWhere);
- }
- /**
- * Get jot(message) by id
- *@param int $iJotId id
- *@return array with jot info
- */
- public function getJotById($iJotId)
- {
- $sQuery = $this->prepare("SELECT * FROM `{$this->CNF['TABLE_MESSAGES']}` WHERE `{$this->CNF['FIELD_MESSAGE_ID']}` = ?", $iJotId);
- return $this->getRow($sQuery);
- }
- /**
- * Get messages number between two messages
- * @param $iLotId
- * @param $iJotStart
- * @param $iJotEnd
- * @return array with jot info
- */
- public function getJotsNumber($iLotId, $iJotStart, $iJotEnd = 0)
- {
- $aWhere = array('id' => $iLotId, 'start' => $iJotStart);
- $sWhere = "`{$this->CNF['FIELD_MESSAGE_ID']}` > :start";
- if ($iJotEnd) {
- $sWhere = "`{$this->CNF['FIELD_MESSAGE_ID']}` BETWEEN :start AND :end";
- $aWhere['end'] = $iJotEnd;
- }
- return $this->getOne("SELECT COUNT(*) FROM `{$this->CNF['TABLE_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_MESSAGE_FK']}`=:id AND {$sWhere}
- ORDER BY `{$this->CNF['FIELD_MESSAGE_ID']}` ASC
- ", $aWhere);
- }
- /**
- * Get all pages with comments block
- *@return array list with pages
- */
- public function getPagesWithComments()
- {
- return $this->getPairs("SELECT * FROM `sys_objects_cmts`", 'Name', 'BaseUrl');
- }
- /**
- * Check if the page already contains messenger block
- *@param $sPage page name
- *@return boolean
- */
- public function isBlockAdded($sPage)
- {
- $sPage = bx_process_input($sPage);
- $sQuery = $this->prepare("SELECT COUNT(*) FROM `sys_pages_blocks` WHERE `object` = ? AND `title` = '_bx_messenger_page_block_title_messenger'", $sPage);
- return $this->getOne($sQuery) == 1;
- }
- /**
- * Add messenger block to the page
- *@param $sPage page name
- *@return affected rows
- */
- public function addMessengerBlock($sPage)
- {
- $sPage = bx_process_input($sPage);
- $aInfo = $this->findCommentsBlock($sPage);
- if (!empty($aInfo))
- return $this->query("INSERT INTO `sys_pages_blocks` (`object`, `cell_id`, `module`, `title`, `designbox_id`, `visible_for_levels`, `type`, `content`, `deletable`, `copyable`, `order`, `active`) VALUES
- (:page, :cell, 'bx_messenger', '_bx_messenger_page_block_title_messenger', 0, 2147483647, 'service', 'a:3:{s:6:\"module\";s:12:\"bx_messenger\";s:6:\"method\";s:19:\"get_block_messenger\";s:6:\"params\";a:1:{i:0;s:6:\"{type}\";}}', 0, 0, :order, 0)",
- array('page' => $sPage, 'cell' => $aInfo['cell_id'], 'order' => $aInfo['order'] + 1)
- );
- return $this->query("INSERT INTO `sys_pages_blocks` (`object`, `cell_id`, `module`, `title`, `designbox_id`, `visible_for_levels`, `type`, `content`, `deletable`, `copyable`, `order`, `active`) VALUES
- (:page, 1, 'bx_messenger', '_bx_messenger_page_block_title_messenger', 0, 2147483647, 'service', 'a:3:{s:6:\"module\";s:12:\"bx_messenger\";s:6:\"method\";s:19:\"get_block_messenger\";s:6:\"params\";a:1:{i:0;s:6:\"{type}\";}}', 0, 0, 0, 0)",
- array('page' => $sPage)
- );
- }
- /**
- * Find comments block on the page
- *@param $sPage page name
- *@return array block info
- */
- private function findCommentsBlock($sPage)
- {
- $sPage = bx_process_input($sPage);
- $sQuery = $this->prepare("SELECT * FROM `sys_pages_blocks` WHERE `object` = ? AND `title` LIKE '%comments%'", $sPage);
- return $this->getRow($sQuery);
- }
- public function getUnreadJotsMessagesCount($iProfileId, $iLotId, $iParentId = 0)
- {
- if (!(int) $iLotId)
- return false;
- return $this->getNewJots($iProfileId, $iLotId, true);
- }
- function getLotsWithUnreadMessages($iProfileId)
- {
- if (!$iProfileId)
- return false;
- return $this->getPairs("SELECT `{$this->CNF['FIELD_NEW_UNREAD']}`, `{$this->CNF['FIELD_NEW_LOT']}`
- FROM `{$this->CNF['TABLE_NEW_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_NEW_PROFILE']}`=:profile AND `{$this->CNF['FIELD_NEW_UNREAD']}` > 0
- ", $this->CNF['FIELD_NEW_LOT'], $this->CNF['FIELD_NEW_UNREAD'], array('profile' => $iProfileId));
- }
- function getNewJots($iProfileId, $iLotId, $bCount = false)
- {
- $aInfo = $this->getRow("SELECT *
- FROM `{$this->CNF['TABLE_NEW_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_NEW_PROFILE']}`=:profile AND `{$this->CNF['FIELD_NEW_LOT']}`=:lot
- ", array('lot' => $iLotId, 'profile' => $iProfileId));
- if (empty($aInfo))
- return 0;
- return $bCount ? (int) $aInfo[$this->CNF['FIELD_NEW_UNREAD']] : $aInfo;
- }
- function getForWhomJotIsNew($iLotId, $iJotId)
- {
- if (!$iLotId && !$iJotId)
- return [];
- return $this->getColumn("SELECT `{$this->CNF['FIELD_NEW_PROFILE']}`
- FROM `{$this->CNF['TABLE_NEW_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_NEW_JOT']}` <=:jot AND `{$this->CNF['FIELD_NEW_LOT']}`=:lot
- ", array('jot' => $iJotId, 'lot' => $iLotId));
- }
- private function addNewJotItem($iProfileId, $iLotId, $iJotId)
- {
- if (!(int) $iProfileId)
- return false;
- $iJotNumber = 0;
- $iLastJot = $iJotId;
- $aJotInfo = $this->getNewJots($iProfileId, $iLotId);
- if (!empty($aJotInfo)) {
- $iJotNumber = (int) $aJotInfo[$this->CNF['FIELD_NEW_UNREAD']];
- $iLastJot = (int) $aJotInfo[$this->CNF['FIELD_NEW_JOT']];
- }
- return $this->query("REPLACE INTO `{$this->CNF['TABLE_NEW_MESSAGES']}`
- SET
- `{$this->CNF['FIELD_NEW_PROFILE']}`=:profile,
- `{$this->CNF['FIELD_NEW_UNREAD']}`=:number,
- `{$this->CNF['FIELD_NEW_LOT']}`=:lot,
- `{$this->CNF['FIELD_NEW_JOT']}`=:jot
- ", array(
- 'profile' => $iProfileId,
- 'lot' => $iLotId,
- 'number' => $iJotNumber + 1,
- 'jot' => $iLastJot
- ));
- }
- private function addNewJotItems($aProfileIds, $iLotId, $iJotId)
- {
- $aProfileIds = array_values(
- array_unique(
- array_filter($aProfileIds, fn($iProfileId) => $iProfileId > 0)
- )
- );
- if (empty($aProfileIds))
- return false;
- $bExecuted = false;
- foreach (array_chunk($aProfileIds, 200) as $aChunk) {
- $aValues = array_map(function ($iProfileId) use ($iLotId, $iJotId) {
- return sprintf("(%d, %d, 1, %d)", (int) $iLotId, (int) $iJotId, (int) $iProfileId);
- }, $aChunk);
- $bExecuted |= (bool) $this->query("
- INSERT INTO `{$this->CNF['TABLE_NEW_MESSAGES']}`
- (`{$this->CNF['FIELD_NEW_LOT']}`, `{$this->CNF['FIELD_NEW_JOT']}`, `{$this->CNF['FIELD_NEW_UNREAD']}`, `{$this->CNF['FIELD_NEW_PROFILE']}`)
- VALUES " . implode(',', $aValues) . "
- ON DUPLICATE KEY UPDATE
- `{$this->CNF['FIELD_NEW_UNREAD']}` = `{$this->CNF['FIELD_NEW_UNREAD']}` + 1,
- `{$this->CNF['FIELD_NEW_JOT']}` = IF(
- `{$this->CNF['FIELD_NEW_JOT']}` = 0 OR `{$this->CNF['FIELD_NEW_JOT']}` > VALUES(`{$this->CNF['FIELD_NEW_JOT']}`),
- VALUES(`{$this->CNF['FIELD_NEW_JOT']}`),
- `{$this->CNF['FIELD_NEW_JOT']}`
- )
- ");
- }
- return $bExecuted;
- }
- function markAsNewJot($mixedProfile, $iLotId, $iJotId, $bUpdateLot = true)
- {
- if (is_array($mixedProfile))
- $this->addNewJotItems($mixedProfile, $iLotId, $iJotId);
- elseif ((int) $mixedProfile)
- $this->addNewJotItem($mixedProfile, $iLotId, $iJotId);
- if (!$bUpdateLot)
- return true;
- return $this->query("UPDATE `{$this->CNF['TABLE_ENTRIES']}`
- SET `{$this->CNF['FIELD_UPDATED']}` = UNIX_TIMESTAMP()
- WHERE `{$this->CNF['FIELD_ID']}` = :id", array('id' => $iLotId));
- }
- function deleteNewJot($iProfileId, $iLotId = 0, $iJotId = 0)
- {
- if ($iJotId) {
- $iLotId = $iLotId ? $iLotId : $this->getLotByJotId($iJotId);
- $aNewJotInfo = $this->getNewJots($iProfileId, $iLotId);
- if (empty($aNewJotInfo) || ((int) $aNewJotInfo[$this->CNF['FIELD_NEW_JOT']] > (int) $iJotId))
- return false;
- $iCount = (int) $this->getOne("SELECT COUNT(*) FROM `{$this->CNF['TABLE_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_MESSAGE_FK']}`=:lot AND `{$this->CNF['FIELD_MESSAGE_ID']}` > :jot
- ", array('lot' => $iLotId, 'jot' => $iJotId));
- if ($iCount)
- return $this->query("REPLACE INTO `{$this->CNF['TABLE_NEW_MESSAGES']}`
- SET
- `{$this->CNF['FIELD_NEW_PROFILE']}`=:profile,
- `{$this->CNF['FIELD_NEW_UNREAD']}`=:number,
- `{$this->CNF['FIELD_NEW_JOT']}`=:jot,
- `{$this->CNF['FIELD_NEW_LOT']}`=:lot
- ", array(
- 'profile' => $iProfileId,
- 'lot' => $iLotId,
- 'number' => $iCount,
- 'jot' => $iJotId
- ));
- }
- return $iLotId ?
- $this->query("DELETE FROM `{$this->CNF['TABLE_NEW_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_NEW_LOT']}`=:lot AND `{$this->CNF['FIELD_NEW_PROFILE']}`=:profile
- ", array('profile' => $iProfileId, 'lot' => $iLotId))
- :
- $this->query("DELETE FROM `{$this->CNF['TABLE_NEW_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_NEW_PROFILE']}`=:profile
- ", array('profile' => $iProfileId));
- }
- public function searchMessage($sParam, $iProfileId = 0, $iLotId = 0)
- {
- $aResult = array();
- if (!$sParam)
- return $aResult;
- $aParams = preg_split('/[\s]/', $sParam, -1, PREG_SPLIT_NO_EMPTY);
- $sCriteria = implode('%', $aParams);
- $aWhere = array('criteria' => "%{$sCriteria}%");
- if ($iLotId) {
- $sWhere = " AND `j`.`{$this->CNF['FIELD_MESSAGE_FK']}`=:id";
- $aWhere['id'] = (int) $iLotId;
- }
- if ($iProfileId) {
- $sWhere = " AND (`l`.`{$this->CNF['FIELD_PARTICIPANTS']}` REGEXP '(^|,){$iProfileId}(,|$)' OR `l`.`{$this->CNF['FIELD_AUTHOR']}`=:profile)";
- $aWhere['profile'] = (int) $iProfileId;
- }
- return $this->getPairs("SELECT `j`.`{$this->CNF['FIELD_MESSAGE_ID']}`, `j`.`{$this->CNF['FIELD_MESSAGE_FK']}`
- FROM `{$this->CNF['TABLE_MESSAGES']}` as `j`
- RIGHT JOIN `{$this->CNF['TABLE_ENTRIES']}` as `l` on `l`.`{$this->CNF['FIELD_ID']}` = `j`.`{$this->CNF['FIELD_MESSAGE_FK']}`
- WHERE `j`.`{$this->CNF['FIELD_MESSAGE']}` LIKE :criteria {$sWhere}", $this->CNF['FIELD_MESSAGE_ID'], $this->CNF['FIELD_MESSAGE_FK'], $aWhere);
- }
- function getMyTalksIdList($iProfileId)
- {
- if (!$iProfileId)
- return [];
- $sField = $this->CNF['FIELD_ID'];
- $aList = $this->getMyLots($iProfileId);
- return array_map(function ($aTalk) use ($sField) {
- return $aTalk[$sField];
- }, $aList);
- }
- /**
- * Get all member's lots
- *@param int $iProfileId
- *@param array $aParams filter params
- *@param array $aReturn may contain special values for search function
- *@return array list of lots
- */
- public function getMyLots($iProfileId, $aParams = [], &$aReturn = [])
- {
- $sOrAddon = $sJoin = $sWhere = '';
- $aSWhere = array();
- $aWhere = array('profile' => (int) $iProfileId, 'parts' => '(^|,)' . (int) $iProfileId . '(,|$)');
- // talks groups
- if (isset($aParams['group']) && (int) $aParams['group']) {
- $aSWhere[] = " `g`.`{$this->CNF['FMGL_GROUP_ID']}`=:group_id ";
- $aWhere['group_id'] = (int) $aParams['group'];
- $sOrAddon = " OR `l`.`{$this->CNF['FIELD_TYPE']}` = " . BX_IM_TYPE_PUBLIC;
- }
- //if threads
- if (isset($aParams['threads']))
- $aSWhere[] = " `l`.`{$this->CNF['FIELD_PARENT_JOT']}` !=0 ";
- // Search criteria: the caller's (API) or the ones selected in the module settings.
- $aCriteria = isset($aParams['search_criteria']) && is_array($aParams['search_criteria']) ? $aParams['search_criteria'] : null;
- $fIsCriteria = function($sCriteria) use ($aCriteria) {
- return $aCriteria !== null ? in_array($sCriteria, $aCriteria) : $this->_oConfig->isSearchCriteria($sCriteria);
- };
- $sParam = isset($aParams['term']) && $aParams['term'] ? $aParams['term'] : '';
- if ($sParam) {
- $aParamWhere = [];
- if ($fIsCriteria(BX_SEARCH_CRITERIA_TITLES)) {
- $aParamWhere[] = "`l`.`{$this->CNF['FIELD_TITLE']}` LIKE :title";
- $aWhere['title'] = "%{$sParam}%";
- }
- if ($fIsCriteria(BX_SEARCH_CRITERIA_PARTS)) {
- $aProfiles = BxDolService::call('system', 'profiles_search', array($sParam), 'TemplServiceProfiles');
- if (!empty($aProfiles)) {
- $aRegexp = array();
- foreach ($aProfiles as &$aProfile) {
- // Site: 'value' holds the profile id; API: profiles come as unit data with 'id'.
- $iPartId = (int)(isset($aProfile['value']) ? $aProfile['value'] : (isset($aProfile['id']) ? $aProfile['id'] : 0));
- if ($iPartId) {
- $aRegexp[] = "(^|,){$iPartId}(,|$)";
- }
- }
- if (!empty($aRegexp)) {
- $aWhere['part_search'] = implode('|', $aRegexp);
- $aParamWhere[] = "`{$this->CNF['FIELD_PARTICIPANTS']}` REGEXP :part_search";
- }
- }
- }
- if ($fIsCriteria(BX_SEARCH_CRITERIA_CONTENT)) {
- $aSelectedLots = $this->searchMessage($sParam, $iProfileId);
- if (!empty($aSelectedLots)) {
- $aParamWhere[] = "`l`.`{$this->CNF['FIELD_ID']}` IN (" . implode(',', array_unique($aSelectedLots)) . ")";
- $aReturn['jots_list'] = $aSelectedLots;
- }
- }
- if (!empty($aParamWhere))
- $aSWhere[] = '(' . implode(' OR ', $aParamWhere) . ')';
- else
- return false;
- }
- if (isset($aParams['type']) && $aParams['type']) {
- if (is_numeric($aParams['type'])) {
- $aSWhere[] = " `l`.`{$this->CNF['FIELD_TYPE']}` = :type AND `l`.`{$this->CNF['FIELD_PARENT_JOT']}` = 0 ";
- $aWhere['type'] = (int) $aParams['type'];
- } else if (is_array($aParams['type'])) {
- $aSWhere[] = " `l`.`{$this->CNF['FIELD_TYPE']}` IN (" . implode(',', $aParams['type']) . ") AND `l`.`{$this->CNF['FIELD_PARENT_JOT']}` = 0 ";
- }
- } else {
- $sJoin .= "LEFT JOIN `{$this->CNF['TABLE_MASS_TRACKER']}` as `mt` ON `mt`.`{$this->CNF['FIELD_MASS_CONVO_ID']}` = `l`.`{$this->CNF['FIELD_ID']}` AND `mt`.`{$this->CNF['FIELD_MASS_USER_ID']}`=:profile";
- $sOrAddon = " OR `mt`.`{$this->CNF['FIELD_MASS_USER_ID']}` IS NOT NULL";
- }
- $bStar = isset($aParams['star']) && $aParams['star'] === true;
- if ($bStar) {
- $sJoin .= " INNER JOIN `{$this->CNF['TABLE_USERS_INFO']}` as `u` ON `u`.`{$this->CNF['FIELD_INFO_LOT_ID']}` = `l`.`{$this->CNF['FIELD_ID']}` AND `u`.`{$this->CNF['FIELD_INFO_USER_ID']}`=:profile";
- $aSWhere[] = "`u`.`{$this->CNF['FIELD_INFO_STAR']}` = 1";
- }
- if (isset($aParams['visibility'])) {
- if ($aParams['visibility'] !== BX_MSG_VISIBILITY_ALL) {
- $aWhere['visibility'] = $aParams['visibility'];
- $aSWhere[] = " `l`.`{$this->CNF['FIELD_VISIBILITY']}` = :visibility";
- }
- } else {
- $aSWhere[] = " `l`.`{$this->CNF['FIELD_VISIBILITY']}` = :visibility";
- $aWhere['visibility'] = BX_MSG_VISIBILITY_VISIBLE;
- }
- $sFields = $sHaving = '';
- $iLastLot = isset($aParams['last_lot']) ? (int) $aParams['last_lot'] : 0;
- if ($iLastLot) {
- $aWhere['last_lot'] = $iLastLot;
- $aSWhere[] = "`l`.`{$this->CNF['FIELD_ID']}` < :last_lot";
- }
- $iLot = isset($aParams['lot']) && (int) $aParams['lot'] ? (int) $aParams['lot'] : 0;
- if ($iLot) {
- $aWhere['lot'] = $iLot;
- $aSWhere[] = "`l`.`{$this->CNF['FIELD_ID']}` = :lot";
- }
- if (!empty($aSWhere))
- $sWhere = ' AND ' . implode(' AND ', $aSWhere);
- $aWhere['start'] = isset($aParams['start']) ? (int) $aParams['start'] : 0;
- $aWhere['per_page'] = isset($aParams['per_page']) ? (int) $aParams['per_page'] : (int) $this->CNF['MAX_LOTS_NUMBER'];
- $sLimit = "LIMIT :start, :per_page";
- $bShowLeft = isset($aParams['left']);
- $sLeft = "";
- if ($bShowLeft)
- $sLeft = "SQL_CALC_FOUND_ROWS";
- $aResult = $this->getAll("SELECT
- {$sLeft}
- {$sFields}
- `l`.*,
- IF (`g`.`{$this->CNF['FMGL_GROUP_ID']}` IS NOT NULL, `g`.`{$this->CNF['FMGL_GROUP_ID']}`, 0) as `{$this->CNF['FMGL_GROUP_ID']}`,
- IF (`l`.`{$this->CNF['FIELD_UPDATED']}` = 0, `l`.`{$this->CNF['FIELD_ADDED']}`, `l`.`{$this->CNF['FIELD_UPDATED']}`) as `order`
- FROM `{$this->CNF['TABLE_ENTRIES']}` as `l`
- LEFT JOIN `{$this->CNF['TABLE_GROUPS_LOTS']}` as `g` ON `g`.`{$this->CNF['FMGL_LOT_ID']}` = `l`.`{$this->CNF['FIELD_ID']}`
- {$sJoin}
- WHERE (`l`.`{$this->CNF['FIELD_PARTICIPANTS']}` REGEXP :parts OR `l`.`{$this->CNF['FIELD_AUTHOR']}`=:profile {$sOrAddon}) {$sWhere}
- {$sHaving}
- ORDER BY `order` DESC
- {$sLimit}", $aWhere);
- if ($bShowLeft)
- $aReturn['left'] = (int) $this->getOne("SELECT FOUND_ROWS()") - $aWhere['start'];
- return $aResult;
- }
- /**
- * Check if the title of the lot type must contain link
- *@param int $iType
- *@return boolean
- */
- public function isLinkedTitle($iType)
- {
- $sQuery = $this->prepare("SELECT `{$this->CNF['FIELD_TYPE_LINKED']}` FROM `{$this->CNF['TABLE_TYPES']}` WHERE `{$this->CNF['FIELD_TYPE_ID']}` = ? LIMIT 1", $iType);
- return (int) $this->getOne($sQuery) == 1;
- }
- /**
- * Get time when member was online
- *@param int $iProfileId
- *@return boolean
- */
- public function lastOnline($iProfileId)
- {
- $sSql = $this->prepare("SELECT
- IF (`ts`.`date` != '', `ts`.`date`, `ta`.`logged`) as `logged`
- FROM `sys_profiles` AS `tp`
- INNER JOIN `sys_accounts` AS `ta` ON `tp`.`account_id`=`ta`.`id`
- LEFT JOIN `sys_sessions` AS `ts` ON `tp`.`account_id`=`ts`.`user_id`
- WHERE
- `tp`.`id` = ? AND
- `ta`.`profile_id`=`tp`.`id`
- LIMIT 1", $iProfileId);
- return $this->getOne($sSql);
- }
- /**
- * Delete all profiles info from lots and jots
- *@param int $iProfileId
- *@param bool $bTalks if true to remove the talk where removed user participates
- *@return int affected rows
- */
- public function deleteProfileInfo($iProfileId, $bTalks = false)
- {
- $bResult = true;
- $aWhere['profile'] = (int) $iProfileId;
- $bResult &= $this->query("DELETE
- FROM `{$this->CNF['TABLE_ENTRIES']}`
- WHERE `{$this->CNF['FIELD_AUTHOR']}`=:profile", $aWhere);
- $aJots = $this->getAll("SELECT *
- FROM `{$this->CNF['TABLE_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_MESSAGE_AUTHOR']}`=:profile", $aWhere);
- foreach ($aJots as &$aJot)
- $this->clearJotsConnections($aJot[$this->CNF['FIELD_MESSAGE_ID']], $aJot[$this->CNF['FIELD_MESSAGE_FK']]);
- $bResult &= $this->query("DELETE
- FROM `{$this->CNF['TABLE_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_MESSAGE_AUTHOR']}`=:profile", $aWhere);
- $this->query("DELETE FROM `{$this->CNF['TABLE_MASS_TRACKER']}` WHERE `{$this->CNF['FIELD_MASS_USER_ID']}` =:profile_id", ['profile_id' => $iProfileId]);
- if ($bTalks)
- $bResult &= (bool) $this->query("DELETE
- FROM `{$this->CNF['TABLE_ENTRIES']}`
- WHERE FIND_IN_SET(:profile, `{$this->CNF['FIELD_PARTICIPANTS']}`) AND `type`=:type", array_merge($aWhere, ['type' => BX_IM_TYPE_PRIVATE]));
- $aLots = $this->getAll("SELECT *
- FROM `{$this->CNF['TABLE_ENTRIES']}`
- WHERE FIND_IN_SET(:profile, `{$this->CNF['FIELD_PARTICIPANTS']}`)", $aWhere);
- if (empty($aLots))
- return $bResult;
- foreach ($aLots as &$aLot) {
- $bResult &= $this->removeParticipant($aLot[$this->CNF['FIELD_ID']], $iProfileId);
- }
- return $bResult;
- }
- /**
- * Add attachment to the jot
- *@param int $iJotId jot id
- *@param text mixedContent attachment content
- *@param int sType attachment type
- *@return int affected rows
- */
- public function addAttachment($iJotId, $aAttachments, $mixedType = false)
- {
- $iJotId = (int) $iJotId;
- if (!$iJotId || !($aJotInfo = $this->getJotById($iJotId)) || empty($aAttachments))
- return false;
- if ($mixedType !== false) {
- if (empty($aJotInfo[$this->CNF['FIELD_MESSAGE_AT']]))
- $aAttachments = array($mixedType => $aAttachments);
- else
- $aAttachments = @unserialize($aJotInfo[$this->CNF['FIELD_MESSAGE_AT']]) + array($mixedType => $aAttachments);
- }
- $sQuery = $this->prepare("UPDATE `{$this->CNF['TABLE_MESSAGES']}`
- SET
- `{$this->CNF['FIELD_MESSAGE_AT']}` = ?
- WHERE `{$this->CNF['FIELD_MESSAGE_ID']}` = ?", @serialize($aAttachments), $iJotId);
- return $this->query($sQuery);
- }
- /**
- * Add attachment to the jot
- *@param int $iJotId jot id
- *@param int $iProfileId profile id
- *@param string $sMessage
- *@return int affected rows
- */
- public function editJot($iJotId, $iProfileId, $sMessage)
- {
- $iJotId = (int) $iJotId;
- $aJotInfo = array();
- if (!$iJotId || !($aJotInfo = $this->getJotById($iJotId)) || !$sMessage)
- return false;
- $sWhere = '';
- if ($aJotInfo[$this->CNF['FIELD_MESSAGE_AT_TYPE']] == BX_ATT_TYPE_REPOST)
- $sWhere = ",`{$this->CNF['FIELD_MESSAGE_AT_TYPE']}` = '', `{$this->CNF['FIELD_MESSAGE_AT']}` = ''";
- $sMessage = clear_xss($sMessage);
- $sQuery = $this->prepare("UPDATE `{$this->CNF['TABLE_MESSAGES']}`
- SET `{$this->CNF['FIELD_MESSAGE']}` = ?,
- `{$this->CNF['FIELD_MESSAGE_LAST_EDIT']}` = UNIX_TIMESTAMP(),
- `{$this->CNF['FIELD_MESSAGE_EDIT_BY']}` = ?
- {$sWhere}
- WHERE `{$this->CNF['FIELD_MESSAGE_ID']}` = ?", $sMessage, $iProfileId, $iJotId);
- return $this->query($sQuery);
- }
- /**
- * Check if the Jot already has attachment
- *@param int $iJotId jot id
- *@param string $sType attachment type
- *@return int original attachment Id
- */
- public function hasAttachment($iJotId, $sType = BX_ATT_TYPE_REPOST)
- {
- $aValues = $this->getRow("SELECT
- `{$this->CNF['FIELD_MESSAGE_AT']}`,
- `{$this->CNF['FIELD_MESSAGE_AT_TYPE']}`
- FROM `{$this->CNF['TABLE_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_MESSAGE_ID']}` = :id", array('id' => $iJotId));
- if (!isset($aValues[$this->CNF['FIELD_MESSAGE_AT']]))
- return false;
- if ($aValues[$this->CNF['FIELD_MESSAGE_AT_TYPE']]) {
- if ($aValues[$this->CNF['FIELD_MESSAGE_AT_TYPE']] == $sType && (int) $aValues[$this->CNF['FIELD_MESSAGE_AT']] && $aValues[$this->CNF['FIELD_MESSAGE_AT']] !== $iJotId)
- return (int) $aValues[$this->CNF['FIELD_MESSAGE_AT']];
- return false;
- }
- $mixedResult = @unserialize($aValues[$this->CNF['FIELD_MESSAGE_AT']]);
- if (is_array($mixedResult)) {
- if (isset($mixedResult[$sType]) && (int) $mixedResult[$sType] && (int) $mixedResult[$sType] != $iJotId)
- return (int) $mixedResult[$sType];
- }
- return false;
- }
- public function updateFiles($iJotId, $mixedValues)
- {
- $sQuery = $this->prepare("UPDATE `{$this->CNF['OBJECT_STORAGE']}` SET " . $this->arrayToSQL($mixedValues) . " WHERE `{$this->CNF['FIELD_ST_ID']}` = ?", $iJotId);
- return $this->query($sQuery);
- }
- public function getJotFiles($iJot, $bCount = false, $iProfileId = 0)
- {
- $sProfileWhere = '';
- if ((int) $iProfileId)
- $sProfileWhere = "AND `t`.`{$this->CNF['FJMT_USER_ID']}`=" . (int) $iProfileId;
- return !$bCount ?
- $this->getAll("SELECT `f`.*, IF (`t`.`{$this->CNF['FJMT_COLLAPSED']}` IS NULL, 0, 1) as `{$this->CNF['FJMT_COLLAPSED']}`
- FROM `{$this->CNF['OBJECT_STORAGE']}` as `f`
- LEFT JOIN `{$this->CNF['TABLE_JOTS_MEDIA_TRACKER']}` as `t` ON `f`.`{$this->CNF['FIELD_ST_ID']}`=`t`.`{$this->CNF['FJMT_FILE_ID']}` {$sProfileWhere}
- WHERE `{$this->CNF['FIELD_ST_JOT']}` = :id", array('id' => $iJot)) :
- $this->getOne("SELECT COUNT(*) FROM `{$this->CNF['OBJECT_STORAGE']}` WHERE `{$this->CNF['FIELD_ST_JOT']}` = :id", array('id' => $iJot));
- }
- private function removeFilesByJotId($iJotId)
- {
- $aFiles = $this->getJotFiles($iJotId);
- if (empty($aFiles))
- return false;
- $oStorage = BxDolStorage::getObjectInstance($this->CNF['OBJECT_STORAGE']);
- $bResult = true;
- foreach ($aFiles as &$aFile) {
- $bResult &= $oStorage->deleteFile($aFile[$this->CNF['FIELD_ST_ID']], $aFile[$this->CNF['FIELD_ST_AUTHOR']]);
- $this->removeMediaTracker($aFile[$this->CNF['FIELD_ST_ID']]);
- }
- return $bResult;
- }
- public function removeFileFromJot($iJotId, $iFileId)
- {
- $aJotInfo = $this->getJotById($iJotId);
- $aAttachment = $aFiles = [];
- if ($aJotInfo[$this->CNF['FIELD_MESSAGE_AT_TYPE']] === BX_ATT_TYPE_FILES) {
- $aFiles = explode(',', $aJotInfo[$this->CNF['FIELD_MESSAGE_AT']]);
- } else
- if (!$aJotInfo[$this->CNF['FIELD_MESSAGE_AT_TYPE']] && isset($aJotInfo[$this->CNF['FIELD_MESSAGE_AT']])) {
- $aAttachment = @unserialize($aJotInfo[$this->CNF['FIELD_MESSAGE_AT']]);
- if (isset($aAttachment[BX_ATT_TYPE_FILES])) {
- $mixedFiles = $aAttachment[BX_ATT_TYPE_FILES];
- if (is_string($mixedFiles))
- $aFiles = explode(',', $mixedFiles);
- else if (is_array($mixedFiles))
- $aFiles = $mixedFiles;
- } else
- return false;
- }
- $oStorage = BxDolStorage::getObjectInstance($this->CNF['OBJECT_STORAGE']);
- $aFile = $oStorage->getFile($iFileId);
- $sFileName = $aFile[$this->CNF['FIELD_ST_NAME']];
- if (!empty($aFiles))
- $aFiles = array_filter($aFiles, function ($sValue) use ($sFileName) {
- return $sValue !== $sFileName;
- });
- else
- return false;
- $aAttachment[BX_ATT_TYPE_FILES] = $aFiles;
- $sQuery = $this->prepare("UPDATE `{$this->CNF['TABLE_MESSAGES']}`
- SET
- `{$this->CNF['FIELD_MESSAGE_AT_TYPE']}` = '',
- `{$this->CNF['FIELD_MESSAGE_AT']}` = ?
- WHERE `{$this->CNF['FIELD_MESSAGE_ID']}` = ?", @serialize($aAttachment), $iJotId);
- return $this->query($sQuery);
- }
- public function isFileVendor($iFileId, $iProfileId)
- {
- $oStorage = BxDolStorage::getObjectInstance($this->CNF['OBJECT_STORAGE']);
- $aFile = $oStorage->getFile($iFileId);
- return $aFile[$this->CNF['FIELD_ST_AUTHOR']] == $iProfileId;
- }
- public function isEmptyJotMessage($iJotId)
- {
- $aJotInfo = $this->getJotById($iJotId);
- return empty($aJotInfo) || !$aJotInfo['FIELD_MESSAGE'];
- }
- public function getFirstUnreadJot($iProfileId, $iLotId)
- {
- if (!$iLotId)
- return 0;
- $aUnreadJots = $this->getNewJots($iProfileId, $iLotId);
- return !empty($aUnreadJots) ? (int) $aUnreadJots[$this->CNF['FIELD_NEW_JOT']] : 0;
- }
- /**
- * @param int $iObjectId item's object ID
- * @param string $sType module's name from sys_modules
- * @param int $iProfileId comments' author
- * @param int $iStart get comments from position
- * @param int $iPerPage number of the comments to get at once
- * @return array list of the comments
- *
- */
- public function getLiveComments($iObjectId, $sType, $iProfileId, $iStart = 0, $iPerPage = 0)
- {
- $aWhere = array('object' => $iObjectId, 'type' => $sType);
- $sLimit = $sAuthor = '';
- if ((int) $iProfileId) {
- $aWhere['author'] = (int) $iProfileId;
- $sAuthor = "AND `{$this->CNF['FIELD_LCMTS_AUTHOR']}`=:author";
- }
- if ($iPerPage) {
- $aWhere['start'] = (int) $iStart;
- $aWhere['per_page'] = (int) $iPerPage;
- $sLimit = "LIMIT :start, :per_page";
- }
- $aComments = $this->getAll(
- "SELECT
- SQL_CALC_FOUND_ROWS
- `{$this->CNF['FIELD_LCMTS_ID']}`,
- `{$this->CNF['FIELD_LCMTS_TEXT']}`,
- `{$this->CNF['FIELD_LCMTS_AUTHOR']}`,
- `{$this->CNF['FIELD_LCMTS_DATE']}`,
- `{$this->CNF['FIELD_LCMTS_OBJECT_ID']}`
- FROM `{$this->CNF['TABLE_LIVE_COMMENTS']}` as `c`
- LEFT JOIN `{$this->CNF['TABLE_CMTS_OBJECTS']}` as `s` ON `s`.`{$this->CNF['FIELD_COBJ_ID']}` = `c`.`{$this->CNF['FIELD_LCMTS_SYS_ID']}`
- WHERE `s`.`{$this->CNF['FIELD_COBJ_MODULE']}` = :type AND `{$this->CNF['FIELD_LCMTS_OBJECT_ID']}` = :object {$sAuthor}
- ORDER BY `{$this->CNF['FIELD_LCMTS_ID']}` DESC
- {$sLimit}",
- $aWhere
- );
- return array('result' => $aComments, 'total' => (int) $this->getOne("SELECT FOUND_ROWS()"));
- }
- /**
- * @param string $sText message body
- * @param int $iObjectId item's object ID
- * @param string $sName module's name from sys_modules
- * @param int $iProfileId comments' author
- * @param int $iDate comments' time
- * @return mixed last added comments id or false
- *
- */
- public function addLiveComment($sText, $iObjectId, $sName, $iProfileId, $iDate = 0)
- {
- $iSystem = $this->getOne("SELECT `{$this->CNF['FIELD_COBJ_ID']}`
- FROM `{$this->CNF['TABLE_CMTS_OBJECTS']}`
- WHERE `{$this->CNF['FIELD_COBJ_MODULE']}`=:name LIMIT 1", array('name' => $sName));
- if (!$iSystem)
- return false;
- $sQuery = $this->prepare("REPLACE INTO `{$this->CNF['TABLE_LIVE_COMMENTS']}`
- SET
- `{$this->CNF['FIELD_LCMTS_TEXT']}`=?,
- `{$this->CNF['FIELD_LCMTS_AUTHOR']}`=?,
- `{$this->CNF['FIELD_LCMTS_DATE']}`=?,
- `{$this->CNF['FIELD_LCMTS_OBJECT_ID']}`=?,
- `{$this->CNF['FIELD_LCMTS_SYS_ID']}`=?
- ", $sText, $iProfileId, $iDate ? $iDate : time(), $iObjectId, $iSystem);
- return $this->query($sQuery) ? $this->lastId() : false;
- }
- public function removeLiveComment($iObjectId, $iProfileId)
- {
- return $this->query("DELETE FROM `{$this->CNF['TABLE_LIVE_COMMENTS']}`
- WHERE `{$this->CNF['FIELD_LCMTS_AUTHOR']}`=:author AND `{$this->CNF['FIELD_LCMTS_ID']}`=:object_id", array('author' => $iProfileId, 'object_id' => $iObjectId));
- }
- public function getLotFiles($iLotId, $iStart = 0, $iPerPage = 0)
- {
- $aWhere = array('id' => $iLotId);
- $sLimit = '';
- if ($iPerPage) {
- $aWhere['start'] = (int) $iStart;
- $aWhere['per_page'] = (int) $iPerPage;
- $sLimit = "LIMIT :start, :per_page";
- }
- return $this->getAll("SELECT `s`.*
- FROM `{$this->CNF['OBJECT_STORAGE']}` as `s`
- LEFT JOIN `{$this->CNF['TABLE_MESSAGES']}` as `j` ON `s`.`{$this->CNF['FIELD_ST_JOT']}` = `j`.`{$this->CNF['FIELD_MESSAGE_ID']}`
- LEFT JOIN `{$this->CNF['TABLE_ENTRIES']}` as `l` ON `l`.`{$this->CNF['FIELD_ID']}` = `j`.`{$this->CNF['FIELD_MESSAGE_FK']}`
- WHERE `l`.`{$this->CNF['FIELD_ID']}` = :id
- ORDER BY `s`.`{$this->CNF['FIELD_ST_ADDED']}` DESC
- {$sLimit}
- ", $aWhere);
- }
- public function getLotFilesCount($iLot)
- {
- return (int) $this->getOne("SELECT COUNT(*)
- FROM `{$this->CNF['OBJECT_STORAGE']}` as `s`
- LEFT JOIN `{$this->CNF['TABLE_MESSAGES']}` as `j` ON `s`.`{$this->CNF['FIELD_ST_JOT']}` = `j`.`{$this->CNF['FIELD_MESSAGE_ID']}`
- LEFT JOIN `{$this->CNF['TABLE_ENTRIES']}` as `l` ON `l`.`{$this->CNF['FIELD_ID']}` = `j`.`{$this->CNF['FIELD_MESSAGE_FK']}`
- WHERE `l`.`{$this->CNF['FIELD_ID']}` = :id
- ORDER BY `s`.`{$this->CNF['FIELD_ST_ADDED']}` DESC
- ", array('id' => $iLot));
- }
- public function getSentNtfsNumber($iProfileId, $iLotId)
- {
- $iInterval = (int) $this->CNF['PARAM_NTFS_INTERVAL'];
- return $this->getOne("SELECT COUNT(*)
- FROM `bx_notifications_events`
- WHERE `action`='got_jot_ntfs'
- AND `type`=:type
- AND `object_owner_id`=:profile
- AND `object_id`=:lot_id
- AND `date` > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL :interval HOUR))",
- array(
- 'type' => $this->_oConfig->getName(),
- 'profile' => $iProfileId,
- 'lot_id' => $iLotId,
- 'interval' => $iInterval
- )
- );
- }
- /**
- * @param int $iJotId id of the message
- * @param int $iProfileId viewer id
- * @param int $iJotAuthor message's author
- * @param int $iLotAuthorId author id
- * @return bool
- */
- public function isAllowedToDeleteJot($iJotId, $iProfileId = 0, $iJotAuthor = 0, $iLotAuthorId = 0)
- {
- if (!$iJotId)
- return true;
- if (!($aJot = $this->getJotById($iJotId)))
- return false;
- if (!$iProfileId)
- $iProfileId = bx_get_logged_profile_id();
- $mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_DELETE_MESSAGES, $iProfileId);
- if ($mixedResult === true)
- return true;
- if (!$iJotAuthor)
- $iJotAuthor = $aJot[$this->CNF['FIELD_MESSAGE_AUTHOR']];
- if (!$iLotAuthorId) {
- $iLotId = $aJot[$this->CNF['FIELD_MESSAGE_FK']];
- $bIsLotAuthor = $this->isAuthor($iLotId, $iProfileId);
- } else
- $bIsLotAuthor = $iLotAuthorId == $iProfileId;
- return ($this->CNF['ALLOW_TO_REMOVE_MESSAGE'] && $iJotAuthor == $iProfileId) || ($this->CNF['ALLOW_TO_MODERATE_MESSAGE_FOR_AUTHORS'] && $bIsLotAuthor);
- }
- public function isAllowedToEditJot($iJotId, $iProfileId)
- {
- if (!$iJotId || !($aJot = $this->getJotById($iJotId)))
- return false;
- $mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_EDIT_MESSAGES, $iProfileId);
- if ($mixedResult === true)
- return true;
- $iJotAuthor = $aJot[$this->CNF['FIELD_MESSAGE_AUTHOR']];
- $iLotId = $aJot[$this->CNF['FIELD_MESSAGE_FK']];
- $bIsLotAuthor = $this->isAuthor($iLotId, $iProfileId);
- return ($this->CNF['ALLOW_TO_REMOVE_MESSAGE'] && +$iJotAuthor === +$iProfileId) || ($this->CNF['ALLOW_TO_MODERATE_MESSAGE_FOR_AUTHORS'] && $bIsLotAuthor);
- }
- function createJVC($iLotId, $iProfileId)
- {
- $aJVC = $this->getJVC($iLotId);
- $aOpened = array();
- if (empty($aJVC)) {
- $aLotInfo = $this->getLotInfoById($iLotId);
- $sRoom = $this->_oConfig->getRoomId($aLotInfo[$this->CNF['FIELD_ID']], $aLotInfo[$this->CNF['FIELD_AUTHOR']]);
- $this->query(
- "INSERT INTO `{$this->CNF['TABLE_JVC']}`
- SET
- `{$this->CNF['FJVC_ROOM']}`=:room,
- `{$this->CNF['FJVC_LOT_ID']}`=:lot_id,
- `{$this->CNF['FJVC_NUMBER']}`=1
- ",
- array(
- 'room' => $sRoom,
- 'lot_id' => $iLotId
- )
- );
- $iJVCId = $this->lastId();
- } else {
- $iJVCId = $aJVC[$this->CNF['FJVC_ID']];
- $aOpened = $this->closeAllOpenedConversations($iJVCId);
- $sRoom = $aJVC[$this->CNF['FJVC_ROOM']];
- $this->query("UPDATE `{$this->CNF['TABLE_JVC']}`
- SET `{$this->CNF['FJVC_NUMBER']}`= 1
- WHERE `{$this->CNF['FJVC_LOT_ID']}`= :lot_id
- ", array('lot_id' => $iLotId));
- }
- if ($iJVCId && ($iJVCItemId = $this->addJVCItem($iJVCId, $iProfileId))) {
- $this->updateJVC($iLotId, $this->CNF['FJVC_ACTIVE'], $iJVCItemId);
- $iJotId = $this->addJot($iLotId, '', $iProfileId);
- $this->updateJot($iJotId, $this->CNF['FIELD_MESSAGE_VIDEOC'], $iJVCItemId);
- }
- return array('jitsi_id' => $iJVCId, 'jot_id' => $iJotId, $this->CNF['FJVC_ROOM'] => $sRoom, 'opened' => array_values($aOpened));
- }
- public function getJotIdByJitsiItem($iJitsiId)
- {
- $sQuery = $this->prepare("SELECT `{$this->CNF['FIELD_MESSAGE_ID']}`
- FROM `{$this->CNF['TABLE_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_MESSAGE_VIDEOC']}` = ?
- ORDER BY `{$this->CNF['FIELD_MESSAGE_ID']}` DESC
- LIMIT 1", $iJitsiId);
- return $this->getOne($sQuery);
- }
- function addJVCItem($iId, $iProfileId)
- {
- $this->query(
- "INSERT INTO `{$this->CNF['TABLE_JVCT']}` SET
- `{$this->CNF['FJVCT_AUTHOR_ID']}` = :author,
- `{$this->CNF['FJVCT_FK']}`= :id,
- `{$this->CNF['FJVCT_PART']}`= :part,
- `{$this->CNF['FJVCT_JOINED']}`= :part,
- `{$this->CNF['FJVCT_START']}` = UNIX_TIMESTAMP()
- ",
- array(
- 'author' => $iProfileId,
- 'id' => $iId,
- 'part' => $iProfileId
- )
- );
- return $this->lastId();
- }
- function closeAllOpenedConversations($iJVCId)
- {
- $aAllOpenedChats = $this->getPairs("SELECT `j`.`{$this->CNF['FIELD_MESSAGE_ID']}`, `v`.`{$this->CNF['FJVCT_ID']}` as `jot_id`
- FROM `{$this->CNF['TABLE_JVCT']}` as `v`
- LEFT JOIN `{$this->CNF['TABLE_MESSAGES']}` as `j` ON `v`.`{$this->CNF['FJVCT_ID']}` = `j`.`{$this->CNF['FIELD_MESSAGE_VIDEOC']}`
- WHERE `{$this->CNF['FJVCT_FK']}`= :id AND `{$this->CNF['FJVCT_END']}`= 0", 'jot_id', $this->CNF['FIELD_MESSAGE_ID'], array('id' => $iJVCId));
- if (empty($aAllOpenedChats))
- return array();
- $this->query(
- "UPDATE `{$this->CNF['TABLE_JVCT']}`
- SET
- `{$this->CNF['FJVCT_END']}`= UNIX_TIMESTAMP()
- WHERE `{$this->CNF['FJVCT_FK']}`= :id AND `{$this->CNF['FJVCT_END']}`= 0",
- array('id' => $iJVCId)
- );
- return $aAllOpenedChats;
- }
- function joinToActiveJVC($iLotId, $iProfileId, $bJoin = true)
- {
- $aItem = $this->getActiveJVCItem($iLotId);
- if (empty($aItem))
- return false;
- $aParticipants = array();
- if ($aItem[$this->CNF['FJVCT_PART']])
- $aParticipants = explode(',', $aItem[$this->CNF['FJVCT_PART']]);
- if (!in_array($iProfileId, $aParticipants))
- $aParticipants[] = $iProfileId;
- $aJoined = $aItem[$this->CNF['FJVCT_JOINED']] ? explode(',', $aItem[$this->CNF['FJVCT_JOINED']]) : array();
- if (!in_array($iProfileId, $aJoined))
- $aJoined[] = $iProfileId;
- $this->updateJVCItem($aItem[$this->CNF['FJVCT_ID']], $this->CNF['FJVCT_PART'], implode(',', $aParticipants));
- if ($bJoin)
- $this->updateJVCItem($aItem[$this->CNF['FJVCT_ID']], $this->CNF['FJVCT_JOINED'], implode(',', $aJoined));
- $this->updateJVC($iLotId, $this->CNF['FJVC_NUMBER'], count($aParticipants));
- return true;
- }
- function updateJVC($iLotId, $sField, $mixedValue)
- {
- return $this->query(
- "UPDATE `{$this->CNF['TABLE_JVC']}` SET
- `{$sField}`=:value
- WHERE `{$this->CNF['FJVC_LOT_ID']}`=:lot_id",
- array(
- 'lot_id' => $iLotId,
- 'value' => $mixedValue
- )
- );
- }
- function leaveJVC($iLotId, $sField, $mixedValue)
- {
- return $this->query(
- "UPDATE `{$this->CNF['TABLE_JVC']}` SET
- `{$sField}`=:value
- WHERE `{$this->CNF['FJVC_LOT_ID']}`=:lot_id",
- array(
- 'lot_id' => $iLotId,
- 'value' => $mixedValue
- )
- );
- }
- function updateJVCItem($iId, $sField, $mixedValue)
- {
- return $this->query(
- "UPDATE `{$this->CNF['TABLE_JVCT']}` SET
- `{$sField}`=:value
- WHERE `{$this->CNF['FJVC_ID']}`=:id",
- array(
- 'id' => $iId,
- 'value' => $mixedValue
- )
- );
- }
- function closeJVC($iLotId)
- {
- $aJVC = $this->getJVC($iLotId);
- if (empty($aJVC) || !(int) $aJVC[$this->CNF['FJVC_ACTIVE']])
- return false;
- $this->query(
- "UPDATE `{$this->CNF['TABLE_JVCT']}`
- SET
- `{$this->CNF['FJVCT_JOINED']}`=:joined,
- `{$this->CNF['FJVCT_END']}`= UNIX_TIMESTAMP()
- WHERE `{$this->CNF['FJVCT_ID']}`= :id",
- array(
- 'id' => $aJVC[$this->CNF['FJVC_ACTIVE']],
- 'joined' => '',
- )
- );
- $this->updateJVC($iLotId, $this->CNF['FJVC_ACTIVE'], 0);
- return $this->updateJVC($iLotId, $this->CNF['FJVC_NUMBER'], 0);
- }
- function stopJVC($iLotId, $iProfileId)
- {
- $aJVC = $this->getJVC($iLotId);
- if (empty($aJVC) || !(int) $aJVC[$this->CNF['FJVC_ACTIVE']])
- return false;
- $aTrack = $this->getRow(
- "SELECT * FROM `{$this->CNF['TABLE_JVCT']}`
- WHERE `{$this->CNF['FJVCT_ID']}`= :id",
- array(
- 'id' => $aJVC[$this->CNF['FJVC_ACTIVE']]
- )
- );
- $aJoined = explode(',', $aTrack[$this->CNF['FJVCT_JOINED']]);
- if (in_array($iProfileId, $aJoined))
- unset($aJoined[array_search($iProfileId, $aJoined)]);
- else
- $this->joinToActiveJVC($iLotId, $iProfileId, false);
- $sEnd = '';
- $iJoinedNumber = count($aJoined);
- if (!$iJoinedNumber)
- $sEnd = ",`{$this->CNF['FJVCT_END']}`= UNIX_TIMESTAMP()";
- $this->query(
- "UPDATE `{$this->CNF['TABLE_JVCT']}`
- SET
- `{$this->CNF['FJVCT_JOINED']}`=:joined
- {$sEnd}
- WHERE `{$this->CNF['FJVCT_ID']}`= :id",
- array(
- 'id' => $aJVC[$this->CNF['FJVC_ACTIVE']],
- 'joined' => implode(',', $aJoined),
- )
- );
- if (!$iJoinedNumber)
- $this->updateJVC($iLotId, $this->CNF['FJVC_ACTIVE'], 0);
- return $this->updateJVC($iLotId, $this->CNF['FJVC_NUMBER'], $iJoinedNumber);
- }
- function getJVC($iLotId)
- {
- return $this->getRow(
- "SELECT * FROM `{$this->CNF['TABLE_JVC']}`
- WHERE `{$this->CNF['FJVC_LOT_ID']}`= :lot_id",
- array('lot_id' => $iLotId)
- );
- }
- function getJVCItem($iId)
- {
- return $this->getRow(
- "SELECT *
- FROM `{$this->CNF['TABLE_JVCT']}`
- WHERE `{$this->CNF['FJVCT_ID']}`= :id",
- array('id' => $iId)
- );
- }
- function getActiveJVCItem($iLotId, $sFiled = '')
- {
- $aJVC = $this->getJVC($iLotId);
- if (empty($aJVC) || !(int) $aJVC[$this->CNF['FJVC_ACTIVE']])
- return false;
- $aInfo = $this->getJVCItem($aJVC[$this->CNF['FJVC_ACTIVE']]);
- if ($sFiled)
- return isset($aInfo[$sFiled]) ? $aInfo[$sFiled] : false;
- return $aInfo;
- }
- /*********************** REACT JOT Integration part *****************/
- /**
- * Returns installed list of modules with comments ability
- * @param string $sModule
- * @return array
- */
- function getAllCmtsModule($sModule = '')
- {
- return $this->getAllWithKey("SELECT
- `c`.`Module` as `module`,
- `TriggerTable` as `table`,
- `TriggerFieldId` as `id`,
- `TriggerFieldAuthor` as `owner`,
- `TriggerFieldTitle` as `title`,
- `TriggerFieldComments` as `cmts`,
- `sm`.*,
- `m`.`icon`
- FROM `sys_objects_cmts` as `c`
- LEFT JOIN `sys_modules` as `sm` ON `sm`.`name` = `c`.`module`
- LEFT JOIN `sys_menu_items` as `m` ON `c`.`module` = `m`.`module` AND `m`.`set_name` = 'sys_site'
- WHERE `c`.`module` != 'bx_timeline'
- GROUP BY `c`.`module`
- ", 'module');
- }
- function addJotReaction($iJotId, $iProfileId, $aEmoji)
- {
- $CNF = &$this->_oConfig->CNF;
- return $this->query("REPLACE INTO `{$CNF['TABLE_JOT_REACTIONS']}`
- SET
- `{$CNF['FIELD_REACT_JOT_ID']}` = :jot_id,
- `{$CNF['FIELD_REACT_NATIVE']}` = :native,
- `{$CNF['FIELD_REACT_EMOJI_ID']}` = :emoji_id,
- `{$CNF['FIELD_REACT_PROFILE_ID']}` = :profile_id,
- `{$CNF['FIELD_REACT_ADDED']}` = UNIX_TIMESTAMP()
- ", array(
- 'jot_id' => $iJotId,
- 'native' => $aEmoji['native'],
- 'emoji_id' => $aEmoji['id'],
- 'profile_id' => $iProfileId
- ));
- }
- function deleteReaction($iJotId, $iProfileId, $sEmoji)
- {
- $CNF = &$this->_oConfig->CNF;
- return $this->query("DELETE FROM `{$CNF['TABLE_JOT_REACTIONS']}`
- WHERE
- `{$CNF['FIELD_REACT_JOT_ID']}` = :jot_id AND
- `{$CNF['FIELD_REACT_PROFILE_ID']}` = :profile_id AND
- `{$CNF['FIELD_REACT_EMOJI_ID']}` = :emoji_id
- ", array(
- 'jot_id' => $iJotId,
- 'profile_id' => $iProfileId,
- 'emoji_id' => $sEmoji
- ));
- }
- function deleteJotReactions($iJotId)
- {
- $CNF = &$this->_oConfig->CNF;
- return $this->query("DELETE FROM `{$CNF['TABLE_JOT_REACTIONS']}`
- WHERE
- `{$CNF['FIELD_REACT_JOT_ID']}` = :jot_id
- ", array(
- 'jot_id' => $iJotId
- ));
- }
- function updateReaction($iJotId, $iProfileId, $sEmojiId, $sAction = BX_JOT_REACTION_ADD)
- {
- $CNF = &$this->_oConfig->CNF;
- if ($sAction === BX_JOT_REACTION_ADD) {
- $sNative = $this->getOne("SELECT `{$CNF['FIELD_REACT_NATIVE']}`
- FROM `{$CNF['TABLE_JOT_REACTIONS']}`
- WHERE `{$CNF['FIELD_REACT_JOT_ID']}` = :jot_id AND `{$CNF['FIELD_REACT_EMOJI_ID']}`=:emoji LIMIT 1", ['jot_id' => $iJotId, 'emoji' => $sEmojiId]);
- return $this->addJotReaction($iJotId, $iProfileId, array('native' => $sNative, 'id' => $sEmojiId));
- }
- return $this->deleteReaction($iJotId, $iProfileId, $sEmojiId);
- }
- function getJotReactions($iJotId)
- {
- $CNF = &$this->_oConfig->CNF;
- return $this->getAll("SELECT * FROM `{$CNF['TABLE_JOT_REACTIONS']}`
- WHERE
- `{$CNF['FIELD_REACT_JOT_ID']}` = :jot_id
- ORDER BY `{$CNF['FIELD_REACT_ADDED']}`", array('jot_id' => $iJotId));
- }
- public function findInHistory($iLotId = 0, $sText = '', $iStart = 0, $iLimit = 10, $sOrder = 'DESC', $bSkipDeleted = false)
- {
- if (!$sText)
- return array();
- $aWhere[] = "`j`.`{$this->CNF['FIELD_MESSAGE']}` LIKE :message";
- $aParams['message'] = "%{$sText}%";
- if ($iLotId) {
- $aWhere[] = " `l`.`{$this->CNF['FIELD_ID']}`=:id ";
- $aParams['id'] = $iLotId;
- }
- if ($bSkipDeleted) {
- $aWhere[] = " `j`.`{$this->CNF['FIELD_MESSAGE_TRASH']}` = 0 ";
- }
- $sLimit = '';
- $sOrder = $sOrder === 'ASC' ? 'ASC' : 'DESC';
- if ($iLimit) {
- $aParams['start'] = (int) $iStart;
- $aParams['limit'] = (int) $iLimit;
- $sLimit = "LIMIT :start, :limit";
- }
- if (!empty($aWhere))
- $sWhere = implode(' AND ', $aWhere);
- return $this->getAll("SELECT
- `l`.`{$this->CNF['FIELD_TITLE']}`,
- `l`.`{$this->CNF['FIELD_TYPE']}`,
- `l`.`{$this->CNF['FIELD_ADDED']}` as `talk_added`,
- `l`.`{$this->CNF['FIELD_AUTHOR']}` as `talk_author`,
- `l`.`{$this->CNF['FIELD_PARTICIPANTS']}`,
- `j`.`{$this->CNF['FIELD_MESSAGE']}`,
- `j`.`{$this->CNF['FIELD_MESSAGE_ID']}` as `message_id`,
- `j`.`{$this->CNF['FIELD_ADDED']}` as `message_added`,
- `j`.`{$this->CNF['FIELD_MESSAGE_AUTHOR']}` as `message_author`
- FROM `{$this->CNF['TABLE_ENTRIES']}` as `l`
- LEFT JOIN `{$this->CNF['TABLE_MESSAGES']}` as `j` ON `l`.`{$this->CNF['FIELD_ID']}` = `j`.`{$this->CNF['FIELD_MESSAGE_FK']}`
- WHERE {$sWhere}
- ORDER BY `j`.`{$this->CNF['FIELD_MESSAGE_ID']}` {$sOrder}
- {$sLimit}", $aParams);
- }
- public function removeNotifications($iJotId, $iConvoId = 0)
- {
- $sModule = $this->_oConfig->getName();
- if ($iConvoId)
- return $this->query("DELETE FROM `bx_notifications_events` WHERE `object_id`=:id AND `type`=:name", ['id' => $iConvoId, 'name' => $sModule]);
- return $this->query("DELETE FROM `bx_notifications_events` WHERE `subobject_id`=:id AND `type`=:name", ['id' => $iJotId, 'name' => $sModule]);
- }
- public function markNotificationAsRead($iRecipientId, $iLotId)
- {
- if (!$this->isModuleByName('bx_notifications'))
- return false;
- $aEvents = $this->getAll("SELECT `e`.`id`
- FROM `bx_notifications_events` AS `e`
- LEFT JOIN `bx_notifications_queue` AS `q` ON `q`.`event_id` = `e`.`id`
- WHERE `action`='got_jot_ntfs'
- AND `e`.`type`=:type
- AND `e`.`object_owner_id`=:object_owner_id
- AND `e`.`object_id`=:lot_id",
- array(
- 'type' => $this->_oConfig->getName(),
- 'lot_id' => $iLotId,
- 'object_owner_id' => $iRecipientId
- )
- );
- if (empty($aEvents))
- return false;
- foreach ($aEvents as &$aEvent) {
- if (
- $this->query(
- "DELETE FROM `bx_notifications_events` WHERE `id`=:id",
- array(
- 'id' => $aEvent['id']
- )
- )
- )
- $this->query("DELETE FROM `bx_notifications_queue` WHERE `profile_id`=:profile_id AND `event_id`=:event_id", array(
- 'profile_id' => $iRecipientId,
- 'event_id' => $aEvent['id']
- ));
- }
- }
- function getPublicRoomParticipants($sRoom)
- {
- $aRoom = $this->getPublicVideoRoom($sRoom);
- if (empty($aRoom) || !$aRoom[$this->CNF['FPJVC_PARTS']])
- return array();
- return explode(',', $aRoom[$this->CNF['FPJVC_PARTS']]);
- }
- function createPublicVideoRoom($sRoom, $iProfileId)
- {
- $CNF = &$this->_oConfig->CNF;
- $aRoom = $this->getPublicVideoRoom($sRoom);
- if (!empty($aRoom)) {
- if (!(int) $aRoom[$CNF['FPJVC_STATUS']])
- return $this->query("UPDATE `{$this->CNF['TABLE_PUBLIC_JVC']}`
- SET
- `{$this->CNF['FPJVC_CREATED']}`=UNIX_TIMESTAMP(),
- `{$this->CNF['FPJVC_STATUS']}`=1,
- `{$this->CNF['FPJVC_PARTS']}`=:part
- WHERE `{$this->CNF['FPJVC_ROOM']}`=:room
- ", array('room' => $sRoom, 'part' => $iProfileId));
- else
- return $this->updatePublicVideoRoom($sRoom, $iProfileId, BX_JOT_PUBLIC_JITSI_JOIN);
- }
- return $this->query("REPLACE INTO `{$this->CNF['TABLE_PUBLIC_JVC']}`
- SET
- `{$this->CNF['FPJVC_ROOM']}`=:room,
- `{$this->CNF['FPJVC_CREATED']}`=UNIX_TIMESTAMP(),
- `{$this->CNF['FPJVC_PARTS']}`=:part
- ", array('room' => $sRoom, 'part' => $iProfileId));
- }
- function getPublicVideoRoom($sRoom)
- {
- if (!$sRoom)
- return false;
- return $this->getRow("SELECT * FROM `{$this->CNF['TABLE_PUBLIC_JVC']}`
- WHERE `{$this->CNF['FPJVC_ROOM']}`=:room LIMIT 1", array('room' => $sRoom));
- }
- function updatePublicVideoRoom($sRoom, $iProfileId, $sAction)
- {
- $aRoom = $this->getPublicVideoRoom($sRoom);
- if (empty($aRoom))
- return false;
- if ($sAction == BX_JOT_PUBLIC_JITSI_CLOSE)
- return $this->query("UPDATE `{$this->CNF['TABLE_PUBLIC_JVC']}`
- SET
- `{$this->CNF['FPJVC_STATUS']}`=0
- WHERE `{$this->CNF['FPJVC_ROOM']}`=:room
- ", array('room' => $sRoom));
- $aParticipants = array();
- if ($aRoom[$this->CNF['FPJVC_PARTS']])
- $aParticipants = explode(',', $aRoom[$this->CNF['FPJVC_PARTS']]);
- if ($sAction == BX_JOT_PUBLIC_JITSI_JOIN && !in_array($iProfileId, $aParticipants))
- $aParticipants[] = $iProfileId;
- if ($sAction == BX_JOT_PUBLIC_JITSI_LEAVE)
- $aParticipants = array_diff($aParticipants, array($iProfileId));
- return $this->query(
- "UPDATE `{$this->CNF['TABLE_PUBLIC_JVC']}`
- SET
- `{$this->CNF['FPJVC_PARTS']}`=:parts,
- `{$this->CNF['FPJVC_STATUS']}`=:status
- WHERE `{$this->CNF['FPJVC_ROOM']}`=:room
- ",
- array(
- 'room' => $sRoom,
- 'parts' => implode(',', $aParticipants),
- 'status' => (int) !empty($aParticipants)
- )
- );
- }
- function saveLotSettings($iLotId, $mixedOptions, $sField = 'actions')
- {
- if (!$iLotId || !$sField)
- return false;
- if (empty($mixedOptions))
- $mixedOptions = [];
- $mixedData = is_array($mixedOptions) ? @serialize($mixedOptions) : (int) $mixedOptions;
- $aLotSettings = $this->getLotSettings($iLotId, false);
- if (empty($aLotSettings)) {
- return $this->query("INSERT INTO `{$this->CNF['TABLE_LOT_SETTINGS']}`
- SET
- `{$sField}` = :data,
- `{$this->CNF['FLS_ID']}` = :id", array('data' => $mixedData, 'id' => $iLotId));
- }
- return $this->query("UPDATE `{$this->CNF['TABLE_LOT_SETTINGS']}`
- SET
- `{$sField}` = :actions
- WHERE `{$this->CNF['FLS_ID']}` = :id", array('actions' => $mixedData, 'id' => $iLotId));
- }
- function isActionAllowed($iLotId, $sAction = BX_MSG_SETTING_MSG)
- {
- $mixedOptions = $this->getLotSettings($iLotId);
- if ($mixedOptions === false)
- return true;
- return in_array($sAction, $mixedOptions);
- }
- function getLotSettings($iLotId, $sField = 'actions')
- {
- $mixedOptions = false;
- if (!$iLotId || !($mixedOptions = $this->getRow("SELECT * FROM `{$this->CNF['TABLE_LOT_SETTINGS']}` WHERE `{$this->CNF['FLS_ID']}` = :id", array('id' => $iLotId))))
- return false;
- return $sField && isset($mixedOptions[$sField]) ?
- ($sField !== $this->CNF['FLS_ICON'] ? @unserialize($mixedOptions[$sField]) : (int) $mixedOptions[$sField]) : $mixedOptions;
- }
- function getLotAttachmentType($sName)
- {
- $sService = $this->getOne("SELECT `{$this->CNF['FLAT_SERVICE']}` FROM `{$this->CNF['TABLE_LOT_ATTACHMENTS']}` WHERE `{$this->CNF['FLAT_NAME']}`=:name LIMIT 1", array('name' => $sName));
- return $sService ? @unserialize($sService) : false;
- }
- function updateLotAttachmentType(&$aData)
- {
- if (!isset($aData['name']) || !isset($aData['service']))
- return false;
- $sService = is_array($aData['service']) ? @serialize($aData['service']) : $aData['service'];
- return $this->query(
- "REPLACE INTO `{$this->CNF['TABLE_LOT_ATTACHMENTS']}`
- SET
- `{$this->CNF['FLSE_NAME']}`=:name,
- `{$this->CNF['FLAT_SERVICE']}`=:service",
- array('name' => $aData['name'], 'service' => $sService)
- );
- }
- function getGroup($iGroupId)
- {
- return $this->getRow("SELECT * FROM `{$this->CNF['TABLE_GROUPS']}` WHERE `{$this->CNF['FMG_ID']}`=:id LIMIT 1", array('id' => $iGroupId));
- }
- function addGroup($iProfileId, $aParams)
- {
- $aGroup = array();
- if (isset($aParams[$this->CNF['FMG_ID']]))
- $aGroup = $this->getGroup($aParams[$this->CNF['FMG_ID']]);
- if (empty($aGroup))
- return $this->query(
- "INSERT INTO `{$this->CNF['TABLE_GROUPS']}`
- SET `{$this->CNF['FMG_NAME']}` = :name,
- `{$this->CNF['FMG_PRIVACY']}` = :privacy,
- `{$this->CNF['FMG_AUTHOR']}` = :author,
- `{$this->CNF['FMG_DESC']}` = :desc,
- `{$this->CNF['FMG_ADDED']}` = UNIX_TIMESTAMP()",
- array(
- 'name' => $aParams[$this->CNF['FMG_NAME']],
- 'author' => $iProfileId,
- 'desc' => $aParams[$this->CNF['FMG_DESC']],
- 'privacy' => $aParams[$this->CNF['FMG_PRIVACY']]
- )
- );
- return $this->query(
- "UPDATE `{$this->CNF['TABLE_GROUPS']}`
- SET `{$this->CNF['FMG_NAME']}` = :name,
- `{$this->CNF['FMG_PRIVACY']}` = :privacy,
- `{$this->CNF['FMG_DESC']}` = :desc
- WHERE `{$this->CNF['FMG_ID']}`=:id",
- array('name' => $aParams[$this->CNF['FMG_NAME']], 'desc' => $aParams[$this->CNF['FMG_DESC']], 'privacy' => $aParams[$this->CNF['FMG_PRIVACY']], 'id' => $aParams[$this->CNF['FMG_ID']])
- );
- }
- function getGroups($iProfileId = 0)
- {
- if ($iProfileId)
- return $this->getAll("SELECT * FROM `{$this->CNF['TABLE_GROUPS']}` WHERE `{$this->CNF['FMG_AUTHOR']}`=:profile", array('author' => $iProfileId));
- return $this->getAll("SELECT * FROM `{$this->CNF['TABLE_GROUPS']}` ORDER BY {$this->CNF['FMG_NAME']}", array('author' => $iProfileId));
- }
- function addLotToGroup($iLotId, $iGroupId)
- {
- return $this->query("REPLACE INTO `{$this->CNF['TABLE_GROUPS_LOTS']}`
- SET `{$this->CNF['FMGL_LOT_ID']}`=:lot_id,
- `{$this->CNF['FMGL_GROUP_ID']}`=:group_id", array('group_id' => $iGroupId, 'lot_id' => $iLotId));
- }
- function saveJotItem($iJotId, $iProfileId)
- {
- return $this->query("REPLACE INTO `{$this->CNF['TABLE_SAVED_JOTS']}`
- SET `{$this->CNF['FSJ_ID']}`=:jot_id,
- `{$this->CNF['FSJ_PROFILE_ID']}`=:profile_id", array('jot_id' => $iJotId, 'profile_id' => $iProfileId));
- }
- /**
- * Talks where the member has saved messages
- * @param int $iProfileId profile id
- * @return array lots ids
- */
- public function getSavedLotIds($iProfileId)
- {
- if (!$iProfileId) {
- return [];
- }
- return array_map('intval', $this->getColumn("SELECT DISTINCT `m`.`{$this->CNF['FIELD_MESSAGE_FK']}`
- FROM `{$this->CNF['TABLE_SAVED_JOTS']}` AS `s`
- INNER JOIN `{$this->CNF['TABLE_MESSAGES']}` AS `m` ON `m`.`{$this->CNF['FIELD_MESSAGE_ID']}` = `s`.`{$this->CNF['FSJ_ID']}`
- WHERE `s`.`{$this->CNF['FSJ_PROFILE_ID']}` = :profile_id", ['profile_id' => (int)$iProfileId]));
- }
- /**
- * Messages from the list that the member saved
- * @param int $iProfileId profile id
- * @param array $aJotIds jots ids
- * @return array jots ids
- */
- public function getSavedJotsByIds($iProfileId, $aJotIds)
- {
- $aJotIds = array_filter(array_map('intval', (array)$aJotIds));
- if (!$iProfileId || empty($aJotIds)) {
- return [];
- }
- return $this->getColumn("SELECT `{$this->CNF['FSJ_ID']}` FROM `{$this->CNF['TABLE_SAVED_JOTS']}`
- WHERE `{$this->CNF['FSJ_PROFILE_ID']}` = :profile_id AND `{$this->CNF['FSJ_ID']}` IN (" . implode(',', $aJotIds) . ")", ['profile_id' => (int)$iProfileId]);
- }
- function isJotSaved($iJotId, $iProfileId)
- {
- return $this->getOne(
- "SELECT COUNT(*) FROM `{$this->CNF['TABLE_SAVED_JOTS']}`
- WHERE `{$this->CNF['FSJ_ID']}`=:jot_id AND `{$this->CNF['FSJ_PROFILE_ID']}`=:profile_id",
- ['jot_id' => $iJotId, 'profile_id' => $iProfileId]
- ) != 0;
- }
- function deleteSavedJotItems($iJotId = 0, $iProfileId = 0)
- {
- if ($iJotId && $iProfileId)
- return $this->query(
- "DELETE FROM `{$this->CNF['TABLE_SAVED_JOTS']}`
- WHERE `{$this->CNF['FSJ_ID']}`=:jot_id AND `{$this->CNF['FSJ_PROFILE_ID']}`=:profile_id",
- array('jot_id' => $iJotId, 'profile_id' => $iProfileId)
- );
- if ($iJotId)
- $this->query(
- "DELETE FROM `{$this->CNF['TABLE_SAVED_JOTS']}`
- WHERE `{$this->CNF['FSJ_ID']}`=:jot_id",
- array('jot_id' => $iJotId)
- );
- return $iProfileId ? $this->query(
- "DELETE FROM `{$this->CNF['TABLE_SAVED_JOTS']}`
- WHERE `{$this->CNF['FSJ_PROFILE_ID']}`=:profile_id",
- array('profile_id' => $iProfileId)
- ) : false;
- }
- function getSavedJots($iProfileId)
- {
- return $this->getAllWithKey("SELECT `m`.* FROM `{$this->CNF['TABLE_SAVED_JOTS']}` as `s`
- LEFT JOIN `{$this->CNF['TABLE_MESSAGES']}` as `m` ON `s`.`{$this->CNF['FSJ_ID']}` = `m`.`{$this->CNF['FIELD_ID']}`
- FROM `{$this->CNF['FSJ_PROFILE_ID']}`=:profile_id", $this->CNF['FIELD_ID'], array('profile_id' => $iProfileId));
- }
- function getSavedJotInLots($iProfileId, $aParams)
- {
- $CNF = &$this->_oConfig->CNF;
- $aWhere['start'] = isset($aParams['start']) ? (int) $aParams['start'] : 0;
- $aWhere['per_page'] = isset($aParams['per_page']) ? (int) $aParams['per_page'] : (int) $this->CNF['MAX_LOTS_NUMBER'];
- $sLimit = "LIMIT :start, :per_page";
- $sWhere = '';
- if (isset($aParams['term'])) {
- $aParams = preg_split('/[\s]/', $aParams['term'], -1, PREG_SPLIT_NO_EMPTY);
- $sCriteria = implode('%', $aParams);
- $aWhere['criteria'] = "%{$sCriteria}%";
- $sWhere = "AND `m`.`{$CNF['FIELD_MESSAGE']}` like :criteria";
- }
- return $this->getAll("SELECT `m`.`{$this->CNF['FIELD_MESSAGE_FK']}`,`l`.* FROM `{$this->CNF['TABLE_SAVED_JOTS']}` as `s`
- LEFT JOIN `{$this->CNF['TABLE_MESSAGES']}` as `m` ON `s`.`{$this->CNF['FSJ_ID']}` = `m`.`{$this->CNF['FIELD_ID']}`
- LEFT JOIN `{$this->CNF['TABLE_ENTRIES']}` as `l` ON `l`.`{$this->CNF['FIELD_ID']}` = `m`.`{$this->CNF['FIELD_MESSAGE_FK']}`
- WHERE `{$this->CNF['FSJ_PROFILE_ID']}`=:profile_id {$sWhere}
- GROUP BY `m`.`{$this->CNF['FIELD_MESSAGE_FK']}`
- {$sLimit}", ['profile_id' => $iProfileId] + $aWhere);
- }
- public function getJotReplies($iLotId, $bCount = false)
- {
- $aWhere = array('id' => $iLotId);
- if ($bCount)
- return $this->getOne("SELECT COUNT(*) FROM `{$this->CNF['TABLE_MESSAGES']}` WHERE `{$this->CNF['FIELD_MESSAGE_FK']}`=:id", $aWhere);
- return $this->getAll("SELECT * FROM `{$this->CNF['TABLE_MESSAGES']}`
- WHERE `{$this->CNF['FIELD_MESSAGE_FK']}`=:id
- ORDER BY `{$this->CNF['FIELD_MESSAGE_ID']}`
- ", $aWhere);
- }
- function getReactionsMentionsLots($iProfileId, $aParams)
- {
- $CNF = &$this->_oConfig->CNF;
- $aWhere['start'] = isset($aParams['start']) ? (int) $aParams['start'] : 0;
- $aWhere['per_page'] = isset($aParams['per_page']) ? (int) $aParams['per_page'] : (int) $this->CNF['MAX_LOTS_NUMBER'];
- $sLimit = "LIMIT :start, :per_page";
- $sUrl = BxDolProfile::getInstance($iProfileId)->getUrl();
- $aWhere['parts'] = '(^|,)' . (int) $iProfileId . '(,|$)';
- $sWhere = '';
- if (isset($aParams['term'])) {
- $aParams = preg_split('/[\s]/', $aParams['term'], -1, PREG_SPLIT_NO_EMPTY);
- $sCriteria = implode('%', $aParams);
- $aWhere['criteria'] = "%{$sCriteria}%";
- $sWhere = "AND `m`.`{$CNF['FIELD_MESSAGE']}` like :criteria";
- }
- return $this->getAll("SELECT `m`.`{$CNF['FIELD_MESSAGE_FK']}`, `l`.*
- FROM `{$CNF['TABLE_MESSAGES']}` as `m`
- LEFT JOIN `{$CNF['TABLE_ENTRIES']}` as `l` ON `l`.`{$CNF['FIELD_ID']}` = `m`.`{$CNF['FIELD_MESSAGE_FK']}`
- LEFT JOIN `{$CNF['TABLE_JOT_REACTIONS']}` as `r` ON `m`.`{$CNF['FIELD_MESSAGE_ID']}` = `r`.`{$CNF['FIELD_REACT_JOT_ID']}`
- WHERE ((`r`.`{$CNF['FIELD_REACT_JOT_ID']}` IS NOT NULL AND `m`.`{$CNF['FIELD_MESSAGE_AUTHOR']}`=:profile_id )
- OR `m`.`{$CNF['FIELD_MESSAGE']}` LIKE '\"%" . '/' . bx_ltrim_str($sUrl, BX_DOL_URL_ROOT) . "\"' ) AND
- (`l`.`{$CNF['FIELD_PARTICIPANTS']}` REGEXP :parts OR `l`.`{$CNF['FIELD_AUTHOR']}`=:profile_id) {$sWhere}
- GROUP BY `m`.`{$CNF['FIELD_MESSAGE_FK']}`
- {$sLimit}", ['profile_id' => $iProfileId] + $aWhere);
- }
- function getLotsWithReplies($iProfileId, $aParams = [])
- {
- $CNF = &$this->_oConfig->CNF;
- $aWhere['start'] = isset($aParams['start']) ? (int) $aParams['start'] : 0;
- $aWhere['per_page'] = isset($aParams['per_page']) ? (int) $aParams['per_page'] : (int) $this->CNF['MAX_LOTS_NUMBER'];
- $sLimit = "LIMIT :start, :per_page";
- $sWhere = '';
- if (isset($aParams['term'])) {
- $aParams = preg_split('/[\s]/', $aParams['term'], -1, PREG_SPLIT_NO_EMPTY);
- $sCriteria = implode('%', $aParams);
- $aWhere['criteria'] = "%{$sCriteria}%";
- $sWhere = "AND `m`.`{$CNF['FIELD_MESSAGE']}` like :criteria";
- }
- return $this->getAll("SELECT `m`.`{$CNF['FIELD_MESSAGE_FK']}`, `l`.*
- FROM `{$this->CNF['TABLE_MESSAGES']}` as `m`
- LEFT JOIN `{$this->CNF['TABLE_ENTRIES']}` as `l` ON `l`.`{$this->CNF['FIELD_ID']}` = `m`.`{$this->CNF['FIELD_MESSAGE_FK']}`
- LEFT JOIN `{$CNF['TABLE_MESSAGES']}` as `r` ON `r`.`{$CNF['FIELD_MESSAGE_ID']}` = `m`.`{$CNF['FIELD_MESSAGE_REPLY']}`
- WHERE `r`.`{$CNF['FIELD_MESSAGE_AUTHOR']}`=:profile_id AND `m`.`{$CNF['FIELD_MESSAGE_REPLY']}` <> 0 {$sWhere}
- GROUP BY `m`.`{$CNF['FIELD_MESSAGE_FK']}`
- {$sLimit}", ['profile_id' => $iProfileId] + $aWhere);
- }
- function registerGroup($sUrl, $iLotId, $bForce = false)
- {
- $aUrl = [];
- parse_str($sUrl, $aUrl);
- if (!empty($aUrl) && isset($aUrl['i'])) {
- $oPage = BxDolPage::getObjectInstanceByModuleAndURI(BX_IM_EMPTY_URL, $aUrl['i']);
- if (!$oPage && !$bForce)
- return false;
- $sModule = '';
- $sTitle = _t('_bx_messenger_page_unknown');
- $iAuthor = $iProfileId = 0;
- $iPrivacy = BX_DOL_PG_ALL;
- if ($oPage && ($aPageInfo = $oPage->getObject()) && ($sModule = $oPage->getModule()) && ($oModule = BxDolModule::getInstance($sModule))) {
- $CNF = &$oModule->_oConfig->CNF;
- $sTitle = _t($aPageInfo['title']);
- $iAuthor = $aPageInfo['author'];
- if (isset($aUrl['id']) && !empty($oModule) && ($aResult = bx_srv($sModule, 'get_info', array($aUrl['id'], false)))) {
- $sTitle = $aResult[$CNF['FIELD_TITLE']];
- $iAuthor = $aResult[$CNF['FIELD_AUTHOR']];
- $iProfileId = isset($aResult['profile_id']) ? $aResult['profile_id'] : 0;
- $iPrivacy = isset($aResult[$CNF['FIELD_ALLOW_VIEW_TO']]);
- }
- }
- if (
- $this->query(
- "INSERT INTO `{$this->CNF['TABLE_GROUPS']}`
- SET `{$this->CNF['FMG_NAME']}` = :name,
- `{$this->CNF['FMG_PRIVACY']}` = :privacy,
- `{$this->CNF['FMG_AUTHOR']}` = :author,
- `{$this->CNF['FMG_URL']}` = :url,
- `{$this->CNF['FMG_MODULE']}` = :module,
- `{$this->CNF['FMG_PROFILE_ID']}` =:profile_id,
- `{$this->CNF['FMG_ADDED']}` = UNIX_TIMESTAMP()",
- array(
- 'name' => $sTitle,
- 'author' => $iAuthor,
- 'url' => $sUrl,
- 'module' => in_array($sModule, $this->CNF['ADD-TO-PAGES-AREA'][BX_MSG_TALK_TYPE_PAGES]) ? BX_MSG_TALK_TYPE_PAGES : $sModule,
- 'profile_id' => $iProfileId,
- 'privacy' => $iPrivacy
- )
- )
- )
- return $this->addLotToGroup($iLotId, $this->lastId());
- }
- return false;
- }
- function getMyLotsByGroups($iProfileId, $bCount = false)
- {
- $aWhere = array('profile' => (int) $iProfileId, 'parts' => '(^|,)' . (int) $iProfileId . '(,|$)');
- if ($bCount)
- $sCount = "SQL_CALC_FOUND_ROWS";
- $aGroups = $this->getAll("SELECT
- `l`.*, `g`.`{$this->CNF['FMG_PROFILE_ID']}`,`g`.`{$this->CNF['FMG_MODULE']}`, `g`.`{$this->CNF['FMG_NAME']}`, `gl`.`{$this->CNF['FMGL_GROUP_ID']}`,
- IF (`l`.`{$this->CNF['FIELD_UPDATED']}` = 0, `l`.`{$this->CNF['FIELD_ADDED']}`, `l`.`{$this->CNF['FIELD_UPDATED']}`) as `order`,
- IF (`l`.`{$this->CNF['FIELD_URL']}` = '', `g`.`{$this->CNF['FMG_URL']}`, `l`.`{$this->CNF['FIELD_URL']}`) as `gurl`,
- `un`.`{$this->CNF['FIELD_NEW_UNREAD']}` as `unread`
- FROM `{$this->CNF['TABLE_ENTRIES']}` as `l`
- LEFT JOIN `{$this->CNF['TABLE_NEW_MESSAGES']}` as `un` ON `l`.`{$this->CNF['FIELD_ID']}` = `un`.`{$this->CNF['FIELD_NEW_LOT']}` AND `un`.`{$this->CNF['FIELD_NEW_PROFILE']}`=:profile
- LEFT JOIN `{$this->CNF['TABLE_GROUPS_LOTS']}` as `gl` ON `gl`.`{$this->CNF['FMGL_LOT_ID']}` = `l`.`{$this->CNF['FIELD_ID']}`
- LEFT JOIN `{$this->CNF['TABLE_GROUPS']}` as `g` ON `g`.`{$this->CNF['FMG_ID']}` = `gl`.`{$this->CNF['FMGL_GROUP_ID']}`
- WHERE (`l`.`{$this->CNF['FIELD_PARTICIPANTS']}` REGEXP :parts OR `l`.`{$this->CNF['FIELD_AUTHOR']}`=:profile) AND `gl`.`{$this->CNF['FMGL_GROUP_ID']}` IS NOT NULL
- ORDER BY `g`.`{$this->CNF['FMG_MODULE']}`", $aWhere);
- // print_r($aGroups);
- $aResult = array();
- $sModule = '';
- foreach ($aGroups as &$aGroup) {
- if (!isset($aGroup[$this->CNF['FMG_MODULE']]))
- continue;
- if ($aGroup[$this->CNF['FMG_MODULE']] != $sModule)
- $sModule = $aGroup[$this->CNF['FMG_MODULE']];
- if (!isset($aResult[$sModule][$aGroup[$this->CNF['FMGL_GROUP_ID']]])) {
- $aResult[$sModule][$aGroup[$this->CNF['FMGL_GROUP_ID']]] = [
- $this->CNF['FMG_NAME'] => $aGroup[$this->CNF['FMG_NAME']],
- $this->CNF['FMG_ID'] => $aGroup[$this->CNF['FMG_PROFILE_ID']], // group profile id of the module
- $this->CNF['FMGL_GROUP_ID'] => $aGroup[$this->CNF['FMGL_GROUP_ID']],
- $this->CNF['FMG_URL'] => $aGroup['gurl']
- ];
- }
- // fill each groups with existed talks in it group
- $aResult[$sModule][$aGroup[$this->CNF['FMGL_GROUP_ID']]][$this->CNF['GROUPS_ITEMS_FIELD']][] = [
- $this->CNF['FIELD_ADDED'] => $aGroup[$this->CNF['FIELD_ADDED']],
- $this->CNF['FIELD_AUTHOR'] => $aGroup[$this->CNF['FIELD_AUTHOR']],
- $this->CNF['FIELD_TITLE'] => $aGroup[$this->CNF['FIELD_TITLE']],
- $this->CNF['FIELD_PARTICIPANTS'] => $aGroup[$this->CNF['FIELD_PARTICIPANTS']],
- $this->CNF['FIELD_ID'] => $aGroup[$this->CNF['FIELD_ID']],
- $this->CNF['FMG_URL'] => $aGroup[$this->CNF['FIELD_URL']]
- //$this->CNF['FMG_PROFILE_ID'] => $aGroup[$this->CNF['FMG_PROFILE_ID']],
- ];
- }
- return $aResult;
- }
- function getGroupIdByLotId($iLotId)
- {
- return $this->getOne("SELECT `{$this->CNF['FMGL_GROUP_ID']}`
- FROM `{$this->CNF['TABLE_GROUPS_LOTS']}`
- WHERE `{$this->CNF['FMGL_LOT_ID']}`=:id", array('id' => $iLotId));
- }
- function getTalksByGroupId($iGroupId)
- {
- return $this->getAllWithKey("SELECT `l`.*
- FROM `{$this->CNF['TABLE_GROUPS_LOTS']}` as `gl`
- LEFT JOIN `{$this->CNF['TABLE_ENTRIES']}` as `l` ON `gl`.`{$this->CNF['FMGL_LOT_ID']}` = `l`.`{$this->CNF['FIELD_ID']}`
- WHERE `gl`.`{$this->CNF['FMGL_GROUP_ID']}`=:id AND `l`.`id` IS NOT NULL
- ", $this->CNF['FIELD_ID'], array('id' => $iGroupId));
- }
- function getGroupById($iGroupId)
- {
- return $this->getRow("SELECT *
- FROM `{$this->CNF['TABLE_GROUPS']}`
- WHERE `{$this->CNF['FMG_ID']}`=:id LIMIT 1", array('id' => $iGroupId));
- }
- function getGroupByUrl($sUrl)
- {
- return $this->getRow("SELECT * FROM `{$this->CNF['TABLE_GROUPS']}` WHERE `{$this->CNF['FMG_URL']}`=:url LIMIT 1", array('url' => $sUrl));
- }
- function getLotType($iLotId)
- {
- $aInfo = $this->getLotInfoById($iLotId);
- if (empty($aInfo))
- return false;
- if ($aInfo[$this->CNF['FMGL_GROUP_ID']]) {
- $aGroupInfo = $this->getGroup($aInfo[$this->CNF['FMGL_GROUP_ID']]);
- return [
- 'group_id' => $aInfo[$this->CNF['FMGL_GROUP_ID']],
- 'type' => BX_MSG_TALK_TYPE_GROUPS,
- 'groups_type' => $aGroupInfo[$this->CNF['FMG_MODULE']]
- ];
- }
- if ((int) $aInfo[$this->CNF['FIELD_PARENT_JOT']])
- return ['type' => BX_MSG_TALK_TYPE_THREADS];
- if ($aInfo[$this->CNF['FIELD_TYPE']] == BX_IM_TYPE_PRIVATE)
- return ['type' => BX_MSG_TALK_TYPE_DIRECT];
- if ($aInfo[$this->CNF['FIELD_TYPE']] == BX_IM_TYPE_BROADCAST)
- return ['type' => BX_MSG_TALK_TYPE_BROADCAST];
- return false;
- }
- function getUnreadMessagesStat($iProfileId)
- {
- $aItems = $this->getLotsWithUnreadMessages($iProfileId);
- $aResult = [];
- if (empty($aItems))
- return $aResult;
- foreach ($aItems as $iLotId => $iCount) {
- if (!($aType = $this->getLotType($iLotId)))
- continue;
- if (isset($aType['group_id']))
- $aResult['groups'][$aType['groups_type']][$aType['group_id']][$iLotId] = +$iCount;
- else
- $aResult[$aType['type']][$iLotId] = +$iCount;
- }
- return array_merge($aResult, [BX_MSG_TALK_TYPE_INBOX => count($aItems)]);
- }
- function addMediaTrack($iFileId, $iProfileId)
- {
- return $this->query("REPLACE INTO `{$this->CNF['TABLE_JOTS_MEDIA_TRACKER']}`
- SET `{$this->CNF['FJMT_FILE_ID']}`=:id,
- `{$this->CNF['FJMT_USER_ID']}`=:user_id", ['id' => $iFileId, 'user_id' => $iProfileId]);
- }
- function isFileCollapsed($iFileId, $iProfileId)
- {
- return $this->getOne(
- "SELECT COUNT(*) FROM `{$this->CNF['TABLE_JOTS_MEDIA_TRACKER']}`
- WHERE `{$this->CNF['FJMT_FILE_ID']}`=:id AND `{$this->CNF['FJMT_USER_ID']}`=:user_id",
- ['id' => $iFileId, 'user_id' => $iProfileId]
- );
- }
- function removeMediaTracker($iFileId = 0, $iProfileId = 0)
- {
- if (!$iFileId && !$iProfileId)
- return false;
- $aWhereParams = $aWhere = [];
- if ($iFileId) {
- $aWhere[] = "`{$this->CNF['FJMT_FILE_ID']}`=:id";
- $aWhereParams['id'] = $iFileId;
- }
- if ($iProfileId) {
- $aWhere[] = "`{$this->CNF['FJMT_USER_ID']}`=:user_id";
- $aWhereParams['user_id'] = $iProfileId;
- }
- $sWhere = implode(' AND ', $aWhere);
- return $this->query("DELETE FROM `{$this->CNF['TABLE_JOTS_MEDIA_TRACKER']}`
- WHERE {$sWhere}", $aWhereParams);
- }
- public function getObjectAuthorId($iId)
- {
- $sQuery = $this->prepare("SELECT `{$this->CNF['FIELD_MESSAGE_AUTHOR']}` FROM `{$this->CNF['TABLE_MESSAGES']}` WHERE `{$this->CNF['FIELD_MESSAGE_ID']}` = ? LIMIT 1", $iId);
- return (int) $this->getOne($sQuery);
- }
- public function isPerformed($iObjectId, $iAuthorId, $sEmoji)
- {
- $sQuery = $this->prepare("SELECT COUNT(*)
- FROM `{$this->CNF['TABLE_JOT_REACTIONS']}`
- WHERE `{$this->CNF['FIELD_REACT_JOT_ID']}` = ? AND `{$this->CNF['FIELD_REACT_PROFILE_ID']}` = ? AND
- `{$this->CNF['FIELD_REACT_EMOJI_ID']}` = ? LIMIT 1", $iObjectId, $iAuthorId, $sEmoji);
- return (int) $this->getOne($sQuery) != 0;
- }
- public function putVote($iObjectId, $iAuthorId, $sAuthorIp, $aData, $bUndo = false)
- {
- $sReaction = $aData['reaction'];
- $aReactions = BxDolFormQuery::getDataItems('sys_vote_reactions', false, BX_DATA_VALUES_ALL);
- if (!isset($aReactions[$sReaction]))
- return false;
- if (!$aReaction = @unserialize($aReactions[$sReaction]['Data']))
- return false;
- $sQuery = "SELECT `id` FROM `{$this->CNF['TABLE_JOT_REACTIONS']}` WHERE `{$this->CNF['FIELD_REACT_JOT_ID']}` = :object_id
- AND `{$this->CNF['FIELD_REACT_EMOJI_ID']}` = :reaction AND `{$this->CNF['FIELD_REACT_PROFILE_ID']}`=:author LIMIT 1";
- $bExists = (int) $this->getOne($sQuery, array('object_id' => $iObjectId, 'reaction' => $sReaction, 'author' => $iAuthorId)) != 0;
- if ($bExists && $bUndo)
- return $this->deleteReaction($iObjectId, $iAuthorId, $aData['reaction']);
- else
- return $this->addJotReaction($iObjectId, $iAuthorId, ['id' => $this->_oConfig->convertApp2Emoji($aData['reaction']), 'native' => $aReaction['emoji']]);
- return $this->lastId();
- }
- public function getVote($iObjectId)
- {
- $aReactions = BxDolFormQuery::getDataItems('sys_vote_reactions', false, BX_DATA_VALUES_ALL);
- $aReactionsGot = $this->getPairs("SELECT COUNT(*) as `count`, `{$this->CNF['FIELD_REACT_EMOJI_ID']}` FROM `{$this->CNF['TABLE_JOT_REACTIONS']}` WHERE `{$this->CNF['FIELD_REACT_JOT_ID']}` = :object_id GROUP BY `{$this->CNF['FIELD_REACT_EMOJI_ID']}`", $this->CNF['FIELD_REACT_EMOJI_ID'], 'count', array('object_id' => $iObjectId));
- $aResult = array();
- foreach ($aReactions as $sName => $iValue) {
- $iCount = $iSum = 0;
- if (!empty($aReactionsGot[$sName])) {
- $iCount = (int) $aReactionsGot[$sName];
- $iSum += $iCount;
- }
- $aResult['count_' . $sName] = $iCount;
- $aResult['sum_' . $sName] = $iSum;
- $aResult['rate_' . $sName] = $iCount != 0 ? $iSum / $iCount : 0;
- }
- $aResult['count_default'] = $iCount;
- $aResult['sum_default'] = $iSum;
- return $aResult;
- }
- public function getTrack($iObjectId, $iAuthorId)
- {
- return $this->getRow("SELECT *, `{$this->CNF['FIELD_REACT_EMOJI_ID']}` as `reaction` FROM `{$this->CNF['TABLE_JOT_REACTIONS']}` WHERE `{$this->CNF['FIELD_REACT_JOT_ID']}` = :object_id AND `{$this->CNF['FIELD_REACT_PROFILE_ID']}` = :author_id LIMIT 1", array(
- 'object_id' => $iObjectId,
- 'author_id' => $iAuthorId
- ));
- }
- public function getPerformed($aParams = array())
- {
- $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
- $aBindings = array();
- $sSelectClause = '`tt`.*';
- $sJoinClause = $sWhereClause = '';
- $sLimitClause = isset($aParams['start']) && !empty($aParams['per_page']) ? "LIMIT " . $aParams['start'] . ", " . $aParams['per_page'] : "";
- if (!empty($aParams['type']))
- switch ($aParams['type']) {
- case 'by':
- $aBindings['object_id'] = $aParams['object_id'];
- $sJoinClause = "INNER JOIN `sys_profiles` AS `tp` ON `tt`.`user_id`=`tp`.`id` AND `tp`.`status`='active'";
- $sWhereClause = "AND `tt`.`jot_id` = :object_id";
- if (!empty($aParams['reaction'])) {
- $aMethod['name'] = 'getColumn';
- $aBindings['reaction'] = $this->_oConfig->convertApp2Emoji($aParams['reaction']);
- $sSelectClause = "`tt`.`user_id`";
- $sWhereClause .= " AND `tt`.`emoji_id`=:reaction";
- } else {
- $aMethod['name'] = 'getAll';
- $sSelectClause = "`tt`.`user_id`, `tt`.`emoji_id`";
- }
- break;
- }
- $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `{$this->CNF['TABLE_JOT_REACTIONS']}` AS `tt` " . $sJoinClause . " WHERE 1 " . $sWhereClause . $sLimitClause;
- $aMethod['params'][] = $aBindings;
- return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
- }
- /**
- * Occurs when delete context object
- * @param $iAuthorId context id (group,space ) and etc...
- */
- function deleteAuthorEntries($iAuthorId)
- {
- return false;
- }
- public function pruningByDate($iDate)
- {
- return 0;
- }
- function updateConvoHash($iConvoId)
- {
- $sHash = $this->_oConfig->generateConvoHash($iConvoId);
- return $this->query("UPDATE `{$this->CNF['TABLE_ENTRIES']}` SET `{$this->CNF['FIELD_HASH']}`=:hash
- WHERE `{$this->CNF['FIELD_ID']}`=:id ", ['hash' => $sHash, 'id' => $iConvoId]);
- }
- function updateConvoFiled($iConvoId, $sField, $mixedValue)
- {
- if (!$sField)
- return false;
- return $this->query("UPDATE `{$this->CNF['TABLE_ENTRIES']}`
- SET `{$sField}`=:value
- WHERE `{$this->CNF['FIELD_ID']}`=:id", ['value' => $mixedValue, 'id' => $iConvoId]);
- }
- function getConvoByHash($sHash, $bIdOnly = true)
- {
- $aConvo = $this->getRow("SELECT * FROM `{$this->CNF['TABLE_ENTRIES']}` WHERE `{$this->CNF['FIELD_HASH']}`=:hash LIMIT 1", ['hash' => $sHash]);
- return $bIdOnly && !empty($aConvo) ? $aConvo[$this->CNF['FIELD_ID']] : $aConvo;
- }
- private function getProfilesByCriteriaSqlParts($aFields)
- {
- $aSqlParts = ['where' => '', 'join' => ''];
- if (isset($aFields['membership'])) {
- $aSqlParts = BxDolAclQuery::getInstance()->getContentByLevelAsSQLPart('sys_profiles', 'id', $aFields['membership']);
- unset($aFields['membership']);
- }
- if (!empty($aFields)) {
- $aSqlParts['join'] .= "LEFT JOIN `bx_persons_data` as `pd` ON `pd`.`id` = `sys_profiles`.`content_id` AND `sys_profiles`.`type` = 'bx_persons'";
- foreach ($aFields as $sName => $mixedValues) {
- if ($sName === 'location' && !empty($mixedValues)) {
- $sLocation = implode("','", $mixedValues);
- $aSqlParts['join'] .= "LEFT JOIN `bx_persons_meta_locations` as `pdl` ON `pdl`.`object_id` = `pd`.`id`";
- $aSqlParts['where'] .= " AND `pdl`.`country` IN ('" . $sLocation . "')";
- continue;
- }
- if (is_array($mixedValues))
- $aSqlParts['where'] .= " AND `pd`.`{$sName}` IN ('" . implode("','", $mixedValues) . "')";
- else
- $aSqlParts['where'] .= " AND `pd`.`{$sName}` = '{$mixedValues}'";
- }
- }
- bx_alert($this->_oConfig->getObject('alert'), 'broadcast_criteria_after', 0, 0, [
- 'fields' => $aFields,
- 'sql' => &$aSqlParts
- ]);
- return $aSqlParts;
- }
- public function getProfilesByCriteria($aFields, $aAddons = [])
- {
- return $this->_getProfilesByCriteria($aFields, $aAddons, false);
- }
- public function getProfilesCountByCriteria($aFields, $aAddons = [])
- {
- return $this->_getProfilesByCriteria($aFields, $aAddons, true);
- }
- private function _getProfilesByCriteria($aFields, $aAddons = [], $bCountOnly = false)
- {
- if (empty($aFields) && empty($aAddons)) {
- return $bCountOnly ? 0 : [];
- }
- $aExcludeProfiles = $aAddons['exclude_profiles'] ?? [];
- $aIncludeProfiles = $aAddons['include_profiles'] ?? [];
- $aQueries = [];
- if (!empty($aFields) || empty($aIncludeProfiles)) {
- $aSqlParts = $this->getProfilesByCriteriaSqlParts($aFields);
- $sWhereClause = !empty($aSqlParts['where']) ? $aSqlParts['where'] : '';
- if (!empty($aExcludeProfiles)) {
- $sWhereClause .= " AND `sys_profiles`.`id` NOT IN (" . implode(',', $aExcludeProfiles) . ")";
- }
- $aQueries[] = "SELECT DISTINCT `sys_profiles`.`id` FROM `sys_profiles`" . $aSqlParts['join'] . " WHERE 1" . $sWhereClause;
- }
- if (!empty($aIncludeProfiles)) {
- $sExcludeClause = '';
- if (!empty($aExcludeProfiles)) {
- $sExcludeClause = " AND `sys_profiles`.`id` NOT IN (" . implode(',', $aExcludeProfiles) . ")";
- }
- $sQueriesParts = implode(',', $aIncludeProfiles);
- $aQueries[] = "SELECT `sys_profiles`.`id` FROM `sys_profiles` WHERE `sys_profiles`.`id` IN ({$sQueriesParts})" . $sExcludeClause;
- }
- $sSql = implode(" UNION ", $aQueries);
- if ($bCountOnly) {
- $sSql = "SELECT COUNT(*) FROM ({$sSql}) AS `t`";
- return (int)$this->getOne($this->prepare($sSql));
- } else {
- return $this->getColumn($this->prepare($sSql));
- }
- }
- function createBroadcastUsers($iConvoId, &$aData)
- {
- $sBegin = "REPLACE INTO `{$this->CNF['TABLE_MASS_TRACKER']}`
- (`{$this->CNF['FIELD_MASS_CONVO_ID']}`, `{$this->CNF['FIELD_MASS_USER_ID']}`)
- VALUES ";
- $bExecuted = false;
- $aProfileChunks = array_chunk($aData, 200);
- foreach ($aProfileChunks as &$aChunk) {
- $aValues = array_map(function ($iProfileId) use ($iConvoId) {
- return "($iConvoId, $iProfileId)";
- }, $aChunk);
- if (!empty($aValues)) {
- $sBegin . implode(",", $aValues);
- $bExecuted |= (bool) $this->query($sBegin . implode(",", $aValues));
- }
- };
- return $bExecuted;
- }
- function getBroadcastParticipants($iConvoId, $iLimit = 0, $iAfterUserId = 0)
- {
- $aBindings = ['id' => $iConvoId];
- $sWhere = "WHERE `{$this->CNF['FIELD_MASS_CONVO_ID']}`=:id";
- if ((int) $iAfterUserId > 0) {
- $aBindings['after_user_id'] = (int) $iAfterUserId;
- $sWhere .= " AND `{$this->CNF['FIELD_MASS_USER_ID']}` > :after_user_id";
- }
- $sLimit = (int) $iLimit > 0 ? " LIMIT " . (int) $iLimit : "";
- return $this->getColumn("SELECT `{$this->CNF['FIELD_MASS_USER_ID']}`
- FROM `{$this->CNF['TABLE_MASS_TRACKER']}`
- {$sWhere}
- ORDER BY `{$this->CNF['FIELD_MASS_USER_ID']}` ASC{$sLimit}", $aBindings);
- }
- function isBroadcastParticipant($iConvoId, $iProfileId)
- {
- return $this->getOne("SELECT COUNT(*)
- FROM `{$this->CNF['TABLE_MASS_TRACKER']}`
- WHERE `{$this->CNF['FIELD_MASS_CONVO_ID']}`=:id AND `{$this->CNF['FIELD_MASS_USER_ID']}`=:user_id", ['id' => $iConvoId, 'user_id' => $iProfileId]) != 0;
- }
- function getNotificationsSettings($sGroup)
- {
- if (!$this->isModuleByName('bx_notifications'))
- return false;
- return $this->getPairs("SELECT `delivery`, `active` FROM `bx_notifications_settings` WHERE `group`=:group", 'delivery', 'active', ['group' => $sGroup]);
- }
- /**
- * For API only.
- */
- public function getJotsByLotIdApi($aData)
- {
- $sMode = isset($aData['mode']) ? $aData['mode'] : 'new';
- $iLot = isset($aData['lot']) ? (int) $aData['lot'] : BX_IM_EMPTY;
- $iStart = isset($aData['start']) ? (int) $aData['start'] : BX_IM_EMPTY;
- $iLimit = isset($aData['limit']) ? (int) $aData['limit'] : BX_IM_EMPTY;
- $sArea = isset($aData['area']) ? $aData['area'] : '';
- $aBindings = [
- 'lot' => (int) $iLot,
- 'start' => (int) $iStart,
- 'limit' => (int) $iLimit
- ];
- $sWhere = $sLimit = '';
- $sWhere = "AND `m`.`{$this->CNF['FIELD_MESSAGE_FK']}` = :lot ";
- $sJoinArea = $sWhereArea = '';
- if ($sArea)
- list($sJoinArea, $sWhereArea) = $this->getJotsByArea($sArea);
- $sQuery = "SELECT `m`.* FROM `{$this->CNF['TABLE_MESSAGES']}` as `m` {$sJoinArea} WHERE 1 {$sWhere} {$sWhereArea} ORDER BY `m`.`{$this->CNF['FIELD_MESSAGE_ID']}` DESC LIMIT :start, :limit";
- return $this->getAll("({$sQuery}) ORDER BY `{$this->CNF['FIELD_MESSAGE_ID']}`", $aBindings);
- }
- public function getVideoConferenceInfoByRoom($sRoom, $iLimit = 0)
- {
- if (!$sRoom)
- return [];
- $sLimit = '';
- $aParams = ['room' => $sRoom];
- if ($iLimit && is_numeric($iLimit)) {
- $limit = (int) $iLimit;
- if ($limit > 0) {
- $sLimit = "LIMIT {$limit}";
- }
- }
- return $this->getAll("SELECT `v`.`{$this->CNF['FJVC_ID']}`,
- `v`.`{$this->CNF['FJVC_LOT_ID']}`,`v`.`{$this->CNF['FJVC_ACTIVE']}`,
- `vt`.`{$this->CNF['FJVCT_ID']}` as `conferance_id`,
- `vt`.`{$this->CNF['FJVCT_START']}`,`vt`.`{$this->CNF['FJVCT_END']}`,
- `vt`. `{$this->CNF['FJVCT_PART']}`, `j`.`{$this->CNF['FIELD_MESSAGE_ID']}` as `jot_id`
- FROM `{$this->CNF['TABLE_JVC']}` as `v`
- INNER JOIN `{$this->CNF['TABLE_JVCT']}` as `vt` ON `v`.`{$this->CNF['FJVC_ID']}`=`vt`.`{$this->CNF['FJVCT_FK']}`
- LEFT JOIN `{$this->CNF['TABLE_MESSAGES']}` as `j` ON `j`.`{$this->CNF['FIELD_MESSAGE_VIDEOC']}` = `vt`.`{$this->CNF['FJVCT_ID']}`
- WHERE `v`.`{$this->CNF['FJVC_ROOM']}`=:room
- ORDER BY `vt`.`{$this->CNF['FJVCT_ID']}` DESC {$sLimit}", $aParams);
- }
- }
- /** @} */
|