Ignore:
Timestamp:
Jan 4, 2014, 4:13:08 PM (10 years ago)
Author:
mistic100
Message:

update for Piwigo 2.6

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/SmartAlbums/include/functions.inc.php

    r21658 r26442  
    11<?php
    2 if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
     2defined('SMART_PATH') or die('Hacking attempt!');
    33
    44/*
     
    1010{
    1111  $query = '
    12 DELETE FROM '.IMAGE_CATEGORY_TABLE.' 
    13   WHERE 
    14     category_id = '.$cat_id.' 
     12DELETE FROM '.IMAGE_CATEGORY_TABLE.'
     13  WHERE
     14    category_id = '.$cat_id.'
    1515    AND smart = true
    1616;';
    1717  pwg_query($query);
    18  
     18
    1919  $images = smart_get_pictures($cat_id);
    20  
     20
    2121  if (count($images) != 0)
    2222  {
     
    3030    }
    3131    mass_inserts(
    32       IMAGE_CATEGORY_TABLE, 
    33       array_keys($datas[0]), 
     32      IMAGE_CATEGORY_TABLE,
     33      array_keys($datas[0]),
    3434      $datas,
    3535      array('ignore'=>true)
    3636      );
    3737  }
    38  
     38
    3939  // representant, try to not overwrite if still in images list
    4040  $query = '
     
    4444;';
    4545  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))
    4848  {
    4949    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
    5050    set_random_representant(array($cat_id));
    5151  }
    52  
     52
    5353  $query = '
    5454UPDATE '.CATEGORY_FILTERS_TABLE.'
     
    5757;';
    5858  pwg_query($query);
    59  
     59
    6060  return $images;
    6161}
     
    6969{
    7070  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
    7477  // get categories with smart filters
    7578  $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
     79SELECT DISTINCT category_id
     80  FROM '.CATEGORY_FILTERS_TABLE.'
    8081;';
    81  
     82
    8283  // regenerate photo list
    83   $smart_cats = array_from_query($query, 'id');
     84  $smart_cats = query2array($query, null, 'category_id');
    8485  array_map('smart_make_associations', $smart_cats);
    8586}
     
    9798
    9899  /* get filters */
    99   if ($filters === null)
     100  if (!isset($filters))
    100101  {
    101102    $query = '
    102 SELECT * 
    103   FROM '.CATEGORY_FILTERS_TABLE.' 
    104   WHERE category_id = '.$cat_id.' 
     103SELECT *
     104  FROM '.CATEGORY_FILTERS_TABLE.'
     105  WHERE category_id = '.$cat_id.'
    105106  ORDER BY type ASC, cond ASC
    106107;';
    107108    $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
    111115    $filters = array();
    112116    while ($row = pwg_db_fetch_assoc($result))
     
    123127    return array();
    124128  }
    125  
     129
    126130  $mode = 'and';
    127  
     131
    128132  /* build constrains */
    129133  ## generate 'join', 'where' arrays and 'limit' string to create the SQL query
    130134  ## inspired by PicsEngine 3 by Michael Villar
    131135  $i_tags = 1;
     136  $join = array();
     137  $where = array();
     138
    132139  foreach ($filters as $filter)
    133140  {
     
    142149          case 'all':
    143150          {
     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
    144202            $tags_arr = explode(',', $filter['value']);
    145203            foreach($tags_arr as $value)
     
    149207              $i_tags++;
    150208            }
    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
    210217      // date
    211218      case 'date':
     
    232239            break;
    233240        }
    234        
    235         break;
    236       }
    237      
     241
     242        break;
     243      }
     244
    238245      // name
    239246      case 'name':
     
    263270            break;
    264271        }
    265        
    266         break;
    267       }
    268      
     272
     273        break;
     274      }
     275
    269276      // album
    270277      case 'album':
     
    279286            {
    280287              $sub_query = '
    281       SELECT image_id 
     288      SELECT image_id
    282289        FROM '.IMAGE_CATEGORY_TABLE.'
    283290        WHERE category_id = '.$value.'
     
    285292              $where[] = 'i.id IN ('.$sub_query.')';
    286293            }
    287            
     294
    288295            break;
    289296          }
     
    297304      ';
    298305            $where[] = 'i.id IN ('.$sub_query.')';
    299            
     306
    300307            break;
    301308          }
     
    309316      ';
    310317            $where[] = 'i.id NOT IN ('.$sub_query.')';
    311            
     318
    312319            break;
    313320          }
     
    321328      ';
    322329            $where[] = 'i.id NOT IN ('.$sub_query.')';
    323            
     330
    324331            $albums_arr = explode(',', $filter['value']);
    325332            foreach($albums_arr as $value)
    326333            {
    327334              $sub_query = '
    328       SELECT image_id 
     335      SELECT image_id
    329336        FROM '.IMAGE_CATEGORY_TABLE.'
    330337        WHERE category_id = '.$value.'
     
    332339              $where[] = 'i.id IN ('.$sub_query.')';
    333340            }
    334            
    335             break;
    336           }
    337         }
    338        
    339         break;
    340       }
    341      
     341
     342            break;
     343          }
     344        }
     345
     346        break;
     347      }
     348
    342349      // dimensions
    343350      case 'dimensions':
    344351      {
    345352        $filter['value'] = explode(',', $filter['value']);
    346        
     353
    347354        switch ($filter['cond'])
    348355        {
     
    358365        }
    359366      }
    360      
     367
    361368      // author
    362369      case 'author':
     
    384391            break;
    385392        }
    386        
    387         break;
    388       }
    389      
     393
     394        break;
     395      }
     396
    390397      // hit
    391398      case 'hit':
     
    400407            break;
    401408        }
    402        
    403         break;
    404       }
    405      
     409
     410        break;
     411      }
     412
    406413      // rating_score
    407414      case 'rating_score':
     
    416423            break;
    417424        }
    418        
    419         break;
    420       }
    421      
     425
     426        break;
     427      }
     428
    422429      // level
    423430      case 'level':
     
    426433        break;
    427434      }
    428      
     435
    429436      // limit
    430437      case 'limit':
     
    433440        break;
    434441      }
    435      
     442
    436443      // mode
    437444      case 'mode':
     
    442449    }
    443450  }
    444  
     451
    445452  /* bluid query */
    446453  $MainQuery = '
    447454SELECT i.id
    448455  FROM '.IMAGES_TABLE.' AS i';
    449    
    450     if (isset($join))
     456
     457    if (count($join))
    451458    {
    452459      $MainQuery.= '
    453460    LEFT JOIN '.implode("\n    LEFT JOIN ", $join);
    454461    }
    455     if (isset($where))
     462    if (count($where))
    456463    {
    457464      $MainQuery.= '
     
    471478    file_put_contents(SMART_PATH.'dump_query.sql', $MainQuery);
    472479  }
    473  
    474   return array_from_query($MainQuery, 'id');
     480
     481  return query2array($MainQuery, null, 'id');
    475482}
    476483
     
    484491{
    485492  global $page, $limit_is_set, $level_is_set;
     493
    486494  $error = false;
    487  
    488495  if (!isset($limit_is_set)) $limit_is_set = false;
    489496  if (!isset($level_is_set)) $level_is_set = false;
    490  
     497
    491498  switch ($filter['type'])
    492499  {
     
    496503      if ($filter['value'] == null)
    497504      {
    498         $error = true;
    499         array_push($page['errors'], l10n('No tag selected'));
     505        $page['errors'][] = l10n('No tag selected');
    500506      }
    501507      else
     
    511517      if (!preg_match('#([0-9]{4})-([0-9]{2})-([0-9]{2})#', $filter['value']))
    512518      {
    513         $error = true;
    514         array_push($page['errors'], l10n('Date string is malformed'));
     519        $page['errors'][] = l10n('Date string is malformed');
    515520      }
    516521      break;
     
    521526      if (empty($filter['value']))
    522527      {
    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');
    530533      }
    531534      break;
     
    536539      if (@$filter['value'] == null)
    537540      {
    538         $error = true;
    539         array_push($page['errors'], l10n('No album selected'));
     541        $page['errors'][] = l10n('No album selected');
    540542      }
    541543      else
     
    548550    case 'dimensions':
    549551    {
    550       if ( empty($filter['value']['min']) or empty($filter['value']['max']) )
     552      if (empty($filter['value']['min']) or empty($filter['value']['max']))
    551553      {
    552554        $error = true;
     
    563565      if (empty($filter['value']))
    564566      {
    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');
    572572      }
    573573      else
     
    580580    case 'hit':
    581581    {
    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');
    586585      }
    587586      break;
     
    590589    case 'rating_score':
    591590    {
    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');
    596594      }
    597595      break;
     
    602600      if ($level_is_set == true) // only one level is allowed, first is saved
    603601      {
    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');
    606603      }
    607604      else
     
    617614      if ($limit_is_set == true) // only one limit is allowed, first is saved
    618615      {
    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';
    630625        $limit_is_set = true;
    631626      }
     
    638633      break;
    639634    }
    640    
     635
    641636    default:
    642637    {
     
    645640    }
    646641  }
    647  
    648  
    649   if ($error == false)
     642
     643
     644  if (!$error && empty($page['errors']))
    650645  {
    651646    return $filter;
     
    656651  }
    657652}
    658 
    659 
    660 /**
    661  * clean table when categories are deleted
    662  */
    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 periodically
    674  */
    675 function smart_periodic_update()
    676 {
    677   global $conf;
    678  
    679   // we only search for old albums every hour, nevermind which user is connected
    680   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 filters
    686   $query = '
    687 SELECT DISTINCT id
    688   FROM '.CATEGORIES_TABLE.' AS c
    689     INNER JOIN '.CATEGORY_FILTERS_TABLE.' AS cf
    690     ON c.id = cf.category_id
    691   WHERE updated < DATE_SUB(NOW(), INTERVAL '.$conf['SmartAlbums']['update_timeout'].' DAY)
    692 ;';
    693  
    694   // regenerate photo list
    695   $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.