Changeset 11333


Ignore:
Timestamp:
Jun 12, 2011, 5:56:09 PM (13 years ago)
Author:
mistic100
Message:

clean code, use TokenInput

Location:
extensions/SmartAlbums
Files:
6 edited

Legend:

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

    r10980 r11333  
    99function smart_make_associations($cat_id)
    1010{
    11   pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE." WHERE category_id = ".$cat_id." AND smart = true;");
     11  pwg_query('DELETE FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$cat_id.' AND smart = true;');
    1212 
    1313  $images = smart_get_pictures($cat_id);
     
    2121        'category_id' => $cat_id,
    2222        'smart' => true,
    23       );
     23        );
    2424    }
    2525    mass_inserts_ignore(
     
    2727      array_keys($datas[0]),
    2828      $datas
    29     );
     29      );
    3030    set_random_representant(array($cat_id));
    3131  }
     
    4848  if ($filters == null)
    4949  {
    50     $filters = pwg_query("SELECT * FROM ".CATEGORY_FILTERS_TABLE." WHERE category_id = ".$cat_id." ORDER BY type ASC, cond ASC;");
     50    $query = '
     51SELECT *
     52  FROM '.CATEGORY_FILTERS_TABLE.'
     53  WHERE category_id = '.$cat_id.'
     54  ORDER BY type ASC, cond ASC
     55;';
     56    $filters = pwg_query($query);
     57   
    5158    if (!pwg_db_num_rows($filters)) return array();
    5259   
     
    5764        'cond' => $filter['cond'],
    5865        'value' => $filter['value'],
    59       );
     66        );
    6067    }
    6168     
     
    7885          foreach($tags_arr as $value)
    7986          {
    80             $join[] = "".IMAGE_TAG_TABLE." AS it_$i_tags ON i.id = it_$i_tags.image_id";
    81             $where[] = "it_$i_tags.tag_id = ".$value."";
     87            $join[] = IMAGE_TAG_TABLE.' AS it_'.$i_tags.' ON i.id = it_'.$i_tags.'.image_id';
     88            $where[] = 'it_'.$i_tags.'.tag_id = '.$value;
    8289            $i_tags++;
    8390          }
     
    8592        else if ($filter['cond'] == 'one')
    8693        {
    87           $join[] = "".IMAGE_TAG_TABLE." AS it_$i_tags ON i.id = it_$i_tags.image_id";
    88           $where[] = "it_$i_tags.tag_id IN (".$filter['value'].")";
     94          $join[] = IMAGE_TAG_TABLE.' AS it_'.$i_tags.' ON i.id = it_'.$i_tags.'.image_id';
     95          $where[] = 'it_'.$i_tags.'.tag_id IN ('.$filter['value'].')';
    8996          $i_tags++;
    9097        }
    9198        else if ($filter['cond'] == 'none')
    9299        {
    93           $sub_query = "SELECT it_$i_tags.image_id
    94             FROM ".IMAGE_TAG_TABLE." AS it_$i_tags
    95             WHERE it_$i_tags.image_id = i.id
    96             AND it_$i_tags.tag_id IN (".$filter['value'].")
    97             GROUP BY it_$i_tags.image_id";
    98           $where[] = "NOT EXISTS (".$sub_query.")";
     100          $sub_query = '
     101      SELECT it_'.$i_tags.'.image_id
     102        FROM '.IMAGE_TAG_TABLE.' AS it_'.$i_tags.'
     103        WHERE
     104          it_'.$i_tags.'.image_id = i.id AND
     105          it_'.$i_tags.'.tag_id IN ('.$filter['value'].')
     106        GROUP BY it_'.$i_tags.'.image_id
     107    ';
     108          $where[] = 'NOT EXISTS ('.$sub_query.')';
    99109          $i_tags++;
    100110        }
    101111        else if ($filter['cond'] == 'only')
    102112        {
    103           $sub_query = "SELECT it_$i_tags.image_id
    104             FROM ".IMAGE_TAG_TABLE." AS it_$i_tags
    105             WHERE it_$i_tags.image_id = i.id
    106             AND it_$i_tags.tag_id NOT IN (".$filter['value'].")
    107             GROUP BY it_$i_tags.image_id";
    108           $where[] = "NOT EXISTS (".$sub_query.")";
     113          $sub_query = '
     114      SELECT it_'.$i_tags.'.image_id
     115        FROM '.IMAGE_TAG_TABLE.' AS it_'.$i_tags.'
     116        WHERE
     117          it_'.$i_tags.'.image_id = i.id AND
     118          it_'.$i_tags.'.tag_id NOT IN ('.$filter['value'].')
     119        GROUP BY it_'.$i_tags.'.image_id
     120    ';
     121          $where[] = 'NOT EXISTS ('.$sub_query.')';
    109122       
    110123          $i_tags++;
     
    113126          foreach($tags_arr as $value)
    114127          {
    115             $join[] = "".IMAGE_TAG_TABLE." AS it_$i_tags ON i.id = it_$i_tags.image_id";
    116             $where[] = "it_$i_tags.tag_id = ".$value."";
     128            $join[] = IMAGE_TAG_TABLE.' AS it_'.$i_tags.' ON i.id = it_'.$i_tags.'.image_id';
     129            $where[] = 'it_'.$i_tags.'.tag_id = '.$value;
    117130            $i_tags++;
    118131          }
     
    122135    else if ($filter['type'] == 'date')
    123136    {
    124       if ($filter['cond'] == 'the')         $where[] = "date_available BETWEEN '".$filter['value']." 00:00:00' AND '".$filter['value']." 23:59:59'";
    125       else if ($filter['cond'] == 'before') $where[] = "date_available < '".$filter['value']." 00:00:00'";
    126       else if ($filter['cond'] == 'after')  $where[] = "date_available > '".$filter['value']." 23:59:59'";
     137      if      ($filter['cond'] == 'the')    $where[] = 'date_available BETWEEN "'.$filter['value'].' 00:00:00" AND "'.$filter['value'].' 23:59:59"';
     138      else if ($filter['cond'] == 'before') $where[] = 'date_available < "'.$filter['value'].' 00:00:00"';
     139      else if ($filter['cond'] == 'after')  $where[] = 'date_available > "'.$filter['value'].' 23:59:59"';
    127140    }
    128141    // limit
    129142    else if ($filter['type'] == 'limit')
    130143    {
    131       $limit = "0, ".$filter['value'];
     144      $limit = '0, '.$filter['value'];
    132145    }
    133146  }
    134147 
    135148  /* bluid query */
    136   $MainQuery = "SELECT i.id
    137     FROM (
    138       SELECT i.id
    139       FROM ".IMAGES_TABLE." AS i"."\n";
    140      
    141       if (isset($join))
    142       {
    143         foreach ($join as $query)
    144         {
    145           $MainQuery .= "LEFT JOIN ".$query."\n";
    146         }
    147       }
    148       if (isset($where))
    149       {
    150         $MainQuery .= "WHERE"."\n";
    151         $i = 0;
    152         foreach ($where as $query)
    153         {
    154           if ($i != 0) $MainQuery .= "AND ";
    155           $MainQuery .= $query."\n";
    156           $i++;
    157         }
    158       }
    159  
    160       $MainQuery .= "GROUP BY i.id
    161       ".$conf['order_by']."
    162       ".(isset($limit) ? "LIMIT ".$limit : null)."
    163     ) AS i
    164   ";
    165  
     149  $MainQuery = '
     150SELECT i.id
     151  FROM '.IMAGES_TABLE.' AS i';
     152   
     153    if (isset($join))
     154    {
     155      foreach ($join as $query)
     156      {
     157        $MainQuery .= '
     158    LEFT JOIN '.$query;
     159      }
     160    }
     161    if (isset($where))
     162    {
     163      $MainQuery .= '
     164  WHERE';
     165      $i = 0;
     166      foreach ($where as $query)
     167      {
     168        if ($i != 0) $MainQuery .= ' AND';
     169        $MainQuery .= '
     170    '.$query;
     171        $i++;
     172      }
     173    }
     174
     175    $MainQuery .= '
     176  GROUP BY i.id
     177 '.$conf['order_by_inside_category'].'
     178  '.(isset($limit) ? "LIMIT ".$limit : null).'
     179;';
     180
    166181  return array_from_query($MainQuery, 'id');
    167182}
     
    189204    else
    190205    {
    191       $filter['value'] = implode(',', get_fckb_tag_ids($filter['value']));
     206      $filter['value'] = implode(',', get_tag_ids($filter['value']));
    192207    }
    193208  }
  • extensions/SmartAlbums/init_cat_list.php

    r11290 r11333  
    1313 
    1414  /* get categories with smart filters */
    15   $query = "SELECT DISTINCT id, name
    16     FROM ".CATEGORIES_TABLE." AS c
    17     INNER JOIN ".CATEGORY_FILTERS_TABLE." AS cf
    18     ON c.id = cf.category_id";
     15  $query = '
     16SELECT DISTINCT id, name
     17  FROM '.CATEGORIES_TABLE.' AS c
     18    INNER JOIN '.CATEGORY_FILTERS_TABLE.' AS cf
     19      ON c.id = cf.category_id';
    1920  if (!isset($_GET['parent_id']))
    2021  {
     
    2425  else
    2526  {
    26     $query.= '
    27     WHERE uppercats LIKE \'%'.$_GET['parent_id'].'%\'';
     27    $query .= '
     28  WHERE uppercats LIKE \'%'.$_GET['parent_id'].'%\'';
    2829  }
     30  $query .= '
     31;';
    2932 
    3033  $result = pwg_query($query);
     
    4548      {
    4649        $associated_images = smart_make_associations($cat);
    47         array_push($page['infos'], l10n_args(get_l10n_args(
    48           '%d photos associated to album &laquo;%s&raquo;',
    49           array(count($associated_images), $name)
    50         )));
     50        array_push(
     51          $page['infos'],
     52          l10n_args(get_l10n_args(
     53            '%d photos associated to album &laquo;%s&raquo;',
     54            array(count($associated_images), $name)
     55            ))
     56          );
    5157      }
    5258    }
     
    5561    {
    5662      $associated_images = smart_make_associations($_GET['smart_generate']);   
    57       array_push($page['infos'], l10n_args(get_l10n_args(
    58         '%d photos associated to album &laquo;%s&raquo;',
    59         array(count($associated_images), $smart_cats[$_GET['smart_generate']])
    60       )));
     63      array_push(
     64        $page['infos'],
     65        l10n_args(get_l10n_args(
     66          '%d photos associated to album &laquo;%s&raquo;',
     67          array(count($associated_images), $smart_cats[$_GET['smart_generate']])
     68          ))
     69        );
    6170    }
    6271   
  • extensions/SmartAlbums/init_cat_modify.php

    r10980 r11333  
    1111 
    1212  $cat_id = $_GET['cat_id'];
    13   list($cat_dir) = pwg_db_fetch_row(pwg_query("SELECT dir FROM ".CATEGORIES_TABLE." WHERE id = ".$cat_id.";"));
     13  list($cat_dir) = pwg_db_fetch_row(pwg_query('SELECT dir FROM '.CATEGORIES_TABLE.' WHERE id = '.$cat_id.';'));
    1414 
    1515  // category must be virtual
     
    2323  {
    2424    // test if it was a Smart Album
    25     $result = pwg_query("SELECT DISTINCT category_id FROM ".CATEGORY_FILTERS_TABLE." WHERE category_id = ".$cat_id.";");
     25    $result = pwg_query('SELECT DISTINCT category_id FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.';');
    2626    $was_smart = pwg_db_num_rows($result);
    2727   
     
    2929    if ($was_smart AND !isset($_POST['is_smart']))
    3030    {
    31       pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE." WHERE category_id = ".$cat_id." AND smart = true;");
    32       pwg_query("DELETE FROM ".CATEGORY_FILTERS_TABLE." WHERE category_id = ".$cat_id.";");
     31      pwg_query('DELETE FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$cat_id.' AND smart = true;');
     32      pwg_query('DELETE FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.';');
    3333    }
    3434    /* no filter selected */
     
    4040    else if (isset($_POST['is_smart']) AND count($_POST['filters']) > 0)
    4141    {
    42       pwg_query("DELETE FROM ".CATEGORY_FILTERS_TABLE." WHERE category_id = ".$cat_id.";");
     42      pwg_query('DELETE FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.';');
    4343     
    4444      $limit_is_set = false;
     
    4747        if (($filter = smart_check_filter($filter)) != false)
    4848        {
    49           pwg_query("INSERT INTO ".CATEGORY_FILTERS_TABLE."
    50             VALUES(".$cat_id.", '".$filter['type']."', '".$filter['cond']."', '".$filter['value']."');");
     49          $query = '
     50INSERT INTO '.CATEGORY_FILTERS_TABLE.'
     51  VALUES(
     52    '.$cat_id.',
     53    "'.$filter['type'].'",
     54    "'.$filter['cond'].'",
     55    "'.$filter['value'].'"
     56  )
     57;';
     58        pwg_query($query);
    5159        }
    5260      }
     
    5967     
    6068  /* select options, for html_options */
    61   $template->assign('options', array(
    62     'tags' => array(
    63       'all' => l10n('All these tags'),
    64       'one' => l10n('One of these tags'),
    65       'none' => l10n('None of these tags'),
    66       'only' => l10n('Only these tags'),
    67       ),
    68     'date' => array(
    69       'the' => l10n('Added the'),
    70       'before' => l10n('Added before the'),
    71       'after' => l10n('Added after the'),
    72       ),
    73     'limit' => array('limit' => 'limit'), // second filter not used
    74   ));
     69  $template->assign(
     70    'options',
     71    array(
     72      'tags' => array(
     73        'all' => l10n('All these tags'),
     74        'one' => l10n('One of these tags'),
     75        'none' => l10n('None of these tags'),
     76        'only' => l10n('Only these tags'),
     77        ),
     78      'date' => array(
     79        'the' => l10n('Added the'),
     80        'before' => l10n('Added before the'),
     81        'after' => l10n('Added after the'),
     82        ),
     83      'limit' => array('limit' => 'limit'), // second filter not used
     84      )
     85    );
    7586 
    7687  /* get filters for this album */
    77   $filters = pwg_query("SELECT * FROM ".CATEGORY_FILTERS_TABLE." WHERE category_id = ".$cat_id." ORDER BY type ASC, cond ASC;");
     88  $filters = pwg_query('SELECT * FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.' ORDER BY type ASC, cond ASC;');
    7889  while ($filter = pwg_db_fetch_assoc($filters))
    7990  {
     
    8192    if ($filter['type'] == 'tags')
    8293    {
    83       $query = "
    84         SELECT
    85           id AS tag_id,
    86           name AS tag_name
    87         FROM ".TAGS_TABLE."
    88         WHERE id IN(".$filter['value'].")
    89       ";
    90       $filter['value'] = get_fckb_taglist($query);
     94      $query = '
     95SELECT
     96    id AS tag_id,
     97    name AS tag_name
     98  FROM '.TAGS_TABLE.'
     99  WHERE id IN('.$filter['value'].')
     100';
     101      $filter['value'] = get_taglist($query);
    91102    }
    92103   
     
    98109  }
    99110 
     111  /* all tags */
     112  $query = '
     113SELECT
     114    id AS tag_id,
     115    name AS tag_name
     116  FROM '.TAGS_TABLE.'
     117;';
     118  $tags = get_taglist($query);
     119 
    100120  /* get image number */
    101121  if ($template->get_template_vars('IMAGE_COUNT') == null)
    102122  {
    103     list($image_num) = pwg_db_fetch_row(pwg_query("SELECT count(*) FROM ".IMAGE_CATEGORY_TABLE." WHERE category_id = ".$cat_id." AND smart = true;"));
     123    list($image_num) = pwg_db_fetch_row(pwg_query('SELECT count(*) FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$cat_id.' AND smart = true;'));
    104124    $template->assign('IMAGE_COUNT', l10n_dec('%d photo', '%d photos', $image_num));
    105125  }
     
    108128    'SMART_PATH' => SMART_PATH,
    109129    'COUNT_SCRIPT_URL' => SMART_PATH.'include/count_images.php',
     130    'tags' => $tags,
    110131  ));
    111132  $template->set_prefilter('categories', 'smart_cat_modify_prefilter');
  • extensions/SmartAlbums/language/fr_FR/plugin.lang.php

    r11290 r11333  
    1010$lang['One of these tags'] = 'Un des tags';
    1111$lang['None of these tags'] = 'Aucun des tags';
    12 $lang['Only these tags'] = 'Tous les tags';
     12$lang['Only these tags'] = 'Seulement les tags';
    1313$lang['Added the'] = 'Ajouté le';
    1414$lang['Added before the'] = 'Ajouté avant le';
  • extensions/SmartAlbums/maintain.inc.php

    r10871 r11333  
    66
    77  /* create table to store filters */
    8         pwg_query("CREATE TABLE IF NOT EXISTS `" . $prefixeTable . "category_filters` (
    9     `category_id` smallint(5) unsigned NOT NULL,
    10     `type` varchar(16) NOT NULL,
    11     `cond` varchar(16) NULL,
    12     `value` text
    13         ) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
     8        pwg_query(
     9'CREATE TABLE IF NOT EXISTS `' . $prefixeTable . 'category_filters` (
     10  `category_id` smallint(5) unsigned NOT NULL,
     11  `type` varchar(16) NOT NULL,
     12  `cond` varchar(16) NULL,
     13  `value` text
     14) ENGINE=MyISAM DEFAULT CHARSET=utf8
     15;');
    1416 
    1517  /* add a collumn to image_category_table */
    16   pwg_query("ALTER TABLE `" . IMAGE_CATEGORY_TABLE . "` ADD `smart` ENUM('true', 'false') NOT NULL DEFAULT 'false';");
     18  pwg_query('ALTER TABLE `' . IMAGE_CATEGORY_TABLE . '` ADD `smart` ENUM("true", "false") NOT NULL DEFAULT "false";');
    1719     
    1820  /* config parameter */
     
    2426        global $prefixeTable;
    2527 
    26   pwg_query("DROP TABLE `" . $prefixeTable . "category_filters`;");
    27   pwg_query("ALTER TABLE `" . IMAGE_CATEGORY_TABLE . "` DROP `smart`;");
    28   pwg_query("DELETE FROM `" . CONFIG_TABLE . "` WHERE param = 'SmartAlbums';");
     28  pwg_query('DROP TABLE `' . $prefixeTable . 'category_filters`;');
     29  pwg_query('ALTER TABLE `' . IMAGE_CATEGORY_TABLE . '` DROP `smart`;');
     30  pwg_query('DELETE FROM `' . CONFIG_TABLE . '` WHERE param = "SmartAlbums";');
    2931}
    3032?>
  • extensions/SmartAlbums/template/cat_modify.tpl

    r10980 r11333  
    7474    });
    7575   
    76     jQuery.getJSON('admin.php?fckb_tags=1', function(data) {
    77       jQuery(".tagSelect").tokenInput(
    78         data,
    79         {
    80       {/literal}
    81           hintText: '{'Type in a search term'|@translate}',
    82           noResultsText: '{'No results'|@translate}',
    83           searchingText: '{'Searching...'|@translate}',
    84           animateDropdown: false,
    85           preventDuplicates: true,
    86           allowCreation: true
    87       {literal}
    88         }
    89       );
    90     });
     76    jQuery(".tagSelect").tokenInput(
     77    {/literal}
     78      [{foreach from=$tags item=tag name=tags}{ldelim}"name":"{$tag.name|@escape:'javascript'}","id":"{$tag.id}"{rdelim}{if !$smarty.foreach.tags.last},{/if}{/foreach}],
     79      {ldelim}
     80        hintText: '{'Type in a search term'|@translate}',
     81        noResultsText: '{'No results'|@translate}',
     82        searchingText: '{'Searching...'|@translate}',
     83        animateDropdown: false,
     84        preventDuplicates: true,
     85        allowCreation: false
     86    {literal}
     87      }
     88    );
    9189  }
    9290 
Note: See TracChangeset for help on using the changeset viewer.