21 $this->sAlbumTable =
'sys_albums';
22 $this->sAlbumObjectsTable =
'sys_albums_objects';
23 $this->aAlbumFields = array(
37 $this->sAlbumCoverParam =
'sys_make_album_cover_last';
42 $aReplacement = array(
47 return str_replace(array_keys($aReplacement), array_values($aReplacement),
$sName);
60 if (in_array($sKey, $this->aAlbumFields) && ($bUseEmptyValues || strlen($sValue))) {
72 $sqlBody .=
"`{$this->sAlbumTable}`.`{$sKey}` = '$sValue'" . $sBound;
76 return trim($sqlBody, $sBound);
86 if (isset($aData[
'allow_view']) && is_array($aData[
'allow_view'])) {
87 $aRes[
'vis'] =
" AND `{$this->sAlbumTable}`.`AllowAlbumView` IN (" . implode(
',',
88 $aData[
'allow_view']) .
")";
90 if (isset($aData[
'hide_default']) && $aData[
'hide_default'] ===
true) {
91 $aRes[
'def'] =
" AND `{$this->sAlbumTable}`.`AllowAlbumView` <> " .
CH_WSB_PG_HIDDEN;
93 if (!isset($aData[
'show_empty']) || $aData[
'show_empty'] ===
false) {
94 if (!isset($aData[
'obj_count'][
'min'])) {
95 $aData[
'obj_count'][
'min'] = 0;
98 if (isset($aData[
'obj_count'])) {
99 if (!is_array($aData[
'obj_count'])) {
100 $aData[
'obj_count'][
'min'] = (int)$aData[
'obj_count'];
102 $sqlObjMain =
" AND `{$this->sAlbumTable}`.`ObjCount`";
103 $sqlMin = isset($aData[
'obj_count'][
'min']) ?
"$sqlObjMain > " . (int)$aData[
'obj_count'][
'min'] :
"";
104 $sqlMax = isset($aData[
'obj_count'][
'max']) ?
"$sqlObjMain < " . (int)$aData[
'obj_count'][
'max'] :
"";
105 $aRes[
'empty'] =
"$sqlObjMain $sqlMin $sqlMax";
112 function addAlbum($aData = array(), $bCheck =
true)
116 $aData[
'caption'] = str_replace(
'\\',
'', $aData[
'caption']);
124 $iOwner = (int)$aData[
'owner'];
126 if (isset($aData[
'AllowAlbumView'])) {
127 $iAllowAlbumView = (int)$aData[
'AllowAlbumView'];
128 } elseif (strpos($aData[
'caption'],
getUsername($iOwner)) !==
false) {
131 $iAllowAlbumView = $oPrivacy->getDefaultValueModule(str_replace(
'ch_',
'', $this->sType),
'album_view');
132 if (!$iAllowAlbumView) {
139 $sUri = $this->
getCorrectUri($aData[
'caption'], $iOwner, $bCheck);
140 $GLOBALS[
'MySQL']->res(
"INSERT INTO `{$this->sAlbumTable}` SET " . $this->
_getSqlPart(array(
141 'Caption' => $aData[
'caption'],
143 'Location' => $aData[
'location'],
144 'Description' => $aData[
'description'],
145 'AllowAlbumView' => $iAllowAlbumView,
146 'Type' => $this->sType,
148 'Status' =>
'active',
150 'LastObjId' => isset($aData[
'lastObjId']) ? (
int)$aData[
'last_obj'] : 0
153 $iResult =
$GLOBALS[
'MySQL']->lastId();
155 $oAlert =
new ChWsbAlerts(
'album',
'add', $iResult, $iOwner, array(
'Type' => $this->sType,
'Uri' => $sUri));
158 $oAlert =
new ChWsbAlerts($this->sType,
'addAlbum', $iResult, $iOwner, array(
'Uri' => $sUri));
180 $sUri .=
'-' . date(
'Y-m-d');
184 for ($i = 0; $i < 999; ++$i) {
186 return ($sUri .
'-' . $i);
198 return !
$GLOBALS[
'MySQL']->getRow(
"SELECT 1 FROM $this->sAlbumTable WHERE `Uri` = ? AND `Owner` = ? AND `Type` = ? LIMIT 1",
205 $iOwner = (int)$aData[
'Owner'] ? (
int)$aData[
'Owner'] :
$this->iOwnerId;
206 $aAlbum = $this->
getAlbumInfo(array(
'fileUri' => $sUri,
'owner' => $iOwner), array(
'ID',
'Uri',
'Type'));
214 $iOwner = (int)$aData[
'Owner'] ? (
int)$aData[
'Owner'] :
$this->iOwnerId;
215 $aAlbum = $this->
getAlbumInfo(array(
'fileid' =>
$iId,
'owner' => $iOwner), array(
'ID',
'Uri',
'Type'));
222 if(
empty($aAlbum) || !is_array($aAlbum))
225 $sSetClause = $this->
_getSqlPart($aData,
', ',
true);
226 $sWhereClause =
"`ID` = '" . $aAlbum[
'ID'] .
"'";
228 $mixedResult =
$GLOBALS[
'MySQL']->res(
"UPDATE `{$this->sAlbumTable}` SET " . $sSetClause .
" WHERE " . $sWhereClause .
" LIMIT 1");
229 if((
int)$mixedResult > 0) {
232 $oAlert =
new ChWsbAlerts(
'album',
'change', $aAlbum[
'ID'], $iUserId, array(
'Type' => $aAlbum[
'Type'],
'Uri' => $aAlbum[
'Uri']));
235 $oAlert =
new ChWsbAlerts($aAlbum[
'Type'],
'changeAlbum', $aAlbum[
'ID'], $iUserId, array(
'Uri' => $aAlbum[
'Uri']));
244 $iAlbumId = (int)$iAlbumId;
249 $aAlbum = $this->
getAlbumInfo(array(
'fileid' => $iAlbumId), array(
'ID',
'Uri',
'Type'));
250 if(
empty($aAlbum) || !is_array($aAlbum))
253 $mixedResult =
$GLOBALS[
'MySQL']->res(
"DELETE FROM `{$this->sAlbumTable}` WHERE `ID`='" . $iAlbumId .
"'");
254 if((
int)$mixedResult > 0) {
257 $oAlert =
new ChWsbAlerts(
'album',
'delete', $iAlbumId, $iUserId, array(
'Type' => $aAlbum[
'Type'],
'Uri' => $aAlbum[
'Uri']));
260 $oAlert =
new ChWsbAlerts($aAlbum[
'Type'],
'deleteAlbum', $iAlbumId, $iUserId, array(
'Uri' => $aAlbum[
'Uri']));
270 'Caption' => $aData[
'caption'],
271 'Type' => $this->sType,
272 'Owner' => (
int)$aData[
'owner'],
275 $sqlQuery =
"SELECT `ID` FROM {$this->sAlbumTable} WHERE $sqlBody";
277 return (
int)
$GLOBALS[
'MySQL']->getOne($sqlQuery);
283 if ((
int)$mixedAlbum > 0) {
284 $sqlWhere =
"`id_album`='" . (int)$mixedAlbum .
"'";
286 $sqlJoin =
"LEFT JOIN `sys_albums` ON `sys_albums_objects`.`id_album` = `sys_albums`.`ID`";
289 $sqlQuery =
"SELECT `id_object` FROM `{$this->sAlbumObjectsTable}` $sqlJoin WHERE $sqlWhere";
291 return $GLOBALS[
'MySQL']->getPairs($sqlQuery,
'id_object',
'id_object');
296 $iAlbumId = (int)$iAlbumId;
297 $iLimit = (int)$iLimit;
298 $sqlWhere =
"`id_album`='$iAlbumId'";
300 if (is_array($aJoin)) {
301 $sqlJoin =
"INNER JOIN `{$aJoin['table']}` ON `{$aJoin['table']}`.`{$aJoin['field']}`=`{$this->sAlbumObjectsTable}`.`id_object`";
302 if (is_array($aJoinCond)) {
303 foreach ($aJoinCond
as $aValue) {
304 $sqlWhere .=
" AND `{$aJoin['table']}`.`{$aValue['field']}`='{$aValue['value']}'";
307 if (is_array($aJoin[
'fields_list'])) {
308 $sqlAddFields =
", `" . implode(
"`, `", $aJoin[
'fields_list']) .
"`";
311 $sqlQuery =
"SELECT `id_object` $sqlAddFields FROM `{$this->sAlbumObjectsTable}` $sqlJoin WHERE $sqlWhere ORDER BY `obj_order`, `id_object` DESC LIMIT $iLimit";
313 return $GLOBALS[
'MySQL']->getAll($sqlQuery);
319 'Type' => $this->sType,
320 'Status' => !isset($aData[
'status']) ?
'active' : $aData[
'status'],
321 'Caption' => isset($aData[
'caption']) ? $aData[
'caption'] :
'',
326 if (isset($aData[
'owner']) && strlen($aData[
'owner']) > 0) {
327 if ((
int)$aData[
'owner'] == 0) {
328 $iUserId =
getID($aData[
'owner']);
329 $aFields[
'Owner'] = $iUserId > 0 ? $iUserId :
'';
331 $aFields[
'Owner'] = (int)$aData[
'owner'];
353 if (isset($aData[
'ownerStatus'])) {
354 $sqlJoin =
"LEFT JOIN `Profiles` ON `Profiles`.`ID`=`{$this->sAlbumTable}`.`Owner`";
355 $sqlJoinWhere =
"AND `Profiles`.`Status` ";
356 if (is_array($aData[
'ownerStatus'])) {
357 $sqlJoinWhere .=
"NOT IN ('" . implode(
"','", $aData[
'ownerStatus']) .
"')";
359 $sqlJoinWhere .=
"<> '{$aData['ownerStatus']}'";
363 $sqlBegin =
"SELECT `{$this->sAlbumTable}`.* FROM `{$this->sAlbumTable}` $sqlJoin";
365 $sqlOrder =
"ORDER BY `{$this->sAlbumTable}`.`Date` DESC";
366 $sqlQuery =
"$sqlBegin $sqlCond {$aSqlSpec['vis']} {$aSqlSpec['def']} {$aSqlSpec['empty']} $sqlJoinWhere $sqlOrder $sqlLimit";
368 return $GLOBALS[
'MySQL']->getAll($sqlQuery);
377 'caption' =>
_t(
'_sys_album_select_friends'),
378 'AllowAlbumView' => 5,
385 'caption' =>
_t(
'_sys_album_select_private'),
386 'AllowAlbumView' => 2,
393 'Type' => $this->sType,
394 'Status' => !isset($aData[
'status']) ?
'active' : $aData[
'status'],
396 if (isset($aData[
'owner']) && strlen($aData[
'owner']) > 0) {
397 if ((
int)$aData[
'owner'] == 0) {
398 $iUserId =
getID($aData[
'owner']);
399 $aFields[
'Owner'] = $iUserId > 0 ? $iUserId :
'';
401 $aFields[
'Owner'] = (int)$aData[
'owner'];
408 if (isset($aData[
'ownerStatus'])) {
409 $sqlJoin =
"LEFT JOIN `Profiles` ON `Profiles`.`ID`=`{$this->sAlbumTable}`.`Owner`";
410 $sqlJoinWhere =
"AND `Profiles`.`Status` ";
411 if (is_array($aData[
'ownerStatus'])) {
412 $sqlJoinWhere .=
"NOT IN ('" . implode(
"','", $aData[
'ownerStatus']) .
"')";
414 $sqlJoinWhere .=
"<> '{$aData['ownerStatus']}'";
418 $sqlBegin =
"SELECT COUNT(*) FROM `{$this->sAlbumTable}` $sqlJoin";
420 $sqlQuery =
"$sqlBegin $sqlCond {$aSqlSpec['vis']} {$aSqlSpec['def']} {$aSqlSpec['empty']} $sqlJoinWhere";
422 return $GLOBALS[
'MySQL']->getOne($sqlQuery);
427 $sqlCondition =
"`{$this->sAlbumTable}`.`Type`= ?";
431 foreach ($aIdent
as $sKey => $sValue) {
432 switch (strtolower($sKey)) {
434 $aParams[
'Uri'] = $sValue;
437 $aParams[
'ID'] = (int)$sValue;
440 $aParams[
'Owner'] = (int)$sValue;
443 $aParams[$sKey] = $sValue;
450 $sqlCondition = $this->
_getSqlPart($aParams,
' AND ');
452 if (in_array($sValue, $this->aAlbumFields)) {
453 $sqlFields .=
"`{$this->sAlbumTable}`.`$sValue`, ";
456 $sqlFields = trim($sqlFields,
', ');
457 $sqlQuery =
"SELECT $sqlFields FROM `{$this->sAlbumTable}` WHERE $sqlCondition LIMIT 1";
459 return $GLOBALS[
'MySQL']->getRow($sqlQuery);
464 $aValue = $this->
getAlbumInfo(array(
'fileId' => (
int)$iAlbumId), array(
'Caption'));
466 return $aValue[
'Caption'];
471 return getParam(
'sys_album_default_name');
475 function addObject($iAlbumId, $mixedObj, $bUpdateCount =
true)
477 $iAlbumId = (int)$iAlbumId;
478 if ($iAlbumId == 0) {
481 $sqlFields =
"`id_album`, `id_object`, `obj_order`";
485 if (is_array($mixedObj)) {
486 foreach ($mixedObj
as $iValue) {
487 $iValue = (int)$iValue;
488 $sqlBody .=
"('$iAlbumId', '$iValue', '$iValue'), ";
492 $iValue = (int)$mixedObj;
493 $sqlBody =
"('$iAlbumId', '$iValue', '$iValue')";
496 $sqlQuery =
"INSERT INTO `{$this->sAlbumObjectsTable}` ($sqlFields) VALUES " . trim($sqlBody,
', ');
497 $iRes =
$GLOBALS[
'MySQL']->query($sqlQuery);
501 if ($iLastObjId == 0) {
503 } elseif ($iLastObjId != 0 &&
getParam($this->sAlbumCoverParam) ==
'on') {
513 $iAlbumId = (int)$iAlbumId;
514 $iNewAlbumId = (int)$iNewAlbumId;
517 if (!
empty($mixedObj)) {
519 if (is_array($mixedObj)) {
520 if (in_array($iLastObjId, $mixedObj)) {
521 $bUpdateLastObj =
true;
523 foreach ($mixedObj
as $iValue) {
524 $iValue = (int)$iValue;
525 $sqlBody .=
"'$iValue', ";
529 $iValue = (int)$mixedObj;
530 $sqlBody =
"'$iValue'";
532 if ($iValue == $iLastObjId) {
533 $bUpdateLastObj =
true;
536 $sqlQuery =
"UPDATE `{$this->sAlbumObjectsTable}`, `{$this->sAlbumTable}`
537 SET `{$this->sAlbumObjectsTable}`.`id_album` = $iNewAlbumId
538 WHERE `{$this->sAlbumObjectsTable}`.`id_album`=`{$this->sAlbumTable}`.`ID`
539 AND `{$this->sAlbumTable}`.`Type` = '$this->sType' AND `{$this->sAlbumObjectsTable}`.`id_object` IN (" . trim($sqlBody,
542 if ($bUpdateLastObj) {
547 $sqlQuery =
"UPDATE `{$this->sAlbumTable}` SET `ObjCount` = CASE
548 WHEN `ID`={$iAlbumId} THEN `ObjCount`-$iCount
549 WHEN `ID`={$iNewAlbumId} THEN `ObjCount`+$iCount END
550 WHERE `ID` IN ($iAlbumId, $iNewAlbumId)";
557 $iAlbumId = (int)$iAlbumId;
561 if (!
empty($mixedObj)) {
562 if (is_array($mixedObj)) {
563 if (in_array($iLastObjId, $mixedObj)) {
564 $bUpdateLastObj =
true;
566 foreach ($mixedObj
as $iValue) {
567 $iValue = (int)$iValue;
568 $sqlBody .=
"'$iValue', ";
572 $iValue = (int)$mixedObj;
573 $sqlBody =
"'$iValue'";
574 if ($iValue == $iLastObjId) {
575 $bUpdateLastObj =
true;
579 $sqlQuery =
"DELETE `{$this->sAlbumObjectsTable}`
580 FROM `{$this->sAlbumObjectsTable}`, `{$this->sAlbumTable}`
581 WHERE `{$this->sAlbumObjectsTable}`.`id_album`=`{$this->sAlbumTable}`.`ID`
582 AND `{$this->sAlbumTable}`.`Type` = '$this->sType' AND `{$this->sAlbumObjectsTable}`.`id_object` IN (" . trim($sqlBody,
585 if ($bUpdateLastObj) {
597 $sqlQuery =
"SELECT `id_album` as `ID`, `ObjCount`, `LastObjId`
598 FROM `{$this->sAlbumObjectsTable}`
599 LEFT JOIN `{$this->sAlbumTable}` ON `{$this->sAlbumTable}`.`ID` = `{$this->sAlbumObjectsTable}`.`id_album`
600 WHERE `id_object` = ? AND `$this->sAlbumTable`.`Type` = ?";
601 $aInfo =
$GLOBALS[
'MySQL']->getRow($sqlQuery, [$iObj, $this->sType]);
602 $sqlDelete =
"DELETE FROM `{$this->sAlbumObjectsTable}` WHERE `id_album`='{$aInfo['ID']}' AND `id_object`='$iObj' LIMIT 1";
604 if (
$aInfo[
'ObjCount'] > 0 && $bUpdateCounter) {
607 if (
$aInfo[
'LastObjId'] == $iObj) {
614 $aAlbumInfo = $this->
getAlbumInfo(array(
'fileUri' => $sAlbumUri,
'owner' => $this->iOwnerId), array(
'ID'));
615 $sqlBegin =
"UPDATE `{$this->sAlbumObjectsTable}` SET `obj_order` = ";
616 $sqlAlbumPart =
" `id_album`='{$aAlbumInfo['ID']}'";
617 $iNum = is_array($aSort) ? count($aSort) : 0;
619 $sqlBegin .=
" CASE ";
620 $sqlWhere =
"WHERE `id_object` IN (";
621 for ($i = 0; $i < $iNum; $i++) {
622 $iElem = (int)$aSort[$i];
623 $sqlBegin .=
" WHEN `id_object`='$iElem' THEN '$i'";
624 $sqlWhere .=
"$iElem, ";
626 $sqlQuery = $sqlBegin .
" END " . trim($sqlWhere,
', ') .
") AND $sqlAlbumPart";
628 $sqlQuery = $sqlBegin .
" `id_object` WHERE $sqlAlbumPart";
630 $GLOBALS[
'MySQL']->query($sqlQuery);
635 $iAlbumId = (int)$iAlbumId;
636 $iObjId = (int)$iObjId;
638 $sqlQuery =
"SELECT MAX(`id_object`) FROM `{$this->sAlbumObjectsTable}` WHERE `id_album` = '{$iAlbumId}'";
641 $sqlQuery =
"UPDATE `{$this->sAlbumTable}` SET `LastObjId`='$iObjId' WHERE `ID`='{$iAlbumId}'";
643 return $GLOBALS[
'MySQL']->query($sqlQuery);
648 $iObjId = (int)$iObjId;
649 $sqlQuery =
"UPDATE `{$this->sAlbumTable}`, `{$this->sAlbumObjectsTable}`
650 SET `LastObjId` = $iObjId
651 WHERE `{$this->sAlbumTable}`.`ID`=`{$this->sAlbumObjectsTable}`.`id_album`
652 AND `{$this->sAlbumObjectsTable}`.`id_object`=$iObjId
653 AND `{$this->sAlbumTable}`.`Type`='{$this->sType}'";
655 return $GLOBALS[
'MySQL']->query($sqlQuery);
660 $iAlbumId = (int)$iAlbumId;
661 $sqlQuery =
"SELECT `LastObjId` FROM `{$this->sAlbumTable}` WHERE `ID`='{$iAlbumId}' AND `Type`='{$this->sType}'";
663 return $GLOBALS[
'MySQL']->getOne($sqlQuery);
669 $iAlbumId = (int)$iAlbumId;
670 $iObjectId = (int)$iObjectId;
671 $iOrder = (int)$iOrder;
675 $sqlCheck =
"SELECT COUNT(*) FROM `$this->sAlbumObjectsTable` WHERE `id_album`=$iAlbumId AND `obj_order`>0";
677 $bOrder = $iCheck > 0 ?
true :
false;
680 if ($iOrder == 0 && !$bOrder) {
681 $sqlField =
"id_object";
682 $sqlValue = $iObjectId;
685 $sqlField =
"obj_order";
699 if (is_array($aExcludeIds) && !
empty($aExcludeIds)) {
700 $sqlIds =
"AND `id_object` NOT IN ('" . implode(
"','", $aExcludeIds) .
"')";
703 $sqlQuery =
"SELECT `id_object` FROM `$this->sAlbumObjectsTable`
704 WHERE `id_album`=$iAlbumId AND `$sqlField`$sSign $sqlValue $sqlIds
705 ORDER BY `$sqlField` $sqlType LIMIT 1";
714 return $aInfo[
'ObjCount'];
720 'Type' => $this->sType,
721 'Status' => !isset($aData[
'status']) ?
'active' : $aData[
'status'],
723 if (isset($aData[
'owner'])) {
724 if ((
int)$aData[
'owner'] == 0) {
725 $iUserId =
getID($aData[
'owner']);
726 $aFields[
'Owner'] = $iUserId > 0 ? $iUserId :
'';
728 $aFields[
'Owner'] = (int)$aData[
'owner'];
731 $sqlQuery =
"SELECT SUM(`ObjCount`) FROM `{$this->sAlbumTable}` WHERE " . $this->
_getSqlPart(
$aFields,
' AND ');
733 return (
int)
$GLOBALS[
'MySQL']->getOne($sqlQuery);
738 $iAlbumId = (int)$iAlbumId;
739 $sqlQuery =
"SELECT COUNT(*) FROM `{$this->sAlbumObjectsTable}` WHERE `id_album`='$iAlbumId'";
741 return $GLOBALS[
'MySQL']->getOne($sqlQuery);
746 $iAlbumId = (int)$iAlbumId;
747 $iNumber = (int)$iNumber;
748 $sOperator = $bIncrease ?
'+' :
'-';
749 $sqlQuery =
"UPDATE `{$this->sAlbumTable}` SET `ObjCount`=`ObjCount` $sOperator $iNumber WHERE `ID`='{$iAlbumId}'";
755 $iObjId = (int)$iObjId;
756 $sOperator = $bIncrease ?
'+' :
'-';
757 $sqlQuery =
"UPDATE `{$this->sAlbumTable}`, `{$this->sAlbumObjectsTable}`
758 SET `ObjCount` = `ObjCount` $sOperator 1
759 WHERE `{$this->sAlbumTable}`.`ID`=`{$this->sAlbumObjectsTable}`.`id_album`
760 AND `{$this->sAlbumObjectsTable}`.`id_object`=$iObjId
761 AND `{$this->sAlbumTable}`.`Type`='{$this->sType}'";