Ignore:
Timestamp:
Aug 14, 2005, 1:09:54 AM (19 years ago)
Author:
plg
Message:
  • new : maintenance screen in administration. There you can update categories informations (number of images, date of the last added element), update images informations (path, average rate), purge obsolete sessions, purge history.
  • new : ability to have random representative for categories. This configuration parameter is set to false by default.
  • new : ability to set an element as representative of a category without belonging to the category. Thus, administrator can choose representative even for empty categories.
  • improvement : semantically superior design for category edition screen by regrouping fields in fieldsets. The improved screen contains action buttons as in category list screen.
  • new : ability to move a virtual category (ie change its parent category).
  • bug fixed : the sync_users function checks all user children tables (access, cache, group association).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/functions.php

    r808 r809  
    396396function update_category($ids = 'all', $recursive = false)
    397397{
     398  global $conf;
     399 
    398400  // retrieving all categories to update
    399401  $cat_ids = array();
     
    429431  $query.= '
    430432;';
    431   $result = pwg_query( $query );
    432   while ( $row = mysql_fetch_array( $result ) )
    433   {
    434     array_push($cat_ids, $row['id']);
    435   }
    436   $cat_ids = array_unique($cat_ids);
     433  $cat_ids = array_unique(array_from_query($query, 'id'));
    437434
    438435  if (count($cat_ids) == 0)
     
    471468  mass_updates(CATEGORIES_TABLE, $fields, $datas);
    472469
    473   $query = '
     470  // representative pictures
     471  if (count($cat_ids) > 0)
     472  {
     473    // find all categories where the setted representative is not possible :
     474    // the picture does not exist
     475    $query = '
     476SELECT c.id
     477  FROM '.CATEGORIES_TABLE.' AS c LEFT JOIN '.IMAGES_TABLE.' AS i
     478    ON c.representative_picture_id = i.id
     479  WHERE representative_picture_id IS NOT NULL
     480    AND c.id IN ('.wordwrap(implode(', ', $cat_ids), 80, "\n").')
     481    AND i.id IS NULL
     482;';
     483    $wrong_representant = array_from_query($query, 'id');
     484
     485    if ($conf['allow_random_representative'])
     486    {
     487      if (count($wrong_representant) > 0)
     488      {
     489        $query = '
    474490UPDATE '.CATEGORIES_TABLE.'
    475491  SET representative_picture_id = NULL
    476   WHERE nb_images = 0
    477 ;';
    478   pwg_query($query);
    479  
    480   if (count($cat_ids) > 0)
    481   {
    482     $categories = array();
    483     // find all categories where the setted representative is not possible
    484     $query = '
     492  WHERE id IN ('.wordwrap(implode(', ', $wrong_representant), 80, "\n").')
     493;';
     494        pwg_query($query);
     495      }
     496    }
     497    else
     498    {
     499      $to_null = array();
     500      $to_rand = array();
     501
     502      if (count($wrong_representant) > 0)
     503      {
     504        // among the categories with an unknown representant, we dissociate
     505        // categories containing pictures and categories containing no
     506        // pictures. Indeed, the representant must set to NULL if no picture
     507        // in the category and set to a random picture otherwise.
     508        $query = '
    485509SELECT id
    486   FROM '.CATEGORIES_TABLE.' LEFT JOIN '.IMAGE_CATEGORY_TABLE.'
    487     ON id = category_id AND representative_picture_id = image_id
    488   WHERE representative_picture_id IS NOT NULL
    489     AND id IN ('.wordwrap(implode(', ', $cat_ids), 80, "\n").')
    490     AND category_id IS NULL
    491 ;';
    492     $result = pwg_query($query);
    493     while ($row = mysql_fetch_array($result))
    494     {
    495       array_push($categories, $row['id']);
    496     }
    497     // find categories with elements and with no representant
    498     $query = '
     510  FROM '.CATEGORIES_TABLE.'
     511  WHERE id IN ('.wordwrap(implode(', ', $wrong_representant), 80, "\n").')
     512    AND nb_images = 0
     513;';
     514        $to_null = array_from_query($query, 'id');
     515        $to_rand = array_diff($wrong_representant, $to_null);
     516      }
     517     
     518      if (count($to_null) > 0)
     519      {
     520        $query = '
     521UPDATE '.CATEGORIES_TABLE.'
     522  SET representative_picture_id = NULL
     523  WHERE id IN ('.wordwrap(implode(', ', $to_null), 80, "\n").')
     524;';
     525        pwg_query($query);
     526      }
     527     
     528      // If the random representant is not allowed, we need to find
     529      // categories with elements and with no representant. Those categories
     530      // must be added to the list of categories to set to a random
     531      // representant.
     532      $query = '
    499533SELECT id
    500534  FROM '.CATEGORIES_TABLE.'
    501535  WHERE representative_picture_id IS NULL
    502536    AND nb_images != 0
    503 ;';
    504     $result = pwg_query($query);
    505     while ($row = mysql_fetch_array($result))
    506     {
    507       array_push($categories, $row['id']);
    508     }
    509 
    510     $categories = array_unique($categories);
    511     set_random_representant($categories);
     537    AND id IN ('.wordwrap(implode(', ', $cat_ids), 80, "\n").')
     538;';
     539      $to_rand =
     540        array_unique(
     541          array_merge(
     542            $to_rand,
     543            array_from_query($query, 'id')
     544            )
     545          );
     546     
     547      if (count($to_rand) > 0)
     548      {
     549        set_random_representant($to_rand);
     550      }
     551    }
    512552  }
    513553}
     
    11871227
    11881228/**
    1189  * compares and synchronizes USERS_TABLE and USER_INFOS_TABLE : each user in
    1190  * USERS_TABLE must be present in USER_INFOS_TABLE.
     1229 * synchronize base users list and related users list
     1230 *
     1231 * compares and synchronizes base users table (USERS_TABLE) with its child
     1232 * tables (USER_INFOS_TABLE, USER_ACCESS, USER_CACHE, USER_GROUP) : each
     1233 * base user must be present in child tables, users in child tables not
     1234 * present in base table must be deleted.
     1235 *
     1236 * @return void
    11911237 */
    11921238function sync_users()
     
    12411287  }
    12421288
    1243   // users present in $infos_users and not in $base_users must be deleted
    1244   $to_delete = array_diff($infos_users, $base_users);
    1245 
    1246   if (count($to_delete) > 0)
     1289  // users present in user related tables must be present in the base user
     1290  // table
     1291  $tables =
     1292    array(
     1293      USER_INFOS_TABLE,
     1294      USER_ACCESS_TABLE,
     1295      USER_CACHE_TABLE,
     1296      USER_GROUP_TABLE
     1297      );
     1298  foreach ($tables as $table)
    12471299  {
    12481300    $query = '
     1301SELECT user_id
     1302  FROM '.$table.'
     1303;';
     1304    $to_delete =
     1305      array_diff(
     1306        array_from_query($query, 'user_id'),
     1307        $base_users
     1308        );
     1309   
     1310    if (count($to_delete) > 0)
     1311    {
     1312      $query = '
    12491313DELETE
    1250   FROM '.USER_INFOS_TABLE.'
     1314  FROM '.$table.'
    12511315  WHERE user_id in ('.implode(',', $to_delete).')
    12521316;';
     1317      pwg_query($query);
     1318    }
     1319  }
     1320}
     1321
     1322/**
     1323 * updates categories.uppercats field based on categories.id +
     1324 * categories.id_uppercat
     1325 *
     1326 * @return void
     1327 */
     1328function update_uppercats()
     1329{
     1330  $uppercat_ids = array();
     1331 
     1332  $query = '
     1333SELECT id, id_uppercat
     1334  FROM '.CATEGORIES_TABLE.'
     1335;';
     1336  $result = pwg_query($query);
     1337  while ($row = mysql_fetch_array($result))
     1338  {
     1339    $uppercat_ids[$row['id']] =
     1340      !empty($row['id_uppercat']) ? $row['id_uppercat'] : 'NULL';
     1341  }
     1342 
     1343  // uppercats array associates a category id to the list of uppercats id.
     1344  $uppercats = array();
     1345 
     1346  foreach (array_keys($uppercat_ids) as $id)
     1347  {
     1348    $uppercats[$id] = array();
     1349
     1350    $uppercat = $id;
     1351
     1352    while ($uppercat != 'NULL')
     1353    {
     1354      array_push($uppercats[$id], $uppercat);
     1355      $uppercat = $uppercat_ids[$uppercat];
     1356    }
     1357  }
     1358
     1359  $datas = array();
     1360
     1361  foreach ($uppercats as $id => $list)
     1362  {
     1363    array_push(
     1364      $datas,
     1365      array(
     1366        'id' => $id,
     1367        'uppercats' => implode(',', array_reverse($list))
     1368        )
     1369      );
     1370  }
     1371
     1372  $fields = array('primary' => array('id'), 'update' => array('uppercats'));
     1373  mass_updates(CATEGORIES_TABLE, $fields, $datas);
     1374}
     1375
     1376/**
     1377 * update images.path field
     1378 *
     1379 * @return void
     1380 */
     1381function update_path()
     1382{
     1383  $query = '
     1384SELECT DISTINCT(storage_category_id)
     1385  FROM '.IMAGES_TABLE.'
     1386;';
     1387  $cat_ids = array_from_query($query, 'storage_category_id');
     1388  $fulldirs = get_fulldirs($cat_ids);
     1389 
     1390  foreach ($cat_ids as $cat_id)
     1391  {
     1392    $query = '
     1393UPDATE '.IMAGES_TABLE.'
     1394  SET path = CONCAT(\''.$fulldirs[$cat_id].'\',\'/\',file)
     1395  WHERE storage_category_id = '.$cat_id.'
     1396;';
    12531397    pwg_query($query);
    1254   }
     1398  }
     1399}
     1400
     1401/**
     1402 * update images.average_rate field
     1403 *
     1404 * @return void
     1405 */
     1406function update_average_rate()
     1407{
     1408  $average_rates = array();
     1409 
     1410  $query = '
     1411SELECT element_id,
     1412       ROUND(AVG(rate),2) AS average_rate
     1413  FROM '.RATE_TABLE.'
     1414  GROUP BY element_id
     1415;';
     1416  $result = pwg_query($query);
     1417  while ($row = mysql_fetch_array($result))
     1418  {
     1419    array_push($average_rates, $row);
     1420  }
     1421
     1422  $datas = array();
     1423  foreach ($average_rates as $item)
     1424  {
     1425    array_push(
     1426      $datas,
     1427      array(
     1428        'id' => $item['element_id'],
     1429        'average_rate' => $item['average_rate']
     1430        )
     1431      );
     1432  }
     1433  $fields = array('primary' => array('id'), 'update' => array('average_rate'));
     1434  mass_updates(IMAGES_TABLE, $fields, $datas);
     1435}
     1436
     1437/**
     1438 * change the parent category of the given category. The category is
     1439 * supposed virtual.
     1440 *
     1441 * @param int category identifier
     1442 * @param int parent category identifier
     1443 * @return void
     1444 */
     1445function move_category($category_id, $new_parent = -1)
     1446{
     1447  // verifies if the move is necessary
     1448  $query = '
     1449SELECT id_uppercat, status
     1450  FROM '.CATEGORIES_TABLE.'
     1451  WHERE id = '.$category_id.'
     1452;';
     1453  list($old_parent, $status) = mysql_fetch_row(pwg_query($query));
     1454
     1455  $old_parent = empty($old_parent) ? 'NULL' : $old_parent;
     1456  $new_parent = $new_parent < 1 ? 'NULL' : $new_parent;
     1457
     1458  if ($new_parent == $old_parent)
     1459  {
     1460    // no need to move !
     1461    return;
     1462  }
     1463 
     1464  $query = '
     1465UPDATE '.CATEGORIES_TABLE.'
     1466  SET id_uppercat = '.$new_parent.'
     1467  WHERE id = '.$category_id.'
     1468;';
     1469  pwg_query($query);
     1470
     1471  update_uppercats();
     1472  ordering();
     1473  update_global_rank();
     1474
     1475  // status and related permissions management
     1476  if ('NULL' == $new_parent)
     1477  {
     1478    $parent_status = 'public';
     1479  }
     1480  else
     1481  {
     1482    $query = '
     1483SELECT status
     1484  FROM '.CATEGORIES_TABLE.'
     1485  WHERE id = '.$new_parent.'
     1486;';
     1487    list($parent_status) = mysql_fetch_row(pwg_query($query));
     1488  }
     1489
     1490  if ('private' == $parent_status)
     1491  {
     1492    switch ($status)
     1493    {
     1494      case 'public' :
     1495      {
     1496        set_cat_status(array($category_id), 'private');
     1497        break;
     1498      }
     1499      case 'private' :
     1500      {
     1501        $subcats = get_subcat_ids(array($category_id));
     1502
     1503        $tables =
     1504          array(
     1505            USER_ACCESS_TABLE => 'user_id',
     1506            GROUP_ACCESS_TABLE => 'group_id'
     1507            );
     1508       
     1509        foreach ($tables as $table => $field)
     1510        {
     1511          $query = '
     1512SELECT '.$field.'
     1513  FROM '.$table.'
     1514  WHERE category_id = '.$category_id.'
     1515;';
     1516          $category_access = array_from_query($query, $field);
     1517
     1518          $query = '
     1519SELECT '.$field.'
     1520  FROM '.$table.'
     1521  WHERE category_id = '.$new_parent.'
     1522;';
     1523          $parent_access = array_from_query($query, $field);
     1524         
     1525          $to_delete = array_diff($parent_access, $category_access);
     1526         
     1527          if (count($to_delete) > 0)
     1528          {
     1529            $query = '
     1530DELETE FROM '.$table.'
     1531  WHERE '.$field.' IN ('.implode(',', $to_delete).')
     1532    AND category_id IN ('.implode(',', $subcats).')
     1533;';
     1534            pwg_query($query);
     1535          }
     1536        }
     1537        break;
     1538      }
     1539    }
     1540  }
    12551541}
    12561542?>
Note: See TracChangeset for help on using the changeset viewer.