| Total Complexity | 114 |
| Total Lines | 678 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PublisherItemHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PublisherItemHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 973 | class PublisherItemHandler extends XoopsPersistableObjectHandler |
||
| 974 | { |
||
| 975 | /** |
||
| 976 | * @var Publisher |
||
| 977 | * @access public |
||
| 978 | */ |
||
| 979 | public $publisher = null; |
||
| 980 | |||
| 981 | /** |
||
| 982 | * @param Connection $db |
||
| 983 | */ |
||
| 984 | public function __construct(Connection $db) |
||
| 985 | { |
||
| 986 | parent::__construct($db, "publisher_items", 'PublisherItem', "itemid", "title"); |
||
| 987 | $this->publisher = Publisher::getInstance(); |
||
| 988 | } |
||
| 989 | |||
| 990 | /** |
||
| 991 | * insert a new item in the database |
||
| 992 | * |
||
| 993 | * @param XoopsObject $item reference to the {@link PublisherItem} object |
||
| 994 | * @param bool $force |
||
| 995 | * |
||
| 996 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 997 | */ |
||
| 998 | public function insert(XoopsObject $item, $force = true) |
||
| 999 | { |
||
| 1000 | $xoops = Xoops::getInstance(); |
||
| 1001 | if (!$item->getVar('meta_keywords') || !$item->getVar('meta_description') || !$item->getVar('short_url')) { |
||
| 1002 | $publisher_metagen = new PublisherMetagen($item->title(), $item->getVar('meta_keywords'), $item->getVar('summary')); |
||
| 1003 | // Auto create meta tags if empty |
||
| 1004 | if (!$item->getVar('meta_keywords')) { |
||
| 1005 | $item->setVar('meta_keywords', $publisher_metagen->_keywords); |
||
| 1006 | } |
||
| 1007 | if (!$item->getVar('meta_description')) { |
||
| 1008 | $item->setVar('meta_description', $publisher_metagen->_description); |
||
| 1009 | } |
||
| 1010 | // Auto create short_url if empty |
||
| 1011 | if (!$item->getVar('short_url')) { |
||
| 1012 | $item->setVar('short_url', $publisher_metagen->generateSeoTitle($item->getVar('title', 'n'), false)); |
||
| 1013 | } |
||
| 1014 | } |
||
| 1015 | if (!parent::insert($item, $force)) { |
||
| 1016 | return false; |
||
| 1017 | } |
||
| 1018 | if ($xoops->isActiveModule('tag')) { |
||
| 1019 | // Storing tags information |
||
| 1020 | $tag_handler = $xoops->getModuleHandler('tag', 'tag'); |
||
| 1021 | $tag_handler->updateByItem($item->getVar('item_tag'), $item->getVar('itemid'), PUBLISHER_DIRNAME, 0); |
||
| 1022 | } |
||
| 1023 | return true; |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | /** |
||
| 1027 | * delete an item from the database |
||
| 1028 | * |
||
| 1029 | * @param XoopsObject $item reference to the ITEM to delete |
||
| 1030 | * @param bool $force |
||
| 1031 | * |
||
| 1032 | * @return bool FALSE if failed. |
||
| 1033 | */ |
||
| 1034 | public function delete(XoopsObject $item, $force = false) |
||
| 1035 | { |
||
| 1036 | $xoops = Xoops::getInstance(); |
||
| 1037 | // Deleting the files |
||
| 1038 | if (!$this->publisher->getFileHandler()->deleteItemFiles($item)) { |
||
| 1039 | $item->setErrors('An error while deleting a file.'); |
||
| 1040 | } |
||
| 1041 | if (!parent::delete($item, $force)) { |
||
| 1042 | $item->setErrors('An error while deleting.'); |
||
| 1043 | return false; |
||
| 1044 | } |
||
| 1045 | // Removing tags information |
||
| 1046 | if ($xoops->isActiveModule('tag')) { |
||
| 1047 | $tag_handler = $xoops->getModuleHandler('tag', 'tag'); |
||
| 1048 | $tag_handler->updateByItem('', $item->getVar('itemid'), PUBLISHER_DIRNAME, 0); |
||
| 1049 | } |
||
| 1050 | return true; |
||
| 1051 | } |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * retrieve items from the database |
||
| 1055 | * |
||
| 1056 | * @param object $criteria {@link CriteriaElement} conditions to be met |
||
| 1057 | * @param string $id_key what shall we use as array key ? none, itemid, categoryid |
||
| 1058 | * @param string $notNullFields fields that cannot be null or empty |
||
| 1059 | * |
||
| 1060 | * @return array array of {@link PublisherItem} objects |
||
| 1061 | */ |
||
| 1062 | public function getItemObjects($criteria = null, $id_key = 'none', $notNullFields = '') |
||
| 1063 | { |
||
| 1064 | $ret = array(); |
||
| 1065 | $whereMode = ''; |
||
| 1066 | |||
| 1067 | $qb = $this->db2->createXoopsQueryBuilder(); |
||
| 1068 | $qb ->select('*') |
||
| 1069 | ->fromPrefix('publisher_items', ''); |
||
| 1070 | if (isset($criteria) && is_subclass_of($criteria, 'Xoops\Core\Kernel\CriteriaElement')) { |
||
| 1071 | $criteria->renderQb($qb, ''); |
||
| 1072 | $whereMode = 'AND'; |
||
| 1073 | } |
||
| 1074 | $this->addNotNullFieldClause($qb, $notNullFields, $whereMode); |
||
| 1075 | $theObjects = array(); |
||
| 1076 | $result = $qb->execute(); |
||
| 1077 | while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) { |
||
| 1078 | $item = new PublisherItem(); |
||
| 1079 | $item->assignVars($myrow); |
||
| 1080 | $theObjects[$myrow['itemid']] = $item; |
||
| 1081 | unset($item); |
||
| 1082 | } |
||
| 1083 | |||
| 1084 | /* @var $theObject PublisherItem */ |
||
| 1085 | foreach ($theObjects as $theObject) { |
||
| 1086 | if ($id_key === 'none') { |
||
| 1087 | $ret[] = $theObject; |
||
| 1088 | } elseif ($id_key === 'itemid') { |
||
| 1089 | $ret[$theObject->getVar('itemid')] = $theObject; |
||
| 1090 | } else { |
||
| 1091 | $ret[$theObject->getVar($id_key)][$theObject->getVar('itemid')] = $theObject; |
||
| 1092 | } |
||
| 1093 | unset($theObject); |
||
| 1094 | } |
||
| 1095 | return $ret; |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | /** |
||
| 1099 | * count items matching a condition |
||
| 1100 | * |
||
| 1101 | * @param object $criteria {@link CriteriaElement} to match |
||
| 1102 | * @param string $notNullFields fields that cannot be null or empty |
||
| 1103 | * |
||
| 1104 | * @return int count of items |
||
| 1105 | */ |
||
| 1106 | public function getItemCount($criteria = null, $notNullFields = '') |
||
| 1107 | { |
||
| 1108 | $whereMode = ''; |
||
| 1109 | |||
| 1110 | $qb = $this->db2->createXoopsQueryBuilder(); |
||
| 1111 | $qb ->select('COUNT(*)') |
||
| 1112 | ->fromPrefix('publisher_items', ''); |
||
| 1113 | if (isset($criteria) && is_subclass_of($criteria, 'Xoops\Core\Kernel\CriteriaElement')) { |
||
| 1114 | $whereClause = $criteria->renderQb($qb, ''); |
||
| 1115 | $whereMode = 'AND'; |
||
| 1116 | } |
||
| 1117 | $this->addNotNullFieldClause($qb, $notNullFields, $whereMode); |
||
| 1118 | $result = $qb->execute(); |
||
| 1119 | |||
| 1120 | if (!$result) { |
||
| 1121 | return 0; |
||
| 1122 | } |
||
| 1123 | list($count) = $result->fetch(PDO::FETCH_NUM); |
||
| 1124 | return $count; |
||
| 1125 | } |
||
| 1126 | |||
| 1127 | /** |
||
| 1128 | * @param $categoryid |
||
| 1129 | * @param string $status |
||
| 1130 | * @param string $notNullFields |
||
| 1131 | * |
||
| 1132 | * @return int |
||
| 1133 | */ |
||
| 1134 | public function getItemsCount($categoryid = -1, $status = '', $notNullFields = '') |
||
| 1135 | { |
||
| 1136 | global $publisher_isAdmin; |
||
| 1137 | if (!$publisher_isAdmin) { |
||
| 1138 | $criteriaPermissions = new CriteriaCompo(); |
||
| 1139 | // Categories for which user has access |
||
| 1140 | $categoriesGranted = $this->publisher->getPermissionHandler()->getGrantedItems('category_read'); |
||
| 1141 | if (!empty($categoriesGranted)) { |
||
| 1142 | $grantedCategories = new Criteria('categoryid', "(" . implode(',', $categoriesGranted) . ")", 'IN'); |
||
| 1143 | $criteriaPermissions->add($grantedCategories, 'AND'); |
||
| 1144 | } else { |
||
| 1145 | return 0; |
||
| 1146 | } |
||
| 1147 | } |
||
| 1148 | if (isset($categoryid) && $categoryid != -1) { |
||
| 1149 | $criteriaCategory = new criteria('categoryid', $categoryid); |
||
| 1150 | } |
||
| 1151 | $criteriaStatus = new CriteriaCompo(); |
||
| 1152 | if (!empty($status) && is_array($status)) { |
||
| 1153 | foreach ($status as $v) { |
||
| 1154 | $criteriaStatus->add(new Criteria('status', $v), 'OR'); |
||
| 1155 | } |
||
| 1156 | } elseif (!empty($status) && $status != -1) { |
||
| 1157 | $criteriaStatus->add(new Criteria('status', $status), 'OR'); |
||
| 1158 | } |
||
| 1159 | $criteria = new CriteriaCompo(); |
||
| 1160 | if (!empty($criteriaCategory)) { |
||
| 1161 | $criteria->add($criteriaCategory); |
||
| 1162 | } |
||
| 1163 | if (!empty($criteriaPermissions)) { |
||
| 1164 | $criteria->add($criteriaPermissions); |
||
| 1165 | } |
||
| 1166 | if (!empty($criteriaStatus)) { |
||
| 1167 | $criteria->add($criteriaStatus); |
||
| 1168 | } |
||
| 1169 | return $this->getItemCount($criteria, $notNullFields); |
||
| 1170 | } |
||
| 1171 | |||
| 1172 | /** |
||
| 1173 | * @param int $limit |
||
| 1174 | * @param int $start |
||
| 1175 | * @param int $categoryid |
||
| 1176 | * @param string $sort |
||
| 1177 | * @param string $order |
||
| 1178 | * @param string $notNullFields |
||
| 1179 | * @param bool $asobject |
||
| 1180 | * @param string $id_key |
||
| 1181 | * |
||
| 1182 | * @return array |
||
| 1183 | */ |
||
| 1184 | public function getAllPublished($limit = 0, $start = 0, $categoryid = -1, $sort = 'datesub', $order = 'DESC', $notNullFields = '', $asobject = true, $id_key = 'none') |
||
| 1185 | { |
||
| 1186 | $otherCriteria = new Criteria('datesub', time(), '<='); |
||
| 1187 | return $this->getItems($limit, $start, array(_PUBLISHER_STATUS_PUBLISHED), $categoryid, $sort, $order, $notNullFields, $asobject, $otherCriteria, $id_key); |
||
| 1188 | } |
||
| 1189 | |||
| 1190 | /** |
||
| 1191 | * @param PublisherItem $obj |
||
| 1192 | * |
||
| 1193 | * @return bool |
||
| 1194 | */ |
||
| 1195 | public function getPreviousPublished($obj) |
||
| 1196 | { |
||
| 1197 | $ret = false; |
||
| 1198 | $otherCriteria = new CriteriaCompo(); |
||
| 1199 | $otherCriteria->add(new Criteria('datesub', $obj->getVar('datesub'), '<')); |
||
| 1200 | $objs = $this->getItems(1, 0, array(_PUBLISHER_STATUS_PUBLISHED), $obj->getVar('categoryid'), 'datesub', 'DESC', '', true, $otherCriteria, 'none'); |
||
| 1201 | if (count($objs) > 0) { |
||
| 1202 | $ret = $objs[0]; |
||
| 1203 | } |
||
| 1204 | return $ret; |
||
| 1205 | } |
||
| 1206 | |||
| 1207 | /** |
||
| 1208 | * @param PublisherItem $obj |
||
| 1209 | * |
||
| 1210 | * @return bool |
||
| 1211 | */ |
||
| 1212 | public function getNextPublished($obj) |
||
| 1213 | { |
||
| 1214 | $ret = false; |
||
| 1215 | $otherCriteria = new CriteriaCompo(); |
||
| 1216 | $otherCriteria->add(new Criteria('datesub', $obj->getVar('datesub'), '>')); |
||
| 1217 | $otherCriteria->add(new Criteria('datesub', time(), '<=')); |
||
| 1218 | $objs = $this->getItems(1, 0, array(_PUBLISHER_STATUS_PUBLISHED), $obj->getVar('categoryid'), 'datesub', 'ASC', '', true, $otherCriteria, 'none'); |
||
| 1219 | if (count($objs) > 0) { |
||
| 1220 | $ret = $objs[0]; |
||
| 1221 | } |
||
| 1222 | return $ret; |
||
| 1223 | } |
||
| 1224 | |||
| 1225 | /** |
||
| 1226 | * @param int $limit |
||
| 1227 | * @param int $start |
||
| 1228 | * @param int $categoryid |
||
| 1229 | * @param string $sort |
||
| 1230 | * @param string $order |
||
| 1231 | * @param string $notNullFields |
||
| 1232 | * @param bool $asobject |
||
| 1233 | * @param string $id_key |
||
| 1234 | * |
||
| 1235 | * @return array |
||
| 1236 | */ |
||
| 1237 | public function getAllSubmitted($limit = 0, $start = 0, $categoryid = -1, $sort = 'datesub', $order = 'DESC', $notNullFields = '', $asobject = true, $id_key = 'none') |
||
| 1238 | { |
||
| 1239 | return $this->getItems($limit, $start, array(_PUBLISHER_STATUS_SUBMITTED), $categoryid, $sort, $order, $notNullFields, $asobject, null, $id_key); |
||
| 1240 | } |
||
| 1241 | |||
| 1242 | /** |
||
| 1243 | * @param int $limit |
||
| 1244 | * @param int $start |
||
| 1245 | * @param int $categoryid |
||
| 1246 | * @param string $sort |
||
| 1247 | * @param string $order |
||
| 1248 | * @param string $notNullFields |
||
| 1249 | * @param bool $asobject |
||
| 1250 | * @param string $id_key |
||
| 1251 | * |
||
| 1252 | * @return array |
||
| 1253 | */ |
||
| 1254 | public function getAllOffline($limit = 0, $start = 0, $categoryid = -1, $sort = 'datesub', $order = 'DESC', $notNullFields = '', $asobject = true, $id_key = 'none') |
||
| 1255 | { |
||
| 1256 | return $this->getItems($limit, $start, array(_PUBLISHER_STATUS_OFFLINE), $categoryid, $sort, $order, $notNullFields, $asobject, null, $id_key); |
||
| 1257 | } |
||
| 1258 | |||
| 1259 | /** |
||
| 1260 | * @param int $limit |
||
| 1261 | * @param int $start |
||
| 1262 | * @param int $categoryid |
||
| 1263 | * @param string $sort |
||
| 1264 | * @param string $order |
||
| 1265 | * @param string $notNullFields |
||
| 1266 | * @param bool $asobject |
||
| 1267 | * @param string $id_key |
||
| 1268 | * |
||
| 1269 | * @return array |
||
| 1270 | */ |
||
| 1271 | public function getAllRejected($limit = 0, $start = 0, $categoryid = -1, $sort = 'datesub', $order = 'DESC', $notNullFields = '', $asobject = true, $id_key = 'none') |
||
| 1272 | { |
||
| 1273 | return $this->getItems($limit, $start, array(_PUBLISHER_STATUS_REJECTED), $categoryid, $sort, $order, $notNullFields, $asobject, null, $id_key); |
||
| 1274 | } |
||
| 1275 | |||
| 1276 | /** |
||
| 1277 | * @param int $limit |
||
| 1278 | * @param int $start |
||
| 1279 | * @param string $status |
||
| 1280 | * @param int $categoryid |
||
| 1281 | * @param string $sort |
||
| 1282 | * @param string $order |
||
| 1283 | * @param string $notNullFields |
||
| 1284 | * @param bool $asobject |
||
| 1285 | * @param null $otherCriteria |
||
| 1286 | * @param string $id_key |
||
| 1287 | * |
||
| 1288 | * @return array |
||
| 1289 | */ |
||
| 1290 | public function getItems($limit = 0, $start = 0, $status = '', $categoryid = -1, $sort = 'datesub', $order = 'DESC', $notNullFields = '', $asobject = true, $otherCriteria = null, $id_key = 'none') |
||
| 1291 | { |
||
| 1292 | global $publisher_isAdmin; |
||
| 1293 | if (!$publisher_isAdmin) { |
||
| 1294 | $criteriaPermissions = new CriteriaCompo(); |
||
| 1295 | // Categories for which user has access |
||
| 1296 | $categoriesGranted = $this->publisher->getPermissionHandler()->getGrantedItems('category_read'); |
||
| 1297 | if (!empty($categoriesGranted)) { |
||
| 1298 | $grantedCategories = new Criteria('categoryid', "(" . implode(',', $categoriesGranted) . ")", 'IN'); |
||
| 1299 | $criteriaPermissions->add($grantedCategories, 'AND'); |
||
| 1300 | } else { |
||
| 1301 | return array(); |
||
| 1302 | } |
||
| 1303 | } |
||
| 1304 | if (isset($categoryid) && ($categoryid != -1)) { |
||
| 1305 | $criteriaCategory = new criteria('categoryid', $categoryid); |
||
| 1306 | } |
||
| 1307 | if (!empty($status) && is_array($status)) { |
||
| 1308 | $criteriaStatus = new CriteriaCompo(); |
||
| 1309 | foreach ($status as $v) { |
||
| 1310 | $criteriaStatus->add(new Criteria('status', $v), 'OR'); |
||
| 1311 | } |
||
| 1312 | } elseif (!empty($status) && $status != -1) { |
||
| 1313 | $criteriaStatus = new CriteriaCompo(); |
||
| 1314 | $criteriaStatus->add(new Criteria('status', $status), 'OR'); |
||
| 1315 | } |
||
| 1316 | $criteria = new CriteriaCompo(); |
||
| 1317 | if (!empty($criteriaCategory)) { |
||
| 1318 | $criteria->add($criteriaCategory); |
||
| 1319 | } |
||
| 1320 | if (!empty($criteriaPermissions)) { |
||
| 1321 | $criteria->add($criteriaPermissions); |
||
| 1322 | } |
||
| 1323 | if (!empty($criteriaStatus)) { |
||
| 1324 | $criteria->add($criteriaStatus); |
||
| 1325 | } |
||
| 1326 | if (!empty($otherCriteria)) { |
||
| 1327 | $criteria->add($otherCriteria); |
||
| 1328 | } |
||
| 1329 | $criteria->setLimit($limit); |
||
| 1330 | $criteria->setStart($start); |
||
| 1331 | $criteria->setSort($sort); |
||
| 1332 | $criteria->setOrder($order); |
||
| 1333 | $ret = $this->getItemObjects($criteria, $id_key, $notNullFields); |
||
| 1334 | return $ret; |
||
| 1335 | } |
||
| 1336 | |||
| 1337 | /** |
||
| 1338 | * @param string $field |
||
| 1339 | * @param string $status |
||
| 1340 | * @param int $categoryId |
||
| 1341 | * |
||
| 1342 | * @return bool |
||
| 1343 | */ |
||
| 1344 | public function getRandomItem($field = '', $status = '', $categoryId = -1) |
||
| 1345 | { |
||
| 1346 | $ret = false; |
||
| 1347 | $notNullFields = $field; |
||
| 1348 | // Getting the number of published Items |
||
| 1349 | $totalItems = $this->getItemsCount($categoryId, $status, $notNullFields); |
||
| 1350 | if ($totalItems > 0) { |
||
| 1351 | $totalItems = $totalItems - 1; |
||
| 1352 | mt_srand((double)microtime() * 1000000); |
||
| 1353 | $entrynumber = mt_rand(0, $totalItems); |
||
| 1354 | $item = $this->getItems(1, $entrynumber, $status, $categoryId, $sort = 'datesub', $order = 'DESC', $notNullFields); |
||
| 1355 | if ($item) { |
||
| 1356 | $ret = $item[0]; |
||
| 1357 | } |
||
| 1358 | } |
||
| 1359 | return $ret; |
||
| 1360 | } |
||
| 1361 | |||
| 1362 | /** |
||
| 1363 | * @param $itemid |
||
| 1364 | * |
||
| 1365 | * @return bool |
||
| 1366 | */ |
||
| 1367 | public function updateCounter($itemid) |
||
| 1368 | { |
||
| 1369 | $qb = $this->db2->createXoopsQueryBuilder(); |
||
| 1370 | $qb->updatePrefix('publisher_items', 'i') |
||
| 1371 | ->set('i.counter', 'i.counter+1') |
||
| 1372 | ->where('i.itemid = :itemid') |
||
| 1373 | ->setParameter(':itemid', $itemid, \PDO::PARAM_INT); |
||
| 1374 | $result = $qb->execute(); |
||
| 1375 | if ($result) { |
||
| 1376 | return true; |
||
| 1377 | } else { |
||
| 1378 | return false; |
||
| 1379 | } |
||
| 1380 | } |
||
| 1381 | |||
| 1382 | /** |
||
| 1383 | * addNotNullFieldClause exclude rows where specified columns are empty or null |
||
| 1384 | * |
||
| 1385 | * @param QueryBuilder $qb QueryBuilder instance |
||
| 1386 | * @param string|array $notNullFields fields that should not be empty |
||
| 1387 | * @param string $whereMode Initial where method, 'AND' andWhere(), otherwise where() |
||
| 1388 | * |
||
| 1389 | * @return QueryBuilder instance |
||
| 1390 | */ |
||
| 1391 | protected function addNotNullFieldClause(\Xoops\Core\Database\QueryBuilder $qb, $notNullFields = array(), $whereMode = '') |
||
| 1411 | } |
||
| 1412 | |||
| 1413 | /** |
||
| 1414 | * @param array $queryarray |
||
| 1415 | * @param string $andor |
||
| 1416 | * @param int $limit |
||
| 1417 | * @param int $offset |
||
| 1418 | * @param int $userid |
||
| 1419 | * @param array $categories |
||
| 1420 | * @param int $sortby |
||
| 1421 | * @param string $searchin |
||
| 1422 | * @param string $extra |
||
| 1423 | * |
||
| 1424 | * @return array |
||
| 1425 | */ |
||
| 1426 | public function getItemsFromSearch($queryarray = array(), $andor = 'AND', $limit = 0, $offset = 0, $userid = 0, $categories = array(), $sortby = 0, $searchin = "", $extra = "") |
||
| 1427 | { |
||
| 1428 | $xoops = Xoops::getInstance(); |
||
| 1429 | $ret = array(); |
||
| 1430 | $gperm_handler = $xoops->getHandlerGroupPermission(); |
||
| 1431 | $groups = $xoops->getUserGroups(); |
||
| 1432 | $searchin = empty($searchin) ? array( |
||
| 1433 | "title", |
||
| 1434 | "body", |
||
| 1435 | "summary" |
||
| 1436 | ) : (is_array($searchin) ? $searchin : array($searchin)); |
||
| 1437 | if (in_array("all", $searchin) || count($searchin) == 0) { |
||
| 1438 | $searchin = array("title", "subtitle", "body", "summary", "meta_keywords"); |
||
| 1439 | } |
||
| 1440 | if (is_array($userid) && count($userid) > 0) { |
||
| 1441 | $userid = array_map("intval", $userid); |
||
| 1442 | $criteriaUser = new CriteriaCompo(); |
||
| 1443 | $criteriaUser->add(new Criteria('uid', '(' . implode(',', $userid) . ')', 'IN'), 'OR'); |
||
| 1444 | } elseif (is_numeric($userid) && $userid > 0) { |
||
| 1445 | $criteriaUser = new CriteriaCompo(); |
||
| 1446 | $criteriaUser->add(new Criteria('uid', $userid), 'OR'); |
||
| 1447 | } |
||
| 1448 | $count = count($queryarray); |
||
| 1449 | if (is_array($queryarray) && $count > 0) { |
||
| 1450 | $criteriaKeywords = new CriteriaCompo(); |
||
| 1451 | for ($i = 0; $i < count($queryarray); ++$i) { |
||
| 1452 | $criteriaKeyword = new CriteriaCompo(); |
||
| 1453 | if (in_array('title', $searchin)) { |
||
| 1454 | $criteriaKeyword->add(new Criteria('title', '%' . $queryarray[$i] . '%', 'LIKE'), 'OR'); |
||
| 1455 | } |
||
| 1456 | if (in_array('subtitle', $searchin)) { |
||
| 1457 | $criteriaKeyword->add(new Criteria('subtitle', '%' . $queryarray[$i] . '%', 'LIKE'), 'OR'); |
||
| 1458 | } |
||
| 1459 | if (in_array('body', $searchin)) { |
||
| 1460 | $criteriaKeyword->add(new Criteria('body', '%' . $queryarray[$i] . '%', 'LIKE'), 'OR'); |
||
| 1461 | } |
||
| 1462 | if (in_array('summary', $searchin)) { |
||
| 1463 | $criteriaKeyword->add(new Criteria('summary', '%' . $queryarray[$i] . '%', 'LIKE'), 'OR'); |
||
| 1464 | } |
||
| 1465 | if (in_array('meta_keywords', $searchin)) { |
||
| 1466 | $criteriaKeyword->add(new Criteria('meta_keywords', '%' . $queryarray[$i] . '%', 'LIKE'), 'OR'); |
||
| 1467 | } |
||
| 1468 | $criteriaKeywords->add($criteriaKeyword, $andor); |
||
| 1469 | unset($criteriaKeyword); |
||
| 1470 | } |
||
| 1471 | } |
||
| 1472 | if (!PublisherUtils::IsUserAdmin() && (count($categories) > 0)) { |
||
| 1473 | $criteriaPermissions = new CriteriaCompo(); |
||
| 1474 | // Categories for which user has access |
||
| 1475 | $categoriesGranted = $gperm_handler->getItemIds('category_read', $groups, $this->publisher->getModule()->getVar('mid')); |
||
| 1476 | if (count($categories) > 0) { |
||
| 1477 | $categoriesGranted = array_intersect($categoriesGranted, $categories); |
||
| 1478 | } |
||
| 1479 | if (count($categoriesGranted) == 0) { |
||
| 1480 | return $ret; |
||
| 1481 | } |
||
| 1482 | $grantedCategories = new Criteria('categoryid', "(" . implode(',', $categoriesGranted) . ")", 'IN'); |
||
| 1483 | $criteriaPermissions->add($grantedCategories, 'AND'); |
||
| 1484 | } elseif (count($categories) > 0) { |
||
| 1485 | $criteriaPermissions = new CriteriaCompo(); |
||
| 1486 | $grantedCategories = new Criteria('categoryid', "(" . implode(',', $categories) . ")", 'IN'); |
||
| 1487 | $criteriaPermissions->add($grantedCategories, 'AND'); |
||
| 1488 | } |
||
| 1489 | $criteriaItemsStatus = new CriteriaCompo(); |
||
| 1490 | $criteriaItemsStatus->add(new Criteria('status', _PUBLISHER_STATUS_PUBLISHED)); |
||
| 1491 | $criteria = new CriteriaCompo(); |
||
| 1492 | if (!empty($criteriaUser)) { |
||
| 1493 | $criteria->add($criteriaUser, 'AND'); |
||
| 1494 | } |
||
| 1495 | if (!empty($criteriaKeywords)) { |
||
| 1496 | $criteria->add($criteriaKeywords, 'AND'); |
||
| 1497 | } |
||
| 1498 | if (!empty($criteriaPermissions)) { |
||
| 1499 | $criteria->add($criteriaPermissions); |
||
| 1500 | } |
||
| 1501 | if (!empty($criteriaItemsStatus)) { |
||
| 1502 | $criteria->add($criteriaItemsStatus, 'AND'); |
||
| 1503 | } |
||
| 1504 | $criteria->setLimit($limit); |
||
| 1505 | $criteria->setStart($offset); |
||
| 1506 | if (empty($sortby)) { |
||
| 1507 | $sortby = "datesub"; |
||
| 1508 | } |
||
| 1509 | $criteria->setSort($sortby); |
||
| 1510 | $order = 'ASC'; |
||
| 1511 | if ($sortby === "datesub") { |
||
| 1512 | $order = 'DESC'; |
||
| 1513 | } |
||
| 1514 | $criteria->setOrder($order); |
||
| 1515 | $ret = $this->getItemObjects($criteria); |
||
| 1516 | return $ret; |
||
| 1517 | } |
||
| 1518 | |||
| 1519 | /** |
||
| 1520 | * @param array $categoriesObj |
||
| 1521 | * @param array $status |
||
| 1522 | * |
||
| 1523 | * @return array |
||
| 1524 | */ |
||
| 1525 | public function getLastPublishedByCat($categoriesObj, $status = array(_PUBLISHER_STATUS_PUBLISHED)) |
||
| 1526 | { |
||
| 1527 | $ret = array(); |
||
| 1528 | $catIds = array(); |
||
| 1529 | /* @var $category PublisherCategory */ |
||
| 1530 | foreach ($categoriesObj as $parentid) { |
||
| 1531 | foreach ($parentid as $category) { |
||
| 1532 | $catId = $category->getVar('categoryid'); |
||
| 1533 | $catIds[$catId] = $catId; |
||
| 1534 | } |
||
| 1535 | } |
||
| 1536 | if (empty($catIds)) { |
||
| 1537 | return $ret; |
||
| 1538 | } |
||
| 1539 | |||
| 1540 | // $sql = "SELECT mi.categoryid, mi.itemid, mi.title, mi.short_url, mi.uid, mi.datesub"; |
||
| 1541 | // $sql .= " FROM (SELECT categoryid, MAX(datesub) AS date FROM " . $this->db->prefix('publisher_items'); |
||
| 1542 | // $sql .= " WHERE status IN (" . implode(',', $status) . ")"; |
||
| 1543 | // $sql .= " AND categoryid IN (" . implode(',', $catIds) . ")"; |
||
| 1544 | // $sql .= " GROUP BY categoryid)mo"; |
||
| 1545 | // $sql .= " JOIN " . $this->db->prefix('publisher_items') . " mi ON mi.datesub = mo.date"; |
||
| 1546 | |||
| 1547 | $qb = $this->db2->createXoopsQueryBuilder(); |
||
| 1548 | $qb->select('mi.categoryid', 'mi.itemid', 'mi.title', 'mi.short_url', 'mi.uid', 'mi.datesub'); |
||
| 1549 | |||
| 1550 | $subqb = $this->db2->createXoopsQueryBuilder(); |
||
| 1551 | $subqb->select('categoryid', 'MAX(datesub) AS date') |
||
| 1552 | ->fromPrefix('publisher_items', '') |
||
| 1553 | ->where($subqb->expr()->in('status', $status)) |
||
| 1554 | ->andWhere($subqb->expr()->in('categoryid', $catIds)) |
||
| 1555 | ->groupBy('categoryid'); |
||
| 1556 | $subquery = '('.$subqb->getSQL().')'; |
||
| 1557 | |||
| 1558 | $qb ->from($subquery, 'mo') |
||
| 1559 | ->joinPrefix('mo', 'publisher_items', 'mi', 'mi.datesub = mo.date'); |
||
| 1560 | |||
| 1561 | $result = $qb->execute(); |
||
| 1562 | while ($row = $result->fetch(\PDO::FETCH_ASSOC)) { |
||
| 1563 | $item = new PublisherItem(); |
||
| 1564 | $item->assignVars($row); |
||
| 1565 | $ret[$row['categoryid']] = $item; |
||
| 1566 | unset($item); |
||
| 1567 | } |
||
| 1568 | return $ret; |
||
| 1569 | } |
||
| 1570 | |||
| 1571 | /** |
||
| 1572 | * @param int $parentid |
||
| 1573 | * @param array $catsCount |
||
| 1574 | * @param string $spaces |
||
| 1575 | * @param array $resultCatCounts |
||
| 1576 | * |
||
| 1577 | * @return int |
||
| 1578 | */ |
||
| 1579 | public function countArticlesByCat($parentid, &$catsCount, $spaces = '', $resultCatCounts = array()) |
||
| 1592 | } |
||
| 1593 | |||
| 1594 | /** |
||
| 1595 | * @param int $cat_id |
||
| 1596 | * @param array $status |
||
| 1597 | * @param bool $inSubCat |
||
| 1598 | * |
||
| 1599 | * @return array |
||
| 1600 | */ |
||
| 1601 | public function getCountsByCat($cat_id = 0, $status, $inSubCat = false) |
||
| 1651 | } |
||
| 1652 | } |
||
| 1653 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: