Changeset 26442 for extensions/SmartAlbums/include/functions.inc.php
- Timestamp:
- Jan 4, 2014, 4:13:08 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/SmartAlbums/include/functions.inc.php
r21658 r26442 1 1 <?php 2 if (!defined('PHPWG_ROOT_PATH'))die('Hacking attempt!');2 defined('SMART_PATH') or die('Hacking attempt!'); 3 3 4 4 /* … … 10 10 { 11 11 $query = ' 12 DELETE FROM '.IMAGE_CATEGORY_TABLE.' 13 WHERE 14 category_id = '.$cat_id.' 12 DELETE FROM '.IMAGE_CATEGORY_TABLE.' 13 WHERE 14 category_id = '.$cat_id.' 15 15 AND smart = true 16 16 ;'; 17 17 pwg_query($query); 18 18 19 19 $images = smart_get_pictures($cat_id); 20 20 21 21 if (count($images) != 0) 22 22 { … … 30 30 } 31 31 mass_inserts( 32 IMAGE_CATEGORY_TABLE, 33 array_keys($datas[0]), 32 IMAGE_CATEGORY_TABLE, 33 array_keys($datas[0]), 34 34 $datas, 35 35 array('ignore'=>true) 36 36 ); 37 37 } 38 38 39 39 // representant, try to not overwrite if still in images list 40 40 $query = ' … … 44 44 ;'; 45 45 list($rep_id) = pwg_db_fetch_row(pwg_query($query)); 46 47 if ( !in_array($rep_id, $images))46 47 if (!in_array($rep_id, $images)) 48 48 { 49 49 include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); 50 50 set_random_representant(array($cat_id)); 51 51 } 52 52 53 53 $query = ' 54 54 UPDATE '.CATEGORY_FILTERS_TABLE.' … … 57 57 ;'; 58 58 pwg_query($query); 59 59 60 60 return $images; 61 61 } … … 69 69 { 70 70 global $conf; 71 72 if ( defined('SMART_NOT_UPDATE') OR !$conf['SmartAlbums']['update_on_upload'] ) return; 73 71 72 if (defined('SMART_NOT_UPDATE')) 73 { 74 return; 75 } 76 74 77 // get categories with smart filters 75 78 $query = ' 76 SELECT DISTINCT id 77 FROM '.CATEGORIES_TABLE.' AS c 78 INNER JOIN '.CATEGORY_FILTERS_TABLE.' AS cf 79 ON c.id = cf.category_id 79 SELECT DISTINCT category_id 80 FROM '.CATEGORY_FILTERS_TABLE.' 80 81 ;'; 81 82 82 83 // regenerate photo list 83 $smart_cats = array_from_query($query, 'id');84 $smart_cats = query2array($query, null, 'category_id'); 84 85 array_map('smart_make_associations', $smart_cats); 85 86 } … … 97 98 98 99 /* get filters */ 99 if ( $filters === null)100 if (!isset($filters)) 100 101 { 101 102 $query = ' 102 SELECT * 103 FROM '.CATEGORY_FILTERS_TABLE.' 104 WHERE category_id = '.$cat_id.' 103 SELECT * 104 FROM '.CATEGORY_FILTERS_TABLE.' 105 WHERE category_id = '.$cat_id.' 105 106 ORDER BY type ASC, cond ASC 106 107 ;'; 107 108 $result = pwg_query($query); 108 109 if (!pwg_db_num_rows($result)) return array(); 110 109 110 if (!pwg_db_num_rows($result)) 111 { 112 return array(); 113 } 114 111 115 $filters = array(); 112 116 while ($row = pwg_db_fetch_assoc($result)) … … 123 127 return array(); 124 128 } 125 129 126 130 $mode = 'and'; 127 131 128 132 /* build constrains */ 129 133 ## generate 'join', 'where' arrays and 'limit' string to create the SQL query 130 134 ## inspired by PicsEngine 3 by Michael Villar 131 135 $i_tags = 1; 136 $join = array(); 137 $where = array(); 138 132 139 foreach ($filters as $filter) 133 140 { … … 142 149 case 'all': 143 150 { 151 $tags_arr = explode(',', $filter['value']); 152 foreach ($tags_arr as $value) 153 { 154 $join[] = IMAGE_TAG_TABLE.' AS it'.$i_tags.' ON i.id = it'.$i_tags.'.image_id'; 155 $where[] = 'it'.$i_tags.'.tag_id = '.$value; 156 $i_tags++; 157 } 158 159 break; 160 } 161 // search images which tags are in the list 162 case 'one': 163 { 164 $join[] = IMAGE_TAG_TABLE.' AS it'.$i_tags.' ON i.id = it'.$i_tags.'.image_id'; 165 $where[] = 'it'.$i_tags.'.tag_id IN ('.$filter['value'].')'; 166 $i_tags++; 167 168 break; 169 } 170 // exclude images which tags are in the list 171 case 'none': 172 { 173 $sub_query = ' 174 SELECT it'.$i_tags.'.image_id 175 FROM '.IMAGE_TAG_TABLE.' AS it'.$i_tags.' 176 WHERE 177 it'.$i_tags.'.image_id = i.id AND 178 it'.$i_tags.'.tag_id IN ('.$filter['value'].') 179 GROUP BY it'.$i_tags.'.image_id 180 '; 181 $join[] = IMAGE_TAG_TABLE.' AS it'.$i_tags.' ON i.id = it'.$i_tags.'.image_id'; 182 $where[] = 'NOT EXISTS ('.$sub_query.')'; 183 $i_tags++; 184 185 break; 186 } 187 // exclude images which tags are not in the list and search images which have all tags 188 case 'only': 189 { 190 $sub_query = ' 191 SELECT it'.$i_tags.'.image_id 192 FROM '.IMAGE_TAG_TABLE.' AS it'.$i_tags.' 193 WHERE 194 it'.$i_tags.'.image_id = i.id AND 195 it'.$i_tags.'.tag_id NOT IN ('.$filter['value'].') 196 GROUP BY it'.$i_tags.'.image_id 197 '; 198 $join[] = IMAGE_TAG_TABLE.' AS it'.$i_tags.' ON i.id = it'.$i_tags.'.image_id'; 199 $where[] = 'NOT EXISTS ('.$sub_query.')'; 200 $i_tags++; 201 144 202 $tags_arr = explode(',', $filter['value']); 145 203 foreach($tags_arr as $value) … … 149 207 $i_tags++; 150 208 } 151 152 break; 153 } 154 // search images which tags are in the list 155 case 'one': 156 { 157 $join[] = IMAGE_TAG_TABLE.' AS it'.$i_tags.' ON i.id = it'.$i_tags.'.image_id'; 158 $where[] = 'it'.$i_tags.'.tag_id IN ('.$filter['value'].')'; 159 $i_tags++; 160 161 break; 162 } 163 // exclude images which tags are in the list 164 case 'none': 165 { 166 $sub_query = ' 167 SELECT it'.$i_tags.'.image_id 168 FROM '.IMAGE_TAG_TABLE.' AS it'.$i_tags.' 169 WHERE 170 it'.$i_tags.'.image_id = i.id AND 171 it'.$i_tags.'.tag_id IN ('.$filter['value'].') 172 GROUP BY it'.$i_tags.'.image_id 173 '; 174 $join[] = IMAGE_TAG_TABLE.' AS it'.$i_tags.' ON i.id = it'.$i_tags.'.image_id'; 175 $where[] = 'NOT EXISTS ('.$sub_query.')'; 176 $i_tags++; 177 178 break; 179 } 180 // exclude images which tags are not in the list and search images which have all tags 181 case 'only': 182 { 183 $sub_query = ' 184 SELECT it'.$i_tags.'.image_id 185 FROM '.IMAGE_TAG_TABLE.' AS it'.$i_tags.' 186 WHERE 187 it'.$i_tags.'.image_id = i.id AND 188 it'.$i_tags.'.tag_id NOT IN ('.$filter['value'].') 189 GROUP BY it'.$i_tags.'.image_id 190 '; 191 $join[] = IMAGE_TAG_TABLE.' AS it'.$i_tags.' ON i.id = it'.$i_tags.'.image_id'; 192 $where[] = 'NOT EXISTS ('.$sub_query.')'; 193 $i_tags++; 194 195 $tags_arr = explode(',', $filter['value']); 196 foreach($tags_arr as $value) 197 { 198 $join[] = IMAGE_TAG_TABLE.' AS it'.$i_tags.' ON i.id = it'.$i_tags.'.image_id'; 199 $where[] = 'it'.$i_tags.'.tag_id = '.$value; 200 $i_tags++; 201 } 202 203 break; 204 } 205 } 206 207 break; 208 } 209 209 210 break; 211 } 212 } 213 214 break; 215 } 216 210 217 // date 211 218 case 'date': … … 232 239 break; 233 240 } 234 235 break; 236 } 237 241 242 break; 243 } 244 238 245 // name 239 246 case 'name': … … 263 270 break; 264 271 } 265 266 break; 267 } 268 272 273 break; 274 } 275 269 276 // album 270 277 case 'album': … … 279 286 { 280 287 $sub_query = ' 281 SELECT image_id 288 SELECT image_id 282 289 FROM '.IMAGE_CATEGORY_TABLE.' 283 290 WHERE category_id = '.$value.' … … 285 292 $where[] = 'i.id IN ('.$sub_query.')'; 286 293 } 287 294 288 295 break; 289 296 } … … 297 304 '; 298 305 $where[] = 'i.id IN ('.$sub_query.')'; 299 306 300 307 break; 301 308 } … … 309 316 '; 310 317 $where[] = 'i.id NOT IN ('.$sub_query.')'; 311 318 312 319 break; 313 320 } … … 321 328 '; 322 329 $where[] = 'i.id NOT IN ('.$sub_query.')'; 323 330 324 331 $albums_arr = explode(',', $filter['value']); 325 332 foreach($albums_arr as $value) 326 333 { 327 334 $sub_query = ' 328 SELECT image_id 335 SELECT image_id 329 336 FROM '.IMAGE_CATEGORY_TABLE.' 330 337 WHERE category_id = '.$value.' … … 332 339 $where[] = 'i.id IN ('.$sub_query.')'; 333 340 } 334 335 break; 336 } 337 } 338 339 break; 340 } 341 341 342 break; 343 } 344 } 345 346 break; 347 } 348 342 349 // dimensions 343 350 case 'dimensions': 344 351 { 345 352 $filter['value'] = explode(',', $filter['value']); 346 353 347 354 switch ($filter['cond']) 348 355 { … … 358 365 } 359 366 } 360 367 361 368 // author 362 369 case 'author': … … 384 391 break; 385 392 } 386 387 break; 388 } 389 393 394 break; 395 } 396 390 397 // hit 391 398 case 'hit': … … 400 407 break; 401 408 } 402 403 break; 404 } 405 409 410 break; 411 } 412 406 413 // rating_score 407 414 case 'rating_score': … … 416 423 break; 417 424 } 418 419 break; 420 } 421 425 426 break; 427 } 428 422 429 // level 423 430 case 'level': … … 426 433 break; 427 434 } 428 435 429 436 // limit 430 437 case 'limit': … … 433 440 break; 434 441 } 435 442 436 443 // mode 437 444 case 'mode': … … 442 449 } 443 450 } 444 451 445 452 /* bluid query */ 446 453 $MainQuery = ' 447 454 SELECT i.id 448 455 FROM '.IMAGES_TABLE.' AS i'; 449 450 if ( isset($join))456 457 if (count($join)) 451 458 { 452 459 $MainQuery.= ' 453 460 LEFT JOIN '.implode("\n LEFT JOIN ", $join); 454 461 } 455 if ( isset($where))462 if (count($where)) 456 463 { 457 464 $MainQuery.= ' … … 471 478 file_put_contents(SMART_PATH.'dump_query.sql', $MainQuery); 472 479 } 473 474 return array_from_query($MainQuery, 'id');480 481 return query2array($MainQuery, null, 'id'); 475 482 } 476 483 … … 484 491 { 485 492 global $page, $limit_is_set, $level_is_set; 493 486 494 $error = false; 487 488 495 if (!isset($limit_is_set)) $limit_is_set = false; 489 496 if (!isset($level_is_set)) $level_is_set = false; 490 497 491 498 switch ($filter['type']) 492 499 { … … 496 503 if ($filter['value'] == null) 497 504 { 498 $error = true; 499 array_push($page['errors'], l10n('No tag selected')); 505 $page['errors'][] = l10n('No tag selected'); 500 506 } 501 507 else … … 511 517 if (!preg_match('#([0-9]{4})-([0-9]{2})-([0-9]{2})#', $filter['value'])) 512 518 { 513 $error = true; 514 array_push($page['errors'], l10n('Date string is malformed')); 519 $page['errors'][] = l10n('Date string is malformed'); 515 520 } 516 521 break; … … 521 526 if (empty($filter['value'])) 522 527 { 523 $error = true; 524 array_push($page['errors'], l10n('Name is empty')); 525 } 526 else if ( $filter['cond']=='regex' and @preg_match('/'.$filter['value'].'/', null)===false ) 527 { 528 $error = true; 529 array_push($page['errors'], l10n('Regex is malformed')); 528 $page['errors'][] = l10n('Name is empty'); 529 } 530 else if ($filter['cond']=='regex' and @preg_match('/'.$filter['value'].'/', null)===false) 531 { 532 $page['errors'][] = l10n('Regex is malformed'); 530 533 } 531 534 break; … … 536 539 if (@$filter['value'] == null) 537 540 { 538 $error = true; 539 array_push($page['errors'], l10n('No album selected')); 541 $page['errors'][] = l10n('No album selected'); 540 542 } 541 543 else … … 548 550 case 'dimensions': 549 551 { 550 if ( empty($filter['value']['min']) or empty($filter['value']['max']))552 if (empty($filter['value']['min']) or empty($filter['value']['max'])) 551 553 { 552 554 $error = true; … … 563 565 if (empty($filter['value'])) 564 566 { 565 $error = true; 566 array_push($page['errors'], l10n('Author is empty')); 567 } 568 else if ( $filter['cond']=='regex' and @preg_match('/'.$filter['value'].'/', null)===false ) 569 { 570 $error = true; 571 array_push($page['errors'], l10n('Regex is malformed')); 567 $page['errors'][] = l10n('Author is empty'); 568 } 569 else if ($filter['cond']=='regex' and @preg_match('/'.$filter['value'].'/', null)===false) 570 { 571 $page['errors'][] = l10n('Regex is malformed'); 572 572 } 573 573 else … … 580 580 case 'hit': 581 581 { 582 if (!preg_match('#([0-9]{1,})#', $filter['value'])) 583 { 584 $error = true; 585 array_push($page['errors'], l10n('Hits must be an integer')); 582 if (!preg_match('#([0-9]+)#', $filter['value'])) 583 { 584 $page['errors'][] = l10n('Hits must be an integer'); 586 585 } 587 586 break; … … 590 589 case 'rating_score': 591 590 { 592 if (!preg_match('#([0-9]{1,})#', $filter['value'])) 593 { 594 $error = true; 595 array_push($page['errors'], l10n('Rating score must be an integer')); 591 if (!preg_match('#([0-9]+)#', $filter['value'])) 592 { 593 $page['errors'][] = l10n('Rating score must be an integer'); 596 594 } 597 595 break; … … 602 600 if ($level_is_set == true) // only one level is allowed, first is saved 603 601 { 604 $error = true; 605 array_push($page['errors'], l10n('You can\'t use more than one level filter')); 602 $page['errors'][] = l10n('You can\'t use more than one level filter'); 606 603 } 607 604 else … … 617 614 if ($limit_is_set == true) // only one limit is allowed, first is saved 618 615 { 619 $error = true; 620 array_push($page['errors'], l10n('You can\'t use more than one limit filter')); 621 } 622 else if (!preg_match('#([0-9]{1,})#', $filter['value'])) 623 { 624 $error = true; 625 array_push($page['errors'], l10n('Limit must be an integer')); 626 } 627 else 628 { 629 $filter['cond'] = 'level'; 616 $page['errors'][] = l10n('You can\'t use more than one limit filter'); 617 } 618 else if (!preg_match('#([0-9]+)#', $filter['value'])) 619 { 620 $page['errors'][] = l10n('Limit must be an integer'); 621 } 622 else 623 { 624 $filter['cond'] = 'limit'; 630 625 $limit_is_set = true; 631 626 } … … 638 633 break; 639 634 } 640 635 641 636 default: 642 637 { … … 645 640 } 646 641 } 647 648 649 if ( $error == false)642 643 644 if (!$error && empty($page['errors'])) 650 645 { 651 646 return $filter; … … 656 651 } 657 652 } 658 659 660 /**661 * clean table when categories are deleted662 */663 function smart_delete_categories($ids)664 {665 $query = '666 DELETE FROM '.CATEGORY_FILTERS_TABLE.'667 WHERE category_id IN('.implode(',', $ids).')668 ;';669 pwg_query($query);670 }671 672 /**673 * update images list periodically674 */675 function smart_periodic_update()676 {677 global $conf;678 679 // we only search for old albums every hour, nevermind which user is connected680 if ($conf['SmartAlbums']['last_update'] > time() - 3600) return;681 682 $conf['SmartAlbums']['last_update'] = time();683 conf_update_param('SmartAlbums', serialize($conf['SmartAlbums']));684 685 // get categories with smart filters686 $query = '687 SELECT DISTINCT id688 FROM '.CATEGORIES_TABLE.' AS c689 INNER JOIN '.CATEGORY_FILTERS_TABLE.' AS cf690 ON c.id = cf.category_id691 WHERE updated < DATE_SUB(NOW(), INTERVAL '.$conf['SmartAlbums']['update_timeout'].' DAY)692 ;';693 694 // regenerate photo list695 $smart_cats = array_from_query($query, 'id');696 array_map('smart_make_associations', $smart_cats);697 }698 699 ?>
Note: See TracChangeset
for help on using the changeset viewer.