Ignore:
Timestamp:
Apr 5, 2006, 12:29:35 AM (18 years ago)
Author:
plg
Message:

feature deleted: code for categories link was too complicated for such a
simple fature. Replaced by static association. Links are not persistent
anymore.

modification removed: #image_category.is_storage replaced by
#images.storage_category_id as in branche 1.5..

File:
1 edited

Legend:

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

    r1119 r1121  
    153153  // destruction of all the related elements
    154154  $query = '
    155 SELECT image_id
    156   FROM '.IMAGE_CATEGORY_TABLE.'
    157   WHERE is_storage = \'true\'
    158     AND category_id IN ('.
    159     wordwrap(
    160       implode(', ', $ids),
    161       80,
    162       "\n"
    163       ).
    164     ')
     155SELECT id
     156  FROM '.IMAGES_TABLE.'
     157  WHERE storage_category_id IN (
     158'.wordwrap(implode(', ', $ids), 80, "\n").')
    165159;';
    166160  $result = pwg_query($query);
     
    168162  while ($row = mysql_fetch_array($result))
    169163  {
    170     array_push($element_ids, $row['image_id']);
     164    array_push($element_ids, $row['id']);
    171165  }
    172166  delete_elements($element_ids);
     
    194188;';
    195189  pwg_query($query);
    196 
    197   // source/destination links deletion
    198   $query = '
    199 SELECT destination, source
    200   FROM '.CATEGORIES_LINK_TABLE.'
    201   WHERE source IN ('.implode(',', $ids).')
    202     OR destination IN ('.implode(',', $ids).')
    203 ;';
    204   $result = pwg_query($query);
    205 
    206   $sources_of = array();
    207 
    208   while ($row = mysql_fetch_array($result))
    209   {
    210     if (!isset($sources_of[ $row['destination'] ]))
    211     {
    212       $sources_of[ $row['destination'] ] = array();
    213     }
    214 
    215     array_push(
    216       $sources_of[ $row['destination'] ],
    217       $row['source']
    218       );
    219   }
    220 
    221   foreach ($sources_of as $destination => $sources)
    222   {
    223     delete_sources($destination, $sources);
    224   }
    225 
    226   update_category();
    227190
    228191  // destruction of the category
     
    442405       COUNT(image_id) AS nb_images,
    443406       MAX(date_available) AS date_last
    444   FROM '.IMAGES_TABLE.'
    445     INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
     407  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
    446408  WHERE category_id IN ('.wordwrap(implode(', ', $cat_ids), 80, "\n").')
    447409  GROUP BY category_id
     
    450412  $datas = array();
    451413  $query_ids = array();
    452   while ($row = mysql_fetch_array($result))
     414  while ( $row = mysql_fetch_array( $result ) )
    453415  {
    454416    array_push($query_ids, $row['category_id']);
     
    467429  foreach (array_diff($cat_ids, $query_ids) as $id)
    468430  {
    469     array_push(
    470       $datas,
    471       array(
    472         'id'        => $id,
    473         'nb_images' => 0,
    474         )
    475       );
    476   }
    477 
    478   mass_updates(
    479     CATEGORIES_TABLE,
    480     array(
    481       'primary' => array('id'),
    482       'update'  => array('date_last', 'nb_images')
    483       ),
    484     $datas
    485     );
     431    array_push($datas, array('id' => $id, 'nb_images' => 0));
     432  }
     433
     434  $fields = array('primary' => array('id'),
     435                  'update'  => array('date_last', 'nb_images'));
     436  mass_updates(CATEGORIES_TABLE, $fields, $datas);
    486437
    487438  // representative pictures
     
    13581309function update_path()
    13591310{
    1360   $images_of = array();
    1361 
    1362   $query = '
    1363 SELECT category_id, image_id
    1364   FROM '.IMAGE_CATEGORY_TABLE.'
    1365   WHERE is_storage = \'true\'
    1366 ;';
    1367   $result = pwg_query($query);
    1368   while ($row = mysql_fetch_array($result))
    1369   {
    1370     if (!isset($images_of[ $row['category_id'] ]))
    1371     {
    1372       $images_of[ $row['category_id'] ] = array();
    1373     }
    1374 
    1375     array_push(
    1376       $images_of[ $row['category_id'] ],
    1377       $row['image_id']
    1378       );
    1379   }
    1380 
    1381   $fulldirs = get_fulldirs(
    1382     array_keys($images_of)
    1383     );
    1384 
    1385   foreach ($images_of as $cat_id => $image_ids)
     1311  $query = '
     1312SELECT DISTINCT(storage_category_id)
     1313  FROM '.IMAGES_TABLE.'
     1314;';
     1315  $cat_ids = array_from_query($query, 'storage_category_id');
     1316  $fulldirs = get_fulldirs($cat_ids);
     1317
     1318  foreach ($cat_ids as $cat_id)
    13861319  {
    13871320    $query = '
    13881321UPDATE '.IMAGES_TABLE.'
    13891322  SET path = CONCAT(\''.$fulldirs[$cat_id].'\',\'/\',file)
    1390   WHERE id IN ('.
    1391       wordwrap(
    1392         implode(', ', $image_ids),
    1393         80,
    1394         "\n").
    1395       ')
     1323  WHERE storage_category_id = '.$cat_id.'
    13961324;';
    13971325    pwg_query($query);
     
    16141542
    16151543/**
    1616  * Returns all destinations of a list of source categories. This function
    1617  * solves transitivity.
    1618  *
    1619  * @param mixed array of category ids, empty for all categories
    1620  */
    1621 function get_destinations($categories = 'all')
    1622 {
    1623   $query = '
    1624 SELECT source, destination
    1625   FROM '.CATEGORIES_LINK_TABLE.'
    1626 ';
    1627   $result = pwg_query($query);
    1628 
    1629   $destinations_of = array();
    1630 
    1631   while ($row = mysql_fetch_array($result))
    1632   {
    1633     if (!isset($destinations_of[ $row['source'] ]))
    1634     {
    1635       $destinations_of[ $row['source'] ] = array();
    1636     }
    1637 
    1638     array_push(
    1639       $destinations_of[ $row['source'] ],
    1640       $row['destination']
    1641       );
    1642   }
    1643 
    1644   // transitivity resolution: if " => " means "source of", if A=>B=>C
    1645   // implies A=>B and A=>C. So A has 2 destinations: B and C.
    1646   do
    1647   {
    1648     // let's suppose we only need a single turn
    1649     $need_new_turn = false;
    1650 
    1651     foreach ($destinations_of as $source => $destinations)
    1652     {
    1653       foreach ($destinations as $destination)
    1654       {
    1655         // does the current destination has destinations itself?
    1656         if (isset($destinations_of[$destination]))
    1657         {
    1658           // are there destinations of current destination not already among
    1659           // destinations of the current source? (advise: take a piece of
    1660           // paper and draw a schema). The source itself must not be counted
    1661           // as a destination, thus avoiding cyclic links.
    1662           $missing_destinations = array_diff(
    1663             $destinations_of[$destination],
    1664             $destinations,
    1665             array($source) // no cyclic link
    1666             );
    1667 
    1668           if (count($missing_destinations) > 0)
    1669           {
    1670             $destinations_of[$source] = array_unique(
    1671               array_merge(
    1672                 $destinations,
    1673                 $missing_destinations
    1674                 )
    1675               );
    1676 
    1677             // a category has a least one new destination, we have to check
    1678             // one more time that it doesn't generate more destinations
    1679             $need_new_turn = true;
    1680           }
    1681         }
    1682       }
    1683     }
    1684   } while ($need_new_turn);
    1685 
    1686   if (is_array($categories))
    1687   {
    1688     $filtered_destinations_of = array();
    1689 
    1690     // Even if there is no destinations for the requested categories, we
    1691     // return empty arrays
    1692     foreach ($categories as $category)
    1693     {
    1694       $filtered_destinations_of[$category] = array();
    1695     }
    1696 
    1697     foreach ($destinations_of as $source => $destinations)
    1698     {
    1699       if (in_array($source, $categories))
    1700       {
    1701         $filtered_destinations_of[$source] = $destinations;
    1702       }
    1703     }
    1704 
    1705     return $filtered_destinations_of;
    1706   }
    1707   else
    1708   {
    1709     return $destinations_of;
    1710   }
    1711 }
    1712 
    1713 /**
    1714  * Returns all sources of a list of destination categories. This function
    1715  * solves transitivity.
    1716  *
    1717  * @param mixed array of category ids, empty for all categories
    1718  */
    1719 function get_sources($categories = 'all')
    1720 {
    1721   $destinations_of = get_destinations();
    1722 
    1723   $sources_of = array();
    1724 
    1725   foreach ($destinations_of as $source => $destinations)
    1726   {
    1727     foreach ($destinations as $destination)
    1728     {
    1729       if (!isset($sources_of[$destination]))
    1730       {
    1731         $sources_of[$destination] = array();
    1732       }
    1733 
    1734       array_push($sources_of[$destination], $source);
    1735     }
    1736   }
    1737 
    1738   // eventually, filter
    1739   if (is_array($categories))
    1740   {
    1741     $filtered_sources_of = array();
    1742 
    1743     // Even if there is no sources for the requested categories, we return
    1744     // empty arrays
    1745     foreach ($categories as $category)
    1746     {
    1747       $filtered_sources_of[$category] = array();
    1748     }
    1749 
    1750     foreach ($sources_of as $destination => $sources)
    1751     {
    1752       if (in_array($destination, $categories))
    1753       {
    1754         $filtered_sources_of[$destination] = $sources;
    1755       }
    1756     }
    1757 
    1758     return $filtered_sources_of;
    1759   }
    1760   else
    1761   {
    1762     return $sources_of;
    1763   }
    1764 }
    1765 
    1766 /**
    1767  * Checks categories links are respected for a given list of destinations.
    1768  *
    1769  * Checking categories links means that each destination must be associated
    1770  * to the images of its sources.
    1771  *
    1772  * @param mixed source category ids
    1773  */
    1774 function check_links($destinations = 'all')
    1775 {
    1776   $sources_of = get_sources($destinations);
    1777 
    1778   if (empty($sources_of))
    1779   {
    1780     return true;
    1781   }
    1782 
    1783   // we need to search images of all sources and destinations
    1784   $images_of = array();
    1785 
    1786   foreach ($sources_of as $destination => $sources)
    1787   {
    1788     $images_of[$destination] = array();
    1789 
    1790     foreach ($sources as $source)
    1791     {
    1792       $images_of[$source] = array();
    1793     }
    1794   }
    1795 
    1796   $query = '
    1797 SELECT image_id, category_id
    1798   FROM '.IMAGE_CATEGORY_TABLE.'
    1799   WHERE category_id IN ('.
    1800     implode(',', array_keys($images_of)).
    1801     ')
    1802 ;';
    1803   $result = pwg_query($query);
    1804 
    1805   while ($row = mysql_fetch_array($result))
    1806   {
    1807     array_push(
    1808       $images_of[ $row['category_id'] ],
    1809       $row['image_id']
    1810       );
    1811   }
    1812 
    1813   $inserts = array();
    1814 
    1815   foreach ($sources_of as $destination => $sources)
    1816   {
    1817     // merge all images from the sources of this destination
    1818     $sources_images = array();
    1819 
    1820     foreach ($sources as $source)
    1821     {
    1822       $sources_images = array_merge(
    1823         $sources_images,
    1824         $images_of[$source]
    1825         );
    1826     }
    1827 
    1828     $sources_images = array_unique($sources_images);
    1829 
    1830     // are there images among the sources that are not linked to the
    1831     // destination?
    1832     $missing_images = array_diff(
    1833       $sources_images,
    1834       $images_of[$destination]
    1835       );
    1836 
    1837     // if we find missing images (missing links in reality), we prepare the
    1838     // final mass_inserts
    1839     if (count($missing_images) > 0)
    1840     {
    1841       foreach ($missing_images as $missing_image)
    1842       {
    1843         array_push(
    1844           $inserts,
    1845           array(
    1846             'category_id' => $destination,
    1847             'image_id'    => $missing_image,
    1848             )
    1849           );
    1850       }
    1851     }
    1852   }
    1853 
    1854   if (count($inserts) > 0)
    1855   {
    1856     mass_inserts(
    1857       IMAGE_CATEGORY_TABLE,
    1858       array_keys($inserts[0]),
    1859       $inserts
    1860       );
    1861   }
    1862 }
    1863 
    1864 /**
    1865  * Based on categories links, delete image_category links on destinations.
    1866  *
    1867  * The rule is the following: if an image belong to the category and to the
    1868  * source, we suppose it comes from the source. If the source/destination
    1869  * link is broken, we delete the image/category link if the only origin of
    1870  * the link was the broken categories link.
    1871  *
    1872  * Example: "=>" means "source of". Between brackets the associated images.
    1873  *
    1874  * A (1,2,9) => \
    1875  *               |=> C (1,2,3,4,5,9) => D (1,2,3,4,5,6,9)
    1876  * B (3,4,9) => /
    1877  *
    1878  * In category C, we suppose (1,2) come from A, (3,4) from B, 9 from A or B
    1879  * and 5 was manually added. In category D, 6 was added manually.
    1880  *
    1881  * If we break A=>C, C and D loose (1,2) but not 9 because it can come from
    1882  * B. If we break C=>D, D loose (3,4,5,9) but not 6 because it was
    1883  * associated manually to 9.
    1884  *
    1885  * Warning: only virtual links can be removed, physical links are protected.
    1886  *
    1887  * @param int destination
    1888  * @param array sources
    1889  */
    1890 function delete_sources($destination, $sources)
    1891 {
    1892   // if no sources to unlink, we stop with OK status
    1893   if (count($sources) == 0)
    1894   {
    1895     return true;
    1896   }
    1897 
    1898   $query = '
    1899 DELETE
    1900   FROM '.CATEGORIES_LINK_TABLE.'
    1901   WHERE destination = '.$destination.'
    1902     AND source IN ('.implode(',', $sources).')
    1903 ;';
    1904   pwg_query($query);
    1905 
    1906   // The strategy is the following:
    1907   //
    1908   // * first we brutally delete the image/category associations on
    1909   // destinations categories for all images belonging to sources.
    1910   //
    1911   // * then we check_links on destinations to rebuild missing image/category
    1912   // associations.
    1913 
    1914   // what are the images associated to the sources to unlink
    1915   $query = '
    1916 SELECT image_id
    1917   FROM '.IMAGE_CATEGORY_TABLE.'
    1918   WHERE category_id IN ('.
    1919     implode(',', $sources).
    1920     ')
    1921 ;';
    1922   $sources_images = array_unique(
    1923     array_from_query($query, 'image_id')
    1924     );
    1925 
    1926   if (count($sources_images) == 0)
    1927   {
    1928     return true;
    1929   }
    1930 
    1931   // retrieve all direct and indirect destinations of the current
    1932   // destination
    1933   $destinations_of = get_destinations(array($destination));
    1934 
    1935   $destinations = array_merge(
    1936     array($destination),
    1937     $destinations_of[$destination]
    1938     );
    1939 
    1940   // unlink sources images from destinations
    1941   $query = '
    1942 DELETE
    1943   FROM '.IMAGE_CATEGORY_TABLE.'
    1944   WHERE category_id IN ('.implode(',', $destinations).')
    1945     AND image_id IN ('.implode(',', $sources_images).')
    1946     AND is_storage = \'false\'
    1947 ;';
    1948   pwg_query($query);
    1949 
    1950   // if the representative thumbnail of destinations was a picture from
    1951   // $sources_images, we request a new random representant
    1952   $query = '
    1953 SELECT id, representative_picture_id
    1954   FROM '.CATEGORIES_TABLE.'
    1955   WHERE id IN ('.implode(',', $destinations).')
    1956 ;';
    1957   $result = pwg_query($query);
    1958 
    1959   $request_random = array();
    1960 
    1961   while ($row = mysql_fetch_array($result))
    1962   {
    1963     if (isset($row['representative_picture_id']))
    1964     {
    1965       if (in_array($row['representative_picture_id'], $sources_images))
    1966       {
    1967         array_push($request_random, $row['id']);
    1968       }
    1969     }
    1970   }
    1971 
    1972   set_random_representant($request_random);
    1973 
    1974   // eventually, we check_links to rebuild missing associations
    1975   check_links($destinations);
    1976 
    1977   return true;
    1978 }
    1979 
    1980 /**
    19811544 * create a virtual category
    19821545 *
     
    23001863
    23011864}
     1865
     1866/**
     1867 * Associate a list of images to a list of categories.
     1868 *
     1869 * The function will not duplicate links
     1870 *
     1871 * @param array images
     1872 * @param array categories
     1873 * @return void
     1874 */
     1875function associate_images_to_categories($images, $categories)
     1876{
     1877  if (count($images) == 0
     1878      or count($categories) == 0)
     1879  {
     1880    return false;
     1881  }
     1882
     1883  $query = '
     1884DELETE
     1885  FROM '.IMAGE_CATEGORY_TABLE.'
     1886  WHERE image_id IN ('.implode(',', $images).')
     1887    AND category_id IN ('.implode(',', $categories).')
     1888;';
     1889  pwg_query($query);
     1890
     1891  $inserts = array();
     1892  foreach ($categories as $category_id)
     1893  {
     1894    foreach ($images as $image_id)
     1895    {
     1896      array_push(
     1897        $inserts,
     1898        array(
     1899          'image_id' => $image_id,
     1900          'category_id' => $category_id,
     1901          )
     1902        );
     1903    }
     1904  }
     1905 
     1906  mass_inserts(
     1907    IMAGE_CATEGORY_TABLE,
     1908    array_keys($inserts[0]),
     1909    $inserts
     1910    );
     1911
     1912  update_category($categories);
     1913}
     1914
     1915/**
     1916 * Associate images associated to a list of source categories to a list of
     1917 * destination categories.
     1918 *
     1919 * @param array sources
     1920 * @param array destinations
     1921 * @return void
     1922 */
     1923function associate_categories_to_categories($sources, $destinations)
     1924{
     1925  if (count($sources) == 0)
     1926  {
     1927    return false;
     1928  }
     1929
     1930  $query = '
     1931SELECT image_id
     1932  FROM '.IMAGE_CATEGORY_TABLE.'
     1933  WHERE category_id IN ('.implode(',', $sources).')
     1934;';
     1935  $images = array_from_query($query, 'image_id');
     1936
     1937  associate_images_to_categories($images, $destinations);
     1938}
    23021939?>
Note: See TracChangeset for help on using the changeset viewer.