Changeset 881 for trunk/admin


Ignore:
Timestamp:
Oct 8, 2005, 12:04:53 AM (19 years ago)
Author:
plg
Message:
  • new: mass virtual categories movement manager in Administration>Categories>Move screen.
Location:
trunk/admin
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/cat_modify.php

    r862 r881  
    6868  if (isset($_POST['parent']))
    6969  {
    70     move_category($_GET['cat_id'], $_POST['parent']);
     70    move_categories(
     71      array($_GET['cat_id']),
     72      $_POST['parent']
     73      );
    7174  }
    7275
  • trunk/admin/include/functions.php

    r880 r881  
    13381338
    13391339/**
    1340  * change the parent category of the given category. The category is
     1340 * change the parent category of the given categories. The categories are
    13411341 * supposed virtual.
    13421342 *
    1343  * @param int category identifier
     1343 * @param array category identifiers
    13441344 * @param int parent category identifier
    13451345 * @return void
    13461346 */
    1347 function move_category($category_id, $new_parent = -1)
    1348 {
    1349   // verifies if the move is necessary
    1350   $query = '
    1351 SELECT id_uppercat, status
     1347function move_categories($category_ids, $new_parent = -1)
     1348{
     1349  global $page;
     1350
     1351  if (count($category_ids) == 0)
     1352  {
     1353    return;
     1354  }
     1355
     1356  $new_parent = $new_parent < 1 ? 'NULL' : $new_parent;
     1357
     1358  $categories = array();
     1359 
     1360  $query = '
     1361SELECT id, id_uppercat, status, uppercats
    13521362  FROM '.CATEGORIES_TABLE.'
    1353   WHERE id = '.$category_id.'
    1354 ;';
    1355   list($old_parent, $status) = mysql_fetch_row(pwg_query($query));
    1356 
    1357   $old_parent = empty($old_parent) ? 'NULL' : $old_parent;
    1358   $new_parent = $new_parent < 1 ? 'NULL' : $new_parent;
    1359 
    1360   if ($new_parent == $old_parent)
    1361   {
    1362     // no need to move !
    1363     return;
    1364   }
     1363  WHERE id IN ('.implode(',', $category_ids).')
     1364;';
     1365  $result = pwg_query($query);
     1366  while ($row = mysql_fetch_array($result))
     1367  {
     1368    $categories[$row['id']] =
     1369      array(
     1370        'parent' => empty($row['id_uppercat']) ? 'NULL' : $row['id_uppercat'],
     1371        'status' => $row['status'],
     1372        'uppercats' => $row['uppercats']
     1373        );
     1374  }
     1375 
     1376  // is the movement possible? The movement is impossible if you try to move
     1377  // a category in a sub-category or itself
     1378  if ('NULL' != $new_parent)
     1379  {
     1380    $query = '
     1381SELECT uppercats
     1382  FROM '.CATEGORIES_TABLE.'
     1383  WHERE id = '.$new_parent.'
     1384;';
     1385    list($new_parent_uppercats) = mysql_fetch_row(pwg_query($query));
     1386
     1387    foreach ($categories as $category)
     1388    {
     1389      // technically, you can't move a category with uppercats 12,125,13,14
     1390      // into a new parent category with uppercats 12,125,13,14,24
     1391      if (preg_match('/^'.$category['uppercats'].'/', $new_parent_uppercats))
     1392      {
     1393        array_push(
     1394          $page['errors'],
     1395          l10n('You cannot move a category in its own sub category')
     1396          );
     1397        return;
     1398      }
     1399    }
     1400  }
     1401 
     1402  $tables =
     1403    array(
     1404      USER_ACCESS_TABLE => 'user_id',
     1405      GROUP_ACCESS_TABLE => 'group_id'
     1406      );
    13651407 
    13661408  $query = '
    13671409UPDATE '.CATEGORIES_TABLE.'
    13681410  SET id_uppercat = '.$new_parent.'
    1369   WHERE id = '.$category_id.'
     1411  WHERE id IN ('.implode(',', $category_ids).')
    13701412;';
    13711413  pwg_query($query);
     
    13921434  if ('private' == $parent_status)
    13931435  {
    1394     switch ($status)
    1395     {
    1396       case 'public' :
    1397       {
    1398         set_cat_status(array($category_id), 'private');
    1399         break;
    1400       }
    1401       case 'private' :
    1402       {
    1403         $subcats = get_subcat_ids(array($category_id));
    1404 
    1405         $tables =
    1406           array(
    1407             USER_ACCESS_TABLE => 'user_id',
    1408             GROUP_ACCESS_TABLE => 'group_id'
    1409             );
     1436    foreach ($categories as $cat_id => $category)
     1437    {
     1438      switch ($category['status'])
     1439      {
     1440        case 'public' :
     1441        {
     1442          set_cat_status(array($cat_id), 'private');
     1443          break;
     1444        }
     1445        case 'private' :
     1446        {
     1447          $subcats = get_subcat_ids(array($cat_id));
    14101448       
    1411         foreach ($tables as $table => $field)
    1412         {
    1413           $query = '
     1449          foreach ($tables as $table => $field)
     1450          {
     1451            $query = '
    14141452SELECT '.$field.'
    14151453  FROM '.$table.'
    1416   WHERE category_id = '.$category_id.'
    1417 ;';
    1418           $category_access = array_from_query($query, $field);
    1419 
    1420           $query = '
     1454  WHERE cat_id = '.$cat_id.'
     1455;';
     1456            $category_access = array_from_query($query, $field);
     1457
     1458            $query = '
    14211459SELECT '.$field.'
    14221460  FROM '.$table.'
    1423   WHERE category_id = '.$new_parent.'
    1424 ;';
    1425           $parent_access = array_from_query($query, $field);
     1461  WHERE cat_id = '.$new_parent.'
     1462;';
     1463            $parent_access = array_from_query($query, $field);
    14261464         
    1427           $to_delete = array_diff($parent_access, $category_access);
     1465            $to_delete = array_diff($parent_access, $category_access);
    14281466         
    1429           if (count($to_delete) > 0)
    1430           {
    1431             $query = '
     1467            if (count($to_delete) > 0)
     1468            {
     1469              $query = '
    14321470DELETE FROM '.$table.'
    14331471  WHERE '.$field.' IN ('.implode(',', $to_delete).')
    1434     AND category_id IN ('.implode(',', $subcats).')
    1435 ;';
    1436             pwg_query($query);
     1472    AND cat_id IN ('.implode(',', $subcats).')
     1473;';
     1474              pwg_query($query);
     1475            }
    14371476          }
     1477          break;
    14381478        }
    1439         break;
    1440       }
    1441     }
    1442   }
     1479      }
     1480    }
     1481  }
     1482
     1483  array_push(
     1484    $page['infos'],
     1485    sprintf(
     1486      l10n('%d categories moved'),
     1487      count($categories)
     1488      )
     1489    );
    14431490}
    14441491?>
Note: See TracChangeset for help on using the changeset viewer.