Changeset 4445 for trunk/include/ws_functions.inc.php
- Timestamp:
- Dec 7, 2009, 11:30:02 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/ws_functions.inc.php
r4425 r4445 1731 1731 ws_add_image_category_relations( 1732 1732 $params['image_id'], 1733 $params['categories'] 1733 $params['categories'], 1734 $params['replace_mode'] 1734 1735 ); 1735 1736 } … … 1739 1740 { 1740 1741 include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); 1741 add_tags( 1742 explode(',', $params['tag_ids']), 1743 array($params['image_id']) 1744 ); 1742 1743 $tag_ids = explode(',', $params['tag_ids']); 1744 1745 if ($params['replace_mode']) 1746 { 1747 set_tags( 1748 $tag_ids, 1749 $params['image_id'] 1750 ); 1751 } 1752 else 1753 { 1754 add_tags( 1755 $tag_ids, 1756 array($params['image_id']) 1757 ); 1758 } 1745 1759 } 1746 1760 … … 1748 1762 } 1749 1763 1750 function ws_add_image_category_relations($image_id, $categories_string )1764 function ws_add_image_category_relations($image_id, $categories_string, $replace_mode=false) 1751 1765 { 1752 1766 // let's add links between the image and the categories … … 1766 1780 @list($cat_id, $rank) = explode(',', $token); 1767 1781 1782 if (!preg_match('/^\d+$/', $cat_id)) 1783 { 1784 continue; 1785 } 1786 1768 1787 array_push($cat_ids, $cat_id); 1769 1788 … … 1782 1801 $cat_ids = array_unique($cat_ids); 1783 1802 1784 if (count($cat_ids) > 0) 1785 { 1786 if ($search_current_ranks) 1803 if (count($cat_ids) == 0) 1804 { 1805 new PwgError( 1806 500, 1807 '[ws_add_image_category_relations] there is no category defined in "'.$categories_string.'"' 1808 ); 1809 exit(); 1810 } 1811 1812 $query = ' 1813 SELECT 1814 id 1815 FROM '.CATEGORIES_TABLE.' 1816 WHERE id IN ('.implode(',', $cat_ids).') 1817 ;'; 1818 $db_cat_ids = array_from_query($query, 'id'); 1819 1820 $unknown_cat_ids = array_diff($cat_ids, $db_cat_ids); 1821 if (count($unknown_cat_ids) != 0) 1822 { 1823 new PwgError( 1824 500, 1825 '[ws_add_image_category_relations] the following categories are unknown: '.implode(', ', $unknown_cat_ids) 1826 ); 1827 exit(); 1828 } 1829 1830 $to_update_cat_ids = array(); 1831 1832 // in case of replace mode, we first check the existing associations 1833 $query = ' 1834 SELECT 1835 category_id 1836 FROM '.IMAGE_CATEGORY_TABLE.' 1837 WHERE image_id = '.$image_id.' 1838 ;'; 1839 $existing_cat_ids = array_from_query($query, 'category_id'); 1840 1841 if ($replace_mode) 1842 { 1843 $to_remove_cat_ids = array_diff($existing_cat_ids, $cat_ids); 1844 if (count($to_remove_cat_ids) > 0) 1787 1845 { 1788 1846 $query = ' 1847 DELETE 1848 FROM '.IMAGE_CATEGORY_TABLE.' 1849 WHERE image_id = '.$image_id.' 1850 AND category_id IN ('.implode(', ', $to_remove_cat_ids).') 1851 ;'; 1852 pwg_query($query); 1853 update_category($to_remove_cat_ids); 1854 } 1855 } 1856 1857 $new_cat_ids = array_diff($cat_ids, $existing_cat_ids); 1858 if (count($new_cat_ids) == 0) 1859 { 1860 return true; 1861 } 1862 1863 if ($search_current_ranks) 1864 { 1865 $query = ' 1789 1866 SELECT 1790 1867 category_id, … … 1792 1869 FROM '.IMAGE_CATEGORY_TABLE.' 1793 1870 WHERE rank IS NOT NULL 1794 AND category_id IN ('.implode(',', $ cat_ids).')1871 AND category_id IN ('.implode(',', $new_cat_ids).') 1795 1872 GROUP BY category_id 1796 1873 ;'; 1797 $current_rank_of = simple_hash_from_query( 1798 $query, 1799 'category_id', 1800 'max_rank' 1801 ); 1802 1803 foreach ($cat_ids as $cat_id) 1874 $current_rank_of = simple_hash_from_query( 1875 $query, 1876 'category_id', 1877 'max_rank' 1878 ); 1879 1880 foreach ($new_cat_ids as $cat_id) 1881 { 1882 if (!isset($current_rank_of[$cat_id])) 1804 1883 { 1805 if (!isset($current_rank_of[$cat_id])) 1806 { 1807 $current_rank_of[$cat_id] = 0; 1808 } 1809 1810 if ('auto' == $rank_on_category[$cat_id]) 1811 { 1812 $rank_on_category[$cat_id] = $current_rank_of[$cat_id] + 1; 1813 } 1884 $current_rank_of[$cat_id] = 0; 1814 1885 } 1815 } 1816 1817 $inserts = array(); 1818 1819 foreach ($cat_ids as $cat_id) 1820 { 1821 array_push( 1822 $inserts, 1823 array( 1824 'image_id' => $image_id, 1825 'category_id' => $cat_id, 1826 'rank' => $rank_on_category[$cat_id], 1827 ) 1828 ); 1829 } 1830 1831 include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); 1832 mass_inserts( 1833 IMAGE_CATEGORY_TABLE, 1834 array_keys($inserts[0]), 1835 $inserts 1836 ); 1837 1838 update_category($cat_ids); 1839 } 1886 1887 if ('auto' == $rank_on_category[$cat_id]) 1888 { 1889 $rank_on_category[$cat_id] = $current_rank_of[$cat_id] + 1; 1890 } 1891 } 1892 } 1893 1894 $inserts = array(); 1895 1896 foreach ($new_cat_ids as $cat_id) 1897 { 1898 array_push( 1899 $inserts, 1900 array( 1901 'image_id' => $image_id, 1902 'category_id' => $cat_id, 1903 'rank' => $rank_on_category[$cat_id], 1904 ) 1905 ); 1906 } 1907 1908 include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); 1909 mass_inserts( 1910 IMAGE_CATEGORY_TABLE, 1911 array_keys($inserts[0]), 1912 $inserts 1913 ); 1914 1915 update_category($new_cat_ids); 1840 1916 } 1841 1917
Note: See TracChangeset
for help on using the changeset viewer.