/usr/share/horde/whups/lib/Driver/Sql.php is in php-horde-whups 3.0.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 | <?php
/**
* Whups backend driver for the Horde_Db abstraction layer.
*
* Copyright 2001-2002 Robert E. Coyle <robertecoyle@hotmail.com>
* Copyright 2001-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsdl.php.
*
* @author Robert E. Coyle <robertecoyle@hotmail.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @package Whups
*/
class Whups_Driver_Sql extends Whups_Driver
{
/**
* The database connection object.
*
* @var Horde_Db_Adapter_Base
*/
protected $_db;
/**
* A mapping of attributes from generic Whups names to DB backend fields.
*
* @var array
*/
protected $_map = array(
'id' => 'ticket_id',
'summary' => 'ticket_summary',
'requester' => 'user_id_requester',
'queue' => 'queue_id',
'version' => 'version_id',
'type' => 'type_id',
'state' => 'state_id',
'priority' => 'priority_id',
'timestamp' => 'ticket_timestamp',
'due' => 'ticket_due',
'date_updated' => 'date_updated',
'date_assigned' => 'date_assigned',
'date_resolved' => 'date_resolved'
);
/**
* Local cache for guest email addresses.
*
* @var array
*/
protected $_guestEmailCache = array();
/**
* Local cache of internal queue hashes
*
* @var array
*/
protected $_internalQueueCache = array();
/**
* Local queues internal cache
*
* @var array
*/
protected $_queues = null;
/**
* Local slug cache
*
* @var array
*/
protected $_slugs = null;
public function setStorage($storage)
{
if (!($storage instanceof Horde_Db_Adapter_Base)) {
throw new InvalidArgumentException("Missing Horde_Db_Adapter_Base");
}
$this->_db = $storage;
}
/**
* Adds a new queue.
*
* @params string $name A queue name.
* @params string $description A queue description.
* @params string $slug A queue slug.
* @params string $email A queue email address.
*
* @return integer The new queue ID.
* @throws Whups_Exception
*/
public function addQueue($name, $description, $slug = '', $email = '')
{
// Check for slug uniqueness.
if (strlen($slug) &&
$this->_db->selectValue('SELECT 1 FROM whups_queues WHERE queue_slug = ?', array($slug))) {
throw new Whups_Exception(
_("That queue slug is already taken. Please select another."));
}
try {
return $this->_db->insert(
'INSERT INTO whups_queues (queue_name, queue_description, '
. 'queue_slug, queue_email) VALUES (?, ?, ?, ?)',
array($this->_toBackend($name),
$this->_toBackend($description),
$slug,
$email));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Adds a new ticket type.
*
* @param string $name A type name.
* @param string $description A type description.
*
* @return integer The new type ID.
* @throws Whups_Exception
*/
public function addType($name, $description)
{
try {
return $this->_db->insert(
'INSERT INTO whups_types (type_name, type_description) '
. 'VALUES (?, ?)',
array($this->_toBackend($name),
$this->_toBackend($description)));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Adds a new state to a ticket type.
*
* @param integer $typeId A ticket type ID.
* @param string $name A state name
* @param string $description A state description.
* @param string $category A state category.
*
* @return integer The new state ID.
* @throws Whups_Exception
*/
public function addState($typeId, $name, $description, $category)
{
try {
return $this->_db->insert(
'INSERT INTO whups_states (type_id, state_name, '
. 'state_description, state_category) VALUES (?, ?, ?, ?)',
array((int)$typeId,
$this->_toBackend($name),
$this->_toBackend($description),
$this->_toBackend($category)));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Adds a new priority to a ticket type.
*
* @param integer $typeId A ticket type ID.
* @param string $name A priority name.
* @param string $description A priority description.
*
* @return integer The new priority ID.
* @throws Whups_Exception
*/
public function addPriority($typeId, $name, $description)
{
try {
return $this->_db->insert(
'INSERT INTO whups_priorities (type_id, priority_name, '
. 'priority_description) VALUES (?, ?, ?)',
array((int)$typeId,
$this->_toBackend($name),
$this->_toBackend($description)));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Adds a new version to a queue.
*
* @param integer $queueId A queue ID.
* @param string $name A version name.
* @param string $description A version description.
* @param boolean $active Whether the version is active.
*
* @return integer The new version ID.
* @throws Whups_Exception
*/
public function addVersion($queueId, $name, $description, $active)
{
try {
return $this->_db->insert(
'INSERT INTO whups_versions (queue_id, version_name, '
. 'version_description, version_active) '
. 'VALUES (?, ?, ?, ?)',
array((int)$queueId,
$this->_toBackend($name),
$this->_toBackend($description),
(boolean)$active));
} catch (Horde_Db_Exception $e) {
throw Whups_Exception($e);
}
}
/**
* Adds a form reply.
*
* @param integer $type A ticket type ID.
* @param string $name A reply name.
* @param string $text A reply text.
*
* @return integer The new form reply ID.
* @throws Whups_Exception
*/
public function addReply($type, $name, $text)
{
try {
return $this->_db->insert(
'INSERT INTO whups_replies (type_id, reply_name, reply_text) '
. 'VALUES (?, ?, ?)',
array((int)$type,
$this->_toBackend($name),
$this->_toBackend($text)));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Adds a ticket.
*
* @param array $info A ticket info hash. Will get a
* 'last-transaction' value added.
* @param string $requester A ticket requester.
*
* @return integer The new ticket ID.
* @throws Whups_Exception
*/
public function addTicket(array &$info, $requester)
{
$timestamp = time();
$type = (int)$info['type'];
$state = $this->getState((int)$info['state']);
$priority = (int)$info['priority'];
$queue = (int)$info['queue'];
$summary = $info['summary'];
$version = (int)isset($info['version']) ? $info['version'] : null;
$due = isset($info['due']) ? $info['due'] : null;
$comment = $info['comment'];
$attributes = isset($info['attributes']) ? $info['attributes'] : array();
// Create the ticket.
try {
$ticket_id = $this->_db->insert(
'INSERT INTO whups_tickets (ticket_summary, '
. 'user_id_requester, type_id, state_id, priority_id, '
. 'queue_id, ticket_timestamp, ticket_due, date_updated, '
. 'date_assigned, version_id)'
. ' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
array($this->_toBackend($summary),
$requester,
$type,
$state['id'],
$priority,
$queue,
$timestamp,
$due,
$timestamp,
$state['category'] == 'assigned' ? $timestamp : null,
$version));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
// Is there a more effecient way to do this? Need the ticketId before
// we can insert this.
if (!empty($info['user_email'])) {
$requester = $ticket_id * -1;
try {
$this->_db->update(
'UPDATE whups_tickets SET user_id_requester = ? WHERE '
. 'ticket_id = ?',
array($requester, $ticket_id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
if ($requester < 0) {
try {
$this->_db->insert(
'INSERT INTO whups_guests (guest_id, guest_email) '
. 'VALUES (?, ?)',
array((string)$requester, $info['user_email']));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
$commentId = $this->addComment(
$ticket_id, $comment, $requester,
isset($info['user_email']) ? $info['user_email'] : null);
// If permissions were specified, set them.
if (!empty($info['group'])) {
Whups_Ticket::addCommentPerms($commentId, $info['group']);
}
$transaction = $this->updateLog($ticket_id,
$requester,
array('state' => $state['id'],
'priority' => $priority,
'type' => $type,
'summary' => $summary,
'due' => $due,
'comment' => $commentId,
'queue' => $queue));
// Store the last-transaction id in the ticket's info for later use if
// needed.
$info['last-transaction'] = $transaction;
// Assign the ticket, if requested.
$owners = array_merge(
isset($info['owners']) ? $info['owners'] : array(),
isset($info['group_owners']) ? $info['group_owners'] : array());
foreach ($owners as $owner) {
$this->addTicketOwner($ticket_id, $owner);
$this->updateLog($ticket_id, $requester,
array('assign' => $owner),
$transaction);
}
// Set timestamps, if necessary.
if ($state['category'] == 'assigned') {
$this->updateLog($ticket_id, $requester,
array('date_assigned' => $timestamp),
$transaction);
}
// Add any supplied attributes for this ticket.
foreach ($attributes as $attribute_id => $attribute_value) {
$attribute_value = $this->_serializeAttribute($attribute_value);
$this->_setAttributeValue(
$ticket_id, $attribute_id, $attribute_value);
$this->updateLog(
$ticket_id, $requester,
array('attribute' => $attribute_id . ':' . $attribute_value,
'attribute_' . $attribute_id => $attribute_value),
$transaction);
}
return $ticket_id;
}
/**
* Adds a new ticket comment.
*
* @param integer $ticket_id A ticket ID.
* @param string $comment A comment text.
* @param string $creator The creator of the comment.
* @param string $creator_email The creator's email address.
*
* @return integer The new comment ID.
* @throws Whups_Exception
*/
public function addComment($ticket_id, $comment, $creator,
$creator_email = null)
{
// Add the row.
try {
$id = $this->_db->insert(
'INSERT INTO whups_comments (ticket_id, user_id_creator, '
. ' comment_text, comment_timestamp) VALUES (?, ?, ?, ?)',
array((int)$ticket_id,
$creator,
$this->_toBackend($comment),
time()));
if (empty($creator) || $creator < 0) {
$creator = '-' . $id . '_comment';
}
$this->_db->update(
'UPDATE whups_comments SET user_id_creator = ?'
. ' WHERE comment_id = ?',
array($creator, $id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
// Hacky. $creator is a string at this point, but it can still evaluate
// to a negative integer.
if ($creator < 0 && !empty($creator_email)) {
try {
$this->_db->insert(
'INSERT INTO whups_guests (guest_id, guest_email)'
. ' VALUES (?, ?)',
array((string)$creator, $creator_email));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
return $id;
}
/**
* Updates a ticket.
*
* Does not update the ticket log (so that it can be used for things
* low-level enough to not show up there. In general, you should *always*
* update the log; Whups_Ticket::commit() will take care of this in most
* cases).
*
* @param integer $ticketId A ticket ID.
* @param array $attributes An attribute hash.
*
* @throws Whups_Exception
*/
public function updateTicket($ticketId, $attributes)
{
if (!count($attributes)) {
return;
}
$query = '';
$values = array();
foreach ($attributes as $field => $value) {
if (empty($this->_map[$field])) {
continue;
}
$query .= $this->_map[$field] . ' = ?, ';
$values[] = $this->_toBackend($value);
}
// Don't try to execute an empty query (if we didn't find any updates
// to make).
if (empty($query)) {
return;
}
$values[] = (int)$ticketId;
try {
$this->_db->update(
'UPDATE whups_tickets SET ' . substr($query, 0, -2)
. ' WHERE ticket_id = ?',
$values);
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Adds a ticket owner.
*
* @param integer $ticketId A ticket ID.
* @param string $owner An owner ID.
*
* @throws Whups_Exception
*/
public function addTicketOwner($ticketId, $owner)
{
try {
$this->_db->insert(
'INSERT INTO whups_ticket_owners (ticket_id, ticket_owner) '
. 'VALUES (?, ?)',
array($ticketId, $owner));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Removes a ticket owner.
*
* @param integer $ticketId A ticket ID.
* @param string $owner An owner ID.
*
* @throws Whups_Exception
*/
public function deleteTicketOwner($ticketId, $owner)
{
try {
$this->_db->delete(
'DELETE FROM whups_ticket_owners WHERE ticket_owner = ? '
. 'AND ticket_id = ?',
array($owner, (int)$ticketId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Removes a ticket.
*
* @param integer $id A ticket ID.
*
* @throws Whups_Exception
*/
public function deleteTicket($id)
{
$id = (int)$id;
$tables = array(
'whups_ticket_listeners',
'whups_logs',
'whups_comments',
'whups_tickets',
'whups_attributes');
if (!empty($GLOBALS['conf']['vfs']['type'])) {
try {
$vfs = $GLOBALS['injector']
->getInstance('Horde_Core_Factory_Vfs')
->create();
} catch (Horde_Vfs_Exception $e) {
throw new Whups_Exception($e);
}
if ($vfs->isFolder(Whups::VFS_ATTACH_PATH, $id)) {
try {
$vfs->deleteFolder(Whups::VFS_ATTACH_PATH, $id, true);
} catch (Horde_Vfs_Exception $e) {
throw new Whups_Exception($e);
}
}
}
// Attempt to clean up everything.
try {
$txs = $this->_db->selectValues(
'SELECT DISTINCT transaction_id FROM whups_logs '
. 'WHERE ticket_id = ?',
array($id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$this->_db->beginDbTransaction();
foreach ($tables as $table) {
try {
$this->_db->delete(
'DELETE FROM ' . $table . ' WHERE ticket_id = ?',
array($id));
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
}
if (!empty($txs)) {
try {
$this->_db->delete(
'DELETE FROM whups_transactions WHERE transaction_id IN '
. '(' . str_repeat('?,', count($txs) - 1) . '?)',
$txs);
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
}
$this->_db->commitDbTransaction();
}
/**
* Executes a query.
*
* @param Whups_Query $query A query object.
* @param Horde_Variables $vars Request variables.
* @param boolean $get_details Whether to return all ticket details.
* @param boolean $munge @TODO (?)
*
* @return array List of ticket IDs or ticket details that match the query
* criteria.
* @throws Whups_Exception
*/
public function executeQuery(Whups_Query $query, Horde_Variables $vars,
$get_details = true, $munge = true)
{
$this->jtables = array();
$this->joins = array();
$where = $query->reduce(array($this, 'clauseFromQuery'), $vars);
if (!$where) {
$GLOBALS['notification']->push(_("No query to run"), 'horde.message');
return array();
}
if ($this->joins) {
$joins = implode(' ', $this->joins);
} else {
$joins = '';
}
try {
$ids = $this->_db->selectValues(
"SELECT whups_tickets.ticket_id FROM whups_tickets $joins "
. "WHERE $where");
} catch (Horde_Db_Exception $e) {
$GLOBALS['notification']->push($e->getMessage(), 'horde.error');
return array();
}
if (!count($ids)) {
return array();
}
if ($get_details) {
$ids = $this->getTicketsByProperties(array('id' => $ids), $munge);
}
return $ids;
}
public function clauseFromQuery($args, $type, $criterion, $cvalue,
$operator, $value)
{
switch ($type) {
case Whups_Query::TYPE_AND:
return $this->_concatClauses($args, 'AND');
case Whups_Query::TYPE_OR:
return $this->_concatClauses($args, 'OR');
case Whups_Query::TYPE_NOT:
return $this->_notClause($args);
case Whups_Query::TYPE_CRITERION:
return $this->_criterionClause($criterion, $cvalue, $operator, $value);
}
}
protected function _concatClauses($args, $conjunction)
{
$count = count($args);
if ($count == 0) {
$result = '';
} elseif ($count == 1) {
$result = $args[0];
} else {
$result = '(' . $args[0] . ')';
for ($i = 1; $i < $count; $i++) {
if ($args[$i] != '') {
$result .= ' ' . $conjunction . ' (' . $args[$i] . ')';
}
}
}
return $result;
}
protected function _notClause($args)
{
if (count($args) == 0) {
return '';
}
if (count($args) !== 1) {
throw new InvalidArgumentException();
}
return 'NOT (' . $args[0] . ')';
}
/**
* @todo: The RDBMS specific clauses should be refactored to use
* Horde_Db_Adapter_Base_Schema#buildClause
*
* @return string
*/
protected function _criterionClause($criterion, $cvalue, $operator, $value)
{
$func = '';
$funcend = '';
$value = $this->_toBackend($value);
switch ($operator) {
case Whups_Query::OPERATOR_GREATER: $op = '>'; break;
case Whups_Query::OPERATOR_LESS: $op = '<'; break;
case Whups_Query::OPERATOR_EQUAL: $op = '='; break;
case Whups_Query::OPERATOR_PATTERN: $op = 'LIKE'; break;
case Whups_Query::OPERATOR_CI_SUBSTRING:
$value = '%' . str_replace(array('%', '_'), array('\%', '\_'), $value) . '%';
if ($this->_db->phptype == 'pgsql') {
$op = 'ILIKE';
} else {
$op = 'LIKE';
$func = 'LOWER(';
$funcend = ')';
}
break;
case Whups_Query::OPERATOR_CS_SUBSTRING:
// FIXME: Does not work in Postgres.
$func = 'LOCATE(' . $this->_db->quoteString($value) . ', ';
$funcend = ')';
$op = '>';
$value = 0;
break;
case Whups_Query::OPERATOR_WORD:
// TODO: There might be a better way to avoid missing
// words at the start and end of the text field.
if ($this->_db->phptype == 'pgsql') {
$func = "' ' || ";
$funcend = " || ' '";
} else {
$func = "CONCAT(' ', CONCAT(";
$funcend = ", ' '))";
}
$op = 'LIKE';
$value = '%' . str_replace(array('%', '_'), array('\%', '\_'), $value) . '%';
break;
}
$qvalue = $this->_db->quoteString($value);
$done = false;
$text = '';
switch ($criterion) {
case Whups_Query::CRITERION_ID:
$text = "{$func}whups_tickets.ticket_id{$funcend}";
break;
case Whups_Query::CRITERION_QUEUE:
$text = "{$func}whups_tickets.queue_id{$funcend}";
break;
case Whups_Query::CRITERION_VERSION:
$text = "{$func}whups_tickets.version_id{$funcend}";
break;
case Whups_Query::CRITERION_TYPE:
$text = "{$func}whups_tickets.type_id{$funcend}";
break;
case Whups_Query::CRITERION_STATE:
$text = "{$func}whups_tickets.state_id{$funcend}";
break;
case Whups_Query::CRITERION_PRIORITY:
$text = "{$func}whups_tickets.priority_id{$funcend}";
break;
case Whups_Query::CRITERION_SUMMARY:
$text = "{$func}whups_tickets.ticket_summary{$funcend}";
break;
case Whups_Query::CRITERION_TIMESTAMP:
$text = "{$func}whups_tickets.ticket_timestamp{$funcend}";
break;
case Whups_Query::CRITERION_UPDATED:
$text = "{$func}whups_tickets.date_updated{$funcend}";
break;
case Whups_Query::CRITERION_RESOLVED:
$text = "{$func}whups_tickets.date_resolved{$funcend}";
break;
case Whups_Query::CRITERION_ASSIGNED:
$text = "{$func}whups_tickets.date_assigned{$funcend}";
break;
case Whups_Query::CRITERION_DUE:
$text = "{$func}whups_tickets.ticket_due{$funcend}";
break;
case Whups_Query::CRITERION_ATTRIBUTE:
$cvalue = (int)$cvalue;
if (!isset($this->jtables['whups_attributes'])) {
$this->jtables['whups_attributes'] = 1;
}
$v = $this->jtables['whups_attributes']++;
$this->joins[] = "LEFT JOIN whups_attributes wa$v ON (whups_tickets.ticket_id = wa$v.ticket_id AND wa$v.attribute_id = $cvalue)";
$text = "{$func}wa$v.attribute_value{$funcend} $op $qvalue";
$done = true;
break;
case Whups_Query::CRITERION_OWNERS:
if (!isset($this->jtables['whups_ticket_owners'])) {
$this->jtables['whups_ticket_owners'] = 1;
}
$v = $this->jtables['whups_ticket_owners']++;
$this->joins[] = "LEFT JOIN whups_ticket_owners wto$v ON whups_tickets.ticket_id = wto$v.ticket_id";
$qvalue = $this->_db->quotestring('user:' . $value);
$text = "{$func}wto$v.ticket_owner{$funcend} $op $qvalue";
$done = true;
break;
case Whups_Query::CRITERION_REQUESTER:
if (!isset($this->jtables['whups_guests'])) {
$this->jtables['whups_guests'] = 1;
}
$v = $this->jtables['whups_guests']++;
$this->joins[] = "LEFT JOIN whups_guests wg$v ON whups_tickets.user_id_requester = wg$v.guest_id";
$text = "{$func}whups_tickets.user_id_requester{$funcend} $op $qvalue OR {$func}wg$v.guest_email{$funcend} $op $qvalue";
$done = true;
break;
case Whups_Query::CRITERION_GROUPS:
if (!isset($this->jtables['whups_ticket_owners'])) {
$this->jtables['whups_ticket_owners'] = 1;
}
$v = $this->jtables['whups_ticket_owners']++;
$this->joins[] = "LEFT JOIN whups_ticket_owners wto$v ON whups_tickets.ticket_id = wto$v.ticket_id";
$qvalue = $this->_db->quoteString('group:' . $value);
$text = "{$func}wto$v.ticket_owner{$funcend} $op $qvalue";
$done = true;
break;
case Whups_Query::CRITERION_ADDED_COMMENT:
if (!isset($this->jtables['whups_comments'])) {
$this->jtables['whups_comments'] = 1;
}
$v = $this->jtables['whups_comments']++;
$this->joins[] = "LEFT JOIN whups_comments wc$v ON (whups_tickets.ticket_id = wc$v.ticket_id)";
$text = "{$func}wc$v.user_id_creator{$funcend} $op $qvalue";
$done = true;
break;
case Whups_Query::CRITERION_COMMENT:
if (!isset($this->jtables['whups_comments'])) {
$this->jtables['whups_comments'] = 1;
}
$v = $this->jtables['whups_comments']++;
$this->joins[] = "LEFT JOIN whups_comments wc$v ON (whups_tickets.ticket_id = wc$v.ticket_id)";
$text = "{$func}wc$v.comment_text{$funcend} $op $qvalue";
$done = true;
break;
}
if ($done == false) {
$text .= " $op $qvalue";
}
return $text;
}
/**
* Returns tickets by searching for its properties.
*
* @param array $info An array of properties to search for.
* @param boolean $munge Munge the query (?)
* @param boolean $perowner Group the results per owner?
*
* @return array An array of ticket information hashes.
* @throws Whups_Exception
*/
public function getTicketsByProperties(array $info, $munge = true,
$perowner = false)
{
if (isset($info['queue']) && !count($info['queue'])) {
return array();
}
// Search conditions.
$where = $this->_generateWhere(
'whups_tickets',
array('ticket_id', 'type_id', 'state_id', 'priority_id', 'queue_id'),
$info, 'integer');
$where2 = $this->_generateWhere(
'whups_tickets', array('user_id_requester'), $info, 'string');
if (empty($where)) {
$where = $where2;
} elseif (!empty($where2)) {
$where .= ' AND ' . $where2;
}
// Add summary filter if present.
if (!empty($info['summary'])) {
$where = $this->_addWhere(
$where, 1,
'LOWER(whups_tickets.ticket_summary) LIKE '
. $this->_db->quotestring('%' . Horde_String::lower($info['summary']) . '%'));
}
// Add date fields.
if (!empty($info['ticket_timestamp'])) {
$where = $this->_addDateWhere($where, $info['ticket_timestamp'], 'ticket_timestamp');
}
if (!empty($info['date_updated'])) {
$where = $this->_addDateWhere($where, $info['date_updated'], 'date_updated');
}
if (!empty($info['date_assigned'])) {
$where = $this->_addDateWhere($where, $info['date_assigned'], 'date_assigned');
}
if (!empty($info['date_resolved'])) {
$where = $this->_addDateWhere($where, $info['date_resolved'], 'date_resolved');
}
if (!empty($info['ticket_due'])) {
$where = $this->_addDateWhere($where, $info['ticket_due'], 'ticket_due');
}
$fields = array(
'ticket_id AS id',
'ticket_summary AS summary',
'user_id_requester',
'state_id AS state',
'type_id AS type',
'priority_id AS priority',
'queue_id AS queue',
'date_updated',
'date_assigned',
'date_resolved',
'version_id AS version');
$fields = $this->_prefixTableToColumns('whups_tickets', $fields)
. ', whups_tickets.ticket_timestamp AS timestamp, whups_tickets.ticket_due AS due';
$tables = 'whups_tickets';
$join = '';
$groupby = 'whups_tickets.ticket_id, whups_tickets.ticket_summary, whups_tickets.user_id_requester, whups_tickets.state_id, whups_tickets.type_id, whups_tickets.priority_id, whups_tickets.queue_id, whups_tickets.ticket_timestamp, whups_tickets.ticket_due, whups_tickets.date_updated, whups_tickets.date_assigned, whups_tickets.date_resolved';
// State filters.
if (isset($info['category'])) {
if (is_array($info['category'])) {
$cat = '';
foreach ($info['category'] as $category) {
if (!empty($cat)) {
$cat .= ' OR ';
}
$cat .= 'whups_states.state_category = '
. $this->_db->quotestring($category);
}
$cat = ' AND (' . $cat . ')';
} else {
$cat = isset($info['category'])
? ' AND whups_states.state_category = '
. $this->_db->quotestring($info['category'])
: '';
}
} else {
$cat = '';
}
// Type filters.
if (isset($info['type_id'])) {
if (is_array($info['type_id'])) {
$t = array();
foreach ($info['type_id'] as $type) {
$t[] = 'whups_tickets.type_id = '
. $this->_db->quotestring($type);
}
$t = ' AND (' . implode(' OR ', $t) . ')';
} else {
$t = isset($info['type_id'])
? ' AND whups_tickets.type_id = '
. $this->_db->quotestring($info['type_id'])
: '';
}
$this->_addWhere($where, $t, $t);
}
$nouc = isset($info['nouc'])
? " AND whups_states.state_category <> 'unconfirmed'" : '';
$nores = isset($info['nores'])
? " AND whups_states.state_category <> 'resolved'" : '';
$nonew = isset($info['nonew'])
? " AND whups_states.state_category <> 'new'" : '';
$noass = isset($info['noass'])
? " AND whups_states.state_category <> 'assigned'" : '';
$uc = isset($info['uc'])
? " AND whups_states.state_category = 'unconfirmed'" : '';
$res = isset($info['res'])
? " AND whups_states.state_category = 'resolved'" : '';
$new = isset($info['new'])
? " AND whups_states.state_category = 'new'" : '';
$ass = isset($info['ass'])
? " AND whups_states.state_category = 'assigned'" : '';
// If there are any state filters, add them in.
if ($nouc || $nores || $nonew || $noass ||
$uc || $res || $new || $ass || $cat) {
$where = $this->_addWhere($where, 1, "(whups_tickets.type_id = whups_states.type_id AND whups_tickets.state_id = whups_states.state_id$nouc$nores$nonew$noass$uc$res$new$ass$cat)");
}
// Initialize join clauses.
$join = '';
// Handle owner properties.
if (isset($info['owner'])) {
$join .= ' INNER JOIN whups_ticket_owners ON whups_tickets.ticket_id = whups_ticket_owners.ticket_id AND ';
if (is_array($info['owner'])) {
$clauses = array();
foreach ($info['owner'] as $owner) {
$clauses[] = 'whups_ticket_owners.ticket_owner = '
. $this->_db->quotestring($owner);
}
$join .= '(' . implode(' OR ', $clauses) . ')';
} else {
$join .= 'whups_ticket_owners.ticket_owner = '
. $this->_db->quotestring($info['owner']);
}
}
if (isset($info['notowner'])) {
if ($info['notowner'] === true) {
// Filter for tickets with no owner.
$join .= ' LEFT JOIN whups_ticket_owners ON whups_tickets.ticket_id = whups_ticket_owners.ticket_id AND whups_ticket_owners.ticket_owner IS NOT NULL';
} else {
$join .= ' LEFT JOIN whups_ticket_owners ON whups_tickets.ticket_id = whups_ticket_owners.ticket_id AND whups_ticket_owners.ticket_owner = ' . $this->_db->quotestring($info['notowner']);
}
$where = $this->_addWhere($where, 1,
'whups_ticket_owners.ticket_id IS NULL');
}
if ($munge) {
$myqueues = $GLOBALS['registry']->hasMethod('tickets/listQueues') == $GLOBALS['registry']->getApp();
$myversions = $GLOBALS['registry']->hasMethod('tickets/listVersions') == $GLOBALS['registry']->getApp();
$fields = "$fields, " .
'whups_types.type_name AS type_name, ' .
'whups_states.state_name AS state_name, ' .
'whups_states.state_category AS state_category, ' .
'whups_priorities.priority_name AS priority_name';
$join .=
' INNER JOIN whups_types ON whups_tickets.type_id = whups_types.type_id' .
' INNER JOIN whups_states ON whups_tickets.state_id = whups_states.state_id' .
' INNER JOIN whups_priorities ON whups_tickets.priority_id = whups_priorities.priority_id' .
' INNER JOIN whups_states state2 ON whups_tickets.type_id = state2.type_id';
$groupby .= ', whups_types.type_name, whups_states.state_name, whups_states.state_category';
if ($myversions) {
$versions = array();
$fields .= ', whups_versions.version_name AS version_name'
. ', whups_versions.version_description AS version_description'
. ', whups_versions.version_active AS version_active';
$join .= ' LEFT JOIN whups_versions ON whups_tickets.version_id = whups_versions.version_id';
$groupby .= ', whups_versions.version_name, whups_versions.version_description, whups_versions.version_active, whups_tickets.version_id';
}
if ($myqueues) {
$queues = array();
$fields .= ', whups_queues.queue_name AS queue_name';
$join .= ' INNER JOIN whups_queues ON whups_tickets.queue_id = whups_queues.queue_id';
$groupby .= ', whups_queues.queue_name';
}
$groupby .= ', whups_priorities.priority_name';
}
if ($perowner) {
$join .= ' LEFT JOIN whups_ticket_owners ON whups_tickets.ticket_id = whups_ticket_owners.ticket_id';
$fields .= ', whups_ticket_owners.ticket_owner AS owner';
$groupby .= ', whups_ticket_owners.ticket_owner';
}
$query = "SELECT $fields FROM $tables$join "
. (!empty($where) ? "WHERE $where " : '')
. 'GROUP BY ' . $groupby;
try {
$info = $this->_db->selectAll($query);
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
if (!count($info)) {
return array();
}
$info = $this->_fromBackend($info);
$tickets = array();
foreach ($info as $ticket) {
if ($munge) {
if (!$myqueues) {
if (!isset($queues[$ticket['queue']])) {
$queues[$ticket['queue']] = $GLOBALS['registry']->call(
'tickets/getQueueDetails',
array($ticket['queue']));
}
$ticket['queue_name'] = $queues[$ticket['queue']]['name'];
if (isset($queues[$ticket['queue']]['link'])) {
$ticket['queue_link'] = $queues[$ticket['queue']]['link'];
}
}
if (!$myversions) {
if (!isset($versions[$ticket['version']])) {
$versions[$ticket['version']] = $GLOBALS['registry']->call(
'tickets/getVersionDetails',
array($ticket['version']));
}
$ticket['version_name'] = $versions[$ticket['version']]['name'];
if (isset($versions[$ticket['version']]['link'])) {
$ticket['version_link'] = $versions[$ticket['version']]['link'];
}
}
$ticket['requester_formatted'] = Whups::formatUser($ticket['user_id_requester'], false, true, true);
}
$tickets[$ticket['id']] = $ticket;
}
foreach ($this->getOwners(array_keys($tickets)) as $id => $owners) {
$tickets[$id]['owners'] = $owners;
foreach($owners as $owner) {
$tickets[$id]['owners_formatted'][] = Whups::formatUser($owner, false, true, true);
}
}
$attributes = $this->getTicketAttributesWithNames(array_keys($tickets));
foreach ($attributes as $row) {
$attribute_id = 'attribute_' . $row['attribute_id'];
$tickets[$row['id']][$attribute_id] = $this->_json_decode($row['attribute_value']);
$tickets[$row['id']][$attribute_id . '_name'] = $row['attribute_name'];
}
return array_values($tickets);
}
/**
* Returns ticket details.
*
* @param integer $ticket A ticket ID.
* @param boolean $checkPerms Enforce permissions?
*
* @return array A ticket information hash.
* @throws Horde_Exception_NotFound
* @throws Horde_Exception_PermissionDenied
*/
public function getTicketDetails($ticket, $checkPerms = true)
{
$result = $this->getTicketsByProperties(array('id' => $ticket));
if (!isset($result[0])) {
throw new Horde_Exception_NotFound(
sprintf(_("Ticket %s was not found."), $ticket));
}
$queues = Whups::permissionsFilter(
$this->getQueues(), 'queue', Horde_Perms::READ,
$GLOBALS['registry']->getAuth(), $result[0]['user_id_requester']);
if ($checkPerms &&
!in_array($result[0]['queue'], array_flip($queues))) {
throw new Horde_Exception_PermissionDenied(
sprintf(_("You do not have permission to access this ticket (%s)."), $ticket));
}
return $result[0];
}
/**
* Returns a ticket state.
*
* @param integer $ticket_id A ticket ID.
*
* @return integer A state ID.
* @throws Whups_Exception
*/
public function getTicketState($ticket_id)
{
try {
return $this->_db->SelectOne(
'SELECT whups_tickets.state_id, whups_states.state_category '
. 'FROM whups_tickets INNER JOIN whups_states '
. 'ON whups_tickets.state_id = whups_states.state_id '
. 'WHERE ticket_id = ?',
array($ticket_id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Returns a guest's email address.
*
* @param string $guest_id A guest ID.
*
* @return string The guest's email address.
* @throws Whups_Exception
*/
public function getGuestEmail($guest_id)
{
if (!isset($this->_guestEmailCache[$guest_id])) {
try {
$result = $this->_db->selectValue(
'SELECT guest_email FROM whups_guests WHERE guest_id = ?',
array($guest_id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$this->_guestEmailCache[$guest_id] = $this->_fromBackend($result);
}
return $this->_guestEmailCache[$guest_id];
}
/**
* Returns a ticket's history.
*
* @param integer $ticket_id A ticket ID.
*
* @return array The ticket's history.
* @throws Whups_Exception
*/
protected function _getHistory($ticket_id)
{
$where = 'whups_logs.ticket_id = ' . (int)$ticket_id;
$join = 'LEFT JOIN whups_comments
ON whups_logs.log_type = \'comment\'
AND whups_logs.log_value_num = whups_comments.comment_id
LEFT JOIN whups_versions
ON whups_logs.log_type = \'version\'
AND whups_logs.log_value_num = whups_versions.version_id
LEFT JOIN whups_states
ON whups_logs.log_type = \'state\'
AND whups_logs.log_value_num = whups_states.state_id
LEFT JOIN whups_priorities
ON whups_logs.log_type = \'priority\'
AND whups_logs.log_value_num = whups_priorities.priority_id
LEFT JOIN whups_types
ON whups_logs.log_type = \'type\'
AND whups_logs.log_value_num = whups_types.type_id
LEFT JOIN whups_attributes_desc
ON whups_logs.log_type = \'attribute\'
AND whups_logs.log_value_num = whups_attributes_desc.attribute_id
LEFT JOIN whups_transactions
ON whups_logs.transaction_id = whups_transactions.transaction_id';
$fields = $this->_prefixTableToColumns('whups_comments',
array('comment_text'))
. ', whups_transactions.transaction_timestamp AS timestamp, whups_logs.ticket_id'
. ', whups_logs.log_type, whups_logs.log_value'
. ', whups_logs.log_value_num, whups_logs.log_id'
. ', whups_logs.transaction_id, whups_transactions.transaction_user_id user_id'
. ', whups_priorities.priority_name, whups_states.state_name, whups_versions.version_name'
. ', whups_types.type_name, whups_attributes_desc.attribute_name';
$query = "SELECT $fields FROM whups_logs $join WHERE $where "
. "ORDER BY whups_logs.transaction_id";
try {
$history = $this->_db->selectAll($query);
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$history = $this->_fromBackend($history);
for ($i = 0, $iMax = count($history); $i < $iMax; ++$i) {
if ($history[$i]['log_type'] == 'queue') {
$queue = $this->getQueue($history[$i]['log_value_num']);
$history[$i]['queue_name'] = $queue ? $queue['name'] : null;
}
}
return $history;
}
/**
* Deletes all changes of a transaction.
*
* @param integer $transaction A transaction ID.
* @throws Whups_Exception
*/
public function deleteHistory($transaction)
{
$transaction = (int)$transaction;
$this->_db->beginDbTransaction();
/* Deleting comments. */
try {
$comments = $this->_db->selectValues(
'SELECT log_value FROM whups_logs WHERE log_type = ? '
. 'AND transaction_id = ?',
array('comment', $transaction));
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
if ($comments) {
$query = sprintf(
'DELETE FROM whups_comments WHERE comment_id IN (%s)',
implode(',', $comments));
try {
$this->_db->delete($query);
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
}
/* Deleting attachments. */
if (isset($GLOBALS['conf']['vfs']['type'])) {
try {
$attachments = $this->_db->selectAll(
'SELECT ticket_id, log_value FROM whups_logs '
. 'WHERE log_type = ? AND transaction_id = ?',
array('attachment', $transaction));
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
$vfs = $GLOBALS['injector']
->getInstance('Horde_Core_Factory_Vfs')
->create();
foreach ($attachments as $attachment) {
$dir = Whups::VFS_ATTACH_PATH . '/' . $attachment['ticket_id'];
if ($vfs->exists($dir, $attachment['log_value'])) {
try {
$vfs->deleteFile($dir, $attachment['log_value']);
} catch (Horde_Vfs_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
} else {
Horde::log(sprintf(_("Attachment %s not found."), $attachment['log_value']), 'WARN');
}
}
}
try {
$this->_db->delete(
'DELETE FROM whups_logs WHERE transaction_id = ?',
array($transaction));
$this->_db->delete(
'DELETE FROM whups_transactions WHERE transaction_id = ?',
array($transaction));
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
$this->_db->commitDbTransaction();
}
/**
* Return a list of queues and the number of open tickets in each.
*
* @param array $queues Array of queue IDs to summarize.
*
* @return array A list of queue hashes.
* @throws Whups_Exception
*/
public function getQueueSummary($queue_ids)
{
$qstring = implode(', ', array_map('intval', $queue_ids));
$sql = 'SELECT q.queue_id AS id, q.queue_slug AS slug, '
. 'q.queue_name AS name, q.queue_description AS description, '
. 'ty.type_name as type, COUNT(t.ticket_id) AS open_tickets '
. 'FROM whups_queues q LEFT JOIN whups_tickets t '
. 'ON q.queue_id = t.queue_id '
. 'INNER JOIN whups_states s '
. 'ON (t.state_id = s.state_id AND s.state_category != \'resolved\') '
. 'INNER JOIN whups_types ty ON ty.type_id = t.type_id '
. 'WHERE q.queue_id IN (' . $qstring . ') '
. 'GROUP BY q.queue_id, q.queue_slug, q.queue_name, '
. 'q.queue_description, ty.type_name ORDER BY q.queue_name';
try {
$queues = $this->_db->selectAll($sql);
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($queues);
}
/**
* Returns a queue information hash.
*
* @param integer $queueId A queue ID.
*
* @return array A queue hash.
* @throws Whups_Exception
*/
public function getQueueInternal($queueId)
{
if (isset($this->_internalQueueCache[$queueId])) {
return $this->_internalQueueCache[$queueId];
}
try {
$queue = $this->_db->selectOne(
'SELECT queue_id, queue_name, queue_description, '
. 'queue_versioned, queue_slug, queue_email '
. 'FROM whups_queues WHERE queue_id = ?',
array((int)$queueId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
if (!$queue) {
return array();
}
$queue = $this->_fromBackend($queue);
$this->_internalQueueCache[$queueId] = array(
'id' => (int)$queue['queue_id'],
'name' => $queue['queue_name'],
'description' => $queue['queue_description'],
'versioned' => (bool)$queue['queue_versioned'],
'slug' => $queue['queue_slug'],
'email' => $queue['queue_email'],
'readonly' => false);
return $this->_internalQueueCache[$queueId];
}
/**
* Returns a queue information hash.
*
* @param string $slug A queue slug.
*
* @return array A queue hash.
* @throws Whups_Exception
*/
public function getQueueBySlugInternal($slug)
{
try {
$queue = $this->_db->selectOne(
'SELECT queue_id, queue_name, queue_description, '
. 'queue_versioned, queue_slug FROM whups_queues WHERE '
. 'queue_slug = ?',
array((string)$slug));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
if (!$queue) {
return array();
}
$queue = $this->_fromBackend($queue);
return array(
'id' => $queue['queue_id'],
'name' => $queue['queue_name'],
'description' => $queue['queue_description'],
'versioned' => $queue['queue_versioned'],
'slug' => $queue['queue_slug'],
'readonly' => false);
}
/**
* Returns a list of available queues.
*
* @return array An hash of queue ID => queue name.
* @throws Whups_Exception
*/
public function getQueuesInternal()
{
if (is_null($this->_queues)) {
try {
$queues = $this->_db->selectAssoc(
'SELECT queue_id, queue_name FROM whups_queues '
. 'ORDER BY queue_name');
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$this->_queues = $this->_fromBackend($queues);
}
return $this->_queues;
}
/**
* Returns a list of all available slugs.
*
* @return array A hash of queue ID => queue slug.
* @throws Whups_Exception
*/
public function getSlugs()
{
if (is_null($this->_slugs)) {
try {
$queues = $this->_db->selectAssoc(
'SELECT queue_id, queue_slug FROM whups_queues '
. 'WHERE queue_slug IS NOT NULL AND queue_slug <> \'\' '
. 'ORDER BY queue_slug');
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$this->_slugs = $this->_fromBackend($queues);
}
return $this->_slugs;
}
/**
* Updates a queue.
*
* @param integer $queueId A queue ID.
* @param string $name A queue name.
* @param string $description A queue description.
* @param array $types A list of type IDs for this queue.
* @param integer $versioned Is this queue versioned? (1 = true,
* 0 = false)
* @param string $slug A queue slug.
* @param string $email A queue email address.
* @param integer $default The default ticket type.
*
* @throws Whups_Exception
*/
public function updateQueue($queueId, $name, $description,
array $types = array(), $versioned = 0,
$slug = '', $email = '', $default = null)
{
global $registry;
if ($registry->hasMethod('tickets/listQueues') == $registry->getApp()) {
// Is slug unique?
if (!empty($slug)) {
if ($this->_db->selectValue('SELECT 1 FROM whups_queues WHERE queue_slug = ? AND queue_id <> ?', array($slug, $queueId))) {
throw new Whups_Exception(
_("That queue slug is already taken. Please select another."));
}
}
// First update the queue entry itself.
try {
$this->_db->update(
'UPDATE whups_queues SET queue_name = ?, '
. 'queue_description = ?, queue_versioned = ?, '
. 'queue_slug = ?, queue_email = ? WHERE queue_id = ?',
array($this->_toBackend($name),
$this->_toBackend($description),
empty($versioned) ? 0 : 1,
$slug,
$email,
(int)$queueId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
// Clear all previous type-queue associations.
try {
$this->_db->delete(
'DELETE FROM whups_types_queues WHERE queue_id = ?',
array((int)$queueId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
// Add the new associations.
if (is_array($types)) {
foreach ($types as $typeId) {
try {
$this->_db->insert(
'INSERT INTO whups_types_queues '
. '(queue_id, type_id, type_default) VALUES (?, ?, ?)',
array((int)$queueId,
(int)$typeId,
$default == $typeId ? 1 : 0));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
}
}
/**
* Returns a queue's default ticket type.
*
* @param integer $queue A queue ID.
*
* @return integer A ticket type ID.
*/
public function getDefaultType($queue)
{
try {
return $this->_db->selectValue(
'SELECT type_id FROM whups_types_queues '
. 'WHERE type_default = 1 AND queue_id = ?',
array($queue));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Deletes an entire queue, and all references to it.
*
* @param integer $queueId A queue ID.
*
* @throws Whups_Exception
*/
public function deleteQueue($queueId)
{
// Clean up the tickets associated with the queue.
try {
$result = $this->_db->selectAll(
'SELECT ticket_id FROM whups_tickets WHERE queue_id = ?',
array((int)$queueId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
foreach ($result as $ticket) {
$this->deleteTicket($ticket['ticket_id']);
}
// Now remove all references to the queue itself.
$tables = array(
'whups_queues_users',
'whups_types_queues',
'whups_versions',
'whups_queues');
$this->_db->beginDbTransaction();
foreach ($tables as $table) {
try {
$this->_db->delete(
'DELETE FROM ' . $table . ' WHERE queue_id = ?',
array((int)$queueId));
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
}
$this->_db->commitDbTransaction();
return parent::deleteQueue($queueId);
}
/**
* Update type-queue-associations.
*
* @param array An array of mappings.
*
* @throws Whups_Exception
*/
public function updateTypesQueues(array $tmPairs)
{
// Do this as a transaction.
$this->_db->beginDbTransaction();
// Delete existing associations.
try {
$this->_db->delete('DELETE FROM whups_types_queues');
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
// Insert new associations.
foreach ($tmPairs as $pair) {
try {
$this->_db->insert(
'INSERT INTO whups_types_queues (queue_id, type_id) '
. 'VALUES (?, ?)',
array((int)$pair[0], (int)$pair[1]));
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
}
try {
$this->_db->commitDbTransaction();
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
}
/**
* Returns a list of responsible users.
*
* @param integer $queueId A queue ID.
*
* @return array An array of users responsible for the queue.
* @throws Whups_Execption
*/
public function getQueueUsers($queueId)
{
try {
return $this->_db->selectValues(
'SELECT user_uid FROM whups_queues_users'
. ' WHERE queue_id = ? ORDER BY user_uid',
array((int)$queueId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Adds a responsible user.
*
* @param integer $queueId A queue ID.
* @param string $userId A user ID.
*
* @throws Whups_Exception
*/
public function addQueueUser($queueId, $userId)
{
if (!is_array($userId)) {
$userId = array($userId);
}
foreach ($userId as $user) {
try {
$this->_db->insert(
'INSERT INTO whups_queues_users (queue_id, user_uid) '
. 'VALUES (?, ?)',
array((int)$queueId, $user));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
}
/**
* Removes a responsible user.
*
* @param integer $queueId A queue ID.
* @param string $userId A user ID.
*
* @throws Whups_Exception
*/
public function removeQueueUser($queueId, $userId)
{
try {
$this->_db->delete(
'DELETE FROM whups_queues_users'
. ' WHERE queue_id = ? AND user_uid = ?',
array((int)$queueId, $userId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Returns a ticket type.
*
* @param integer $typeId A ticket type ID.
*
* @return array The ticket type information.
* @throws Whups_Exception
*/
public function getType($typeId)
{
try {
$type = $this->_db->selectOne(
'SELECT type_id, type_name, type_description '
. 'FROM whups_types WHERE type_id = ?',
array((int)$typeId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$type = $this->_fromBackend($type);
return array('id' => $typeId,
'name' => $type['type_name'],
'description' => $type['type_description']);
}
/**
* Returns a list of ticket types associated with a queue.
*
* @param integer $queueId A queue ID.
*
* @return array A hash of type ID => type name.
* @throws Whups_Exception
*/
public function getTypes($queueId)
{
try {
$types = $this->_db->selectAssoc(
'SELECT t.type_id, t.type_name '
. 'FROM whups_types t, whups_types_queues tm '
. 'WHERE tm.queue_id = ? AND tm.type_id = t.type_id '
. 'ORDER BY t.type_name',
array((int)$queueId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($types);
}
/**
* Returns a list of ticket type IDs associated with a queue.
*
* @param integer $queueId A queue ID.
*
* @return array A list of type IDs.
* @throws Whups_Exception
*/
public function getTypeIds($queueId)
{
try {
return $this->_db->selectValues(
'SELECT type_id FROM whups_types_queues '
. 'WHERE queue_id = ? ORDER BY type_id',
array((int)$queueId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Returns a list of all available ticket types.
*
* @return array A hash of type ID => type name.
* @throws Whups_Exception
*/
public function getAllTypes()
{
try {
$types = $this->_db->selectAssoc(
'SELECT type_id, type_name FROM whups_types ORDER BY type_name');
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($types);
}
/**
* Returns a list of all available ticket types.
*
* @return array A list of ticket type information hashes.
* @throws Whups_Exception
*/
public function getAllTypeInfo()
{
try {
$info = $this->_db->selectAll(
'SELECT type_id, type_name, type_description '
. 'FROM whups_types ORDER BY type_id');
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($info);
}
/**
* Returns a ticket type name.
*
* @param integer $type A type ID.
*
* @return string The ticket type's name.
* @throws Whups_Exception
*/
public function getTypeName($type)
{
try {
$name = $this->_db->selectValue(
'SELECT type_name FROM whups_types WHERE type_id = ?',
array((int)$type));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($name);
}
/**
* Updates a ticket type.
*
* @param integer $typeId A ticket type ID.
* @param string $name A ticket type name.
* @param string $description A ticket type description.
*
* @throws Whups_Exception
*/
public function updateType($typeId, $name, $description)
{
try {
$this->_db->update(
'UPDATE whups_types SET type_name = ?, type_description = ? '
. 'WHERE type_id = ?',
array($this->_toBackend($name),
$this->_toBackend($description),
(int)$typeId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Deletes a ticket type.
*
* @param integer $typeId A type ID.
*
* @throws Whups_Exception
*/
public function deleteType($typeId)
{
$this->_db->beginDbTransaction();
$values = array((int)$typeId);
try {
$this->_db->delete(
'DELETE FROM whups_states WHERE type_id = ?',
$values);
$this->_db->delete(
'DELETE FROM whups_priorities WHERE type_id = ?',
$values);
$this->_db->delete(
'DELETE FROM whups_attributes_desc WHERE type_id = ?',
$values);
$this->_db->delete(
'DELETE FROM whups_types WHERE type_id = ?',
$values);
$this->_db->commitDbTransaction();
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
}
/**
* Returns available states for a ticket type and state category.
*
* @param integer $type A ticket type ID.
* @param string|array $category State categories to include.
* @param string|array $notcategory State categories to not include.
*
* @return array A hash of state ID => state name.
* @throws Whups_Exception
*/
public function getStates($type = null, $category = '', $notcategory = '')
{
$fields = 'state_id, state_name';
$from = 'whups_states';
$order = 'state_category, state_name';
if (empty($type)) {
$fields .= ', whups_types.type_id, type_name';
$from .= ' LEFT JOIN whups_types ON whups_states.type_id = whups_types.type_id';
$where = '';
$order = 'type_name, ' . $order;
} else {
$where = 'type_id = ' . $type;
}
if (!is_array($category)) {
$where = $this->_addWhere($where, $category, 'state_category = ' . $this->_db->quoteString($category));
} else {
$clauses = array();
foreach ($category as $cat) {
$clauses[] = 'state_category = ' . $this->_db->quoteString($cat);
}
if (count($clauses))
$where = $this->_addWhere($where, $cat, implode(' OR ', $clauses));
}
if (!is_array($notcategory)) {
$where = $this->_addWhere($where, $notcategory, 'state_category <> ' . $this->_db->quoteString($notcategory));
} else {
$clauses = array();
foreach ($notcategory as $notcat) {
$clauses[] = 'state_category <> ' . $this->_db->quoteString($notcat);
}
if (count($clauses)) {
$where = $this->_addWhere($where, $notcat, implode(' OR ', $clauses));
}
}
if (!empty($where)) {
$where = ' WHERE ' . $where;
}
$query = "SELECT $fields FROM $from$where ORDER BY $order";
try {
$states = $this->_db->selectAll($query);
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$return = array();
if (empty($type)) {
foreach ($states as $state) {
$return[$state['state_id']] = $state['state_name'] . ' (' . $state['type_name'] . ')';
}
} else {
foreach ($states as $state) {
$return[$state['state_id']] = $state['state_name'];
}
}
return $this->_fromBackend($return);
}
/**
* Returns a state information hash.
*
* @param integer $stateId A state ID.
*
* @return array A state definition hash.
*/
public function getState($stateId)
{
try {
$state = $this->_db->selectOne(
'SELECT state_name, state_description, state_category, '
. 'type_id FROM whups_states WHERE state_id = ?',
array((int)$stateId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$state = $this->_fromBackend($state);
return array('id' => $stateId,
'name' => $state['state_name'],
'description' => $state['state_description'],
'category' => $state['state_category'],
'type' => $state['type_id']);
}
/**
* Returns all state information hashes for a ticket type.
*
* @param integer $type A ticket type ID.
*
* @return array A list of state hashes.
* @throws Whups_Exception
*/
public function getAllStateInfo($type)
{
try {
$info = $this->_db->selectAll(
'SELECT state_id, state_name, state_description, '
. 'state_category FROM whups_states WHERE type_id = ? '
. 'ORDER BY state_id',
array((int)$type));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($info);
}
/**
* Updates a state.
*
* @param integer $stateId A state ID.
* @param string $name A state name.
* @param string $description A state description.
* @param string $category A state category.
*
* @throws Whups_Exception
*/
public function updateState($stateId, $name, $description, $category)
{
try {
$this->_db->update(
'UPDATE whups_states SET state_name = ?, state_description = ?, '
. 'state_category = ? WHERE state_id = ?',
array($this->_toBackend($name),
$this->_toBackend($description),
$this->_toBackend($category),
(int)$stateId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Returns the default state for a ticket type.
*
* @param integer $type A type ID.
*
* @return integer The default state ID for the specified type.
* @throws Whups_Exception
*/
public function getDefaultState($type)
{
try {
return $this->_db->selectValue(
'SELECT state_id FROM whups_states WHERE state_default = 1 '
. 'AND type_id = ?',
array($type));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Sets the default state for a ticket type.
*
* @param integer $type A type ID.
* @param integer $state A state ID.
*
* @throws Whups_Exception
*/
public function setDefaultState($type, $state)
{
$this->_db->beginDbTransaction();
try {
$this->_db->update(
'UPDATE whups_states SET state_default = 0 WHERE type_id = ?',
array((int)$type));
$this->_db->update(
'UPDATE whups_states SET state_default = 1 WHERE state_id = ?',
array((int)$state));
$this->_db->commitDbTransaction();
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
}
/**
* Deletes a state.
*
* @param integer $state_id A state ID.
*
* @throws Whups_Exception
*/
public function deleteState($state_id)
{
try {
$this->_db->delete('DELETE FROM whups_states WHERE state_id = ?',
array((int)$state_id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Returns query details.
*
* @param integer $queryId A query ID.
*
* @return array Query information.
* @throws Whups_Exception
*/
public function getQuery($queryId)
{
try {
$query = $this->_db->selectOne(
'SELECT query_parameters, query_object FROM whups_queries '
. 'WHERE query_id = ?',
array((int)$queryId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($query);
}
/**
* Saves query details.
*
* If the query doesn't exist yes, it is added, update otherwise.
*
* @param Whups_Query $query A query.
*
* @throws Whups_Exception
*/
public function saveQuery($query)
{
try {
$exists = $this->_db->selectValue(
'SELECT 1 FROM whups_queries WHERE query_id = ?',
array((int)$query->id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$values = $this->_toBackend(array(serialize($query->parameters),
serialize($query->query),
$query->id));
try {
if ($exists) {
$this->_db->update(
'UPDATE whups_queries SET query_parameters = ?, '
. 'query_object = ? WHERE query_id = ?',
$values);
} else {
$this->_db->insert(
'INSERT INTO whups_queries (query_parameters, '
. 'query_object, query_id) VALUES (?, ?, ?)',
$values);
}
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Deletes a query.
*
* @param integer $queryId A query ID.
*
* @throws Whups_Exception
*/
public function deleteQuery($queryId)
{
try {
$this->_db->delete('DELETE FROM whups_queries WHERE query_id = ?',
array((int)$queryId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Returns whether a state is of a certain category.
*
* @param string $category A state category.
* @param integer $state_id A state ID.
*
* @return boolean True if the state is of the given category.
* @throws Whups_Exception
*/
public function isCategory($category, $state_id)
{
try {
return (bool)$this->_db->selectValue(
'SELECT 1 FROM whups_states '
. 'WHERE state_id = ? AND state_category = ?',
array((int)$state_id, $category));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Returns all priority information hashes for a ticket type.
*
* @param integer $type A ticket type ID.
*
* @return array A list of priority hashes.
* @throws Whups_Exception
*/
public function getAllPriorityInfo($type)
{
try {
$info = $this->_db->selectAll(
'SELECT priority_id, priority_name, priority_description '
. 'FROM whups_priorities WHERE type_id = ? '
. 'ORDER BY priority_id',
array((int)$type));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($info);
}
/**
* Returns a list of priorities.
*
* If the priorities are not limited to a ticket type, the priority names
* are suffixed with associated ticket type names.
*
* @param integer $type Limit result to this ticket type's priorities.
*
* @return array A hash of priority ID => priority name.
* @throws Whups_Exception
*/
public function getPriorities($type = null)
{
$fields = 'priority_id, priority_name';
$from = 'whups_priorities';
$order = 'priority_name';
if (empty($type)) {
$fields .= ', whups_types.type_id, type_name';
$from .= ' LEFT JOIN whups_types ON whups_priorities.type_id = whups_types.type_id';
$where = '';
$order = 'type_name, ' . $order;
} else {
$where = ' WHERE type_id = ' . $type;
}
$query = "SELECT $fields FROM $from$where ORDER BY $order";
try {
$priorities = $this->_db->selectAll($query);
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$return = array();
if (empty($type)) {
foreach ($priorities as $priority) {
$return[$priority['priority_id']] = $priority['priority_name'] . ' (' . $priority['type_name'] . ')';
}
} else {
foreach ($priorities as $priority) {
$return[$priority['priority_id']] = $priority['priority_name'];
}
}
return $this->_fromBackend($return);
}
/**
* Returns a priority information hash.
*
* @param integer $priorityId A state ID.
*
* @return array A priority definition hash.
* @throws Whups_Exception
*/
public function getPriority($priorityId)
{
try {
$priority = $this->_db->selectOne(
'SELECT priority_name, priority_description, type_id '
. 'FROM whups_priorities WHERE priority_id = ?',
array((int)$priorityId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$priority = $this->_fromBackend($priority);
return array('id' => $priorityId,
'name' => $priority['priority_name'],
'description' => $priority['priority_description'],
'type' => $priority['type_id']);
}
/**
* Updates a priority.
*
* @param integer $priorityId A priority ID.
* @param string $name A priority name.
* @param string $description A priority description.
*
* @throws Whups_Exception
*/
public function updatePriority($priorityId, $name, $description)
{
try {
$this->_db->update(
'UPDATE whups_priorities SET priority_name = ?, '
. 'priority_description = ? WHERE priority_id = ?',
array($this->_toBackend($name),
$this->_toBackend($description),
(int)$priorityId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Returns the default priority for a ticket type.
*
* @param integer $type A type ID.
*
* @return integer The default priority ID for the specified type.
* @throws Whups_Exception
*/
public function getDefaultPriority($type)
{
try {
return $this->_db->selectValue(
'SELECT priority_id FROM whups_priorities '
. 'WHERE priority_default = 1 AND type_id = ?',
array((int)$type));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Sets the default priority for a ticket type.
*
* @param integer $type A type ID.
* @param integer $priority A priority ID.
*
* @throws Whups_Exception
*/
public function setDefaultPriority($type, $priority)
{
$this->_db->beginDbTransaction();
try {
$this->_db->update(
'UPDATE whups_priorities SET priority_default = 0 '
. 'WHERE type_id = ?',
array((int)$type));
$this->_db->update(
'UPDATE whups_priorities SET priority_default = 1 '
. 'WHERE priority_id = ?',
array((int)$priority));
$this->_db->commitDbTransaction();
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
}
/**
* Deletes a priority.
*
* @param integer $priorityId A priority ID.
*
* @throws Whups_Exception
*/
public function deletePriority($priorityId)
{
try {
$this->_db->delete(
'DELETE FROM whups_priorities WHERE priority_id = ?',
array((int)$priorityId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Returns all versions of a queue.
*
* @param integer $queue A queue ID.
*
* @return array A list of version information hashes.
* @throws Whups_Exception
*/
public function getVersionInfoInternal($queue)
{
try {
$info = $this->_db->selectAll(
'SELECT version_id, version_name, version_description, '
. 'version_active FROM whups_versions WHERE queue_id = ?'
. ' ORDER BY version_id',
array((int)$queue));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($info);
}
/**
* Returns a version information hash.
*
* @param integer $versionId A state ID.
*
* @return array A version definition hash.
* @throws Whups_Exception
*/
public function getVersionInternal($versionId)
{
try {
$version = $this->_db->selectOne(
'SELECT version_name, version_description, version_active '
. 'FROM whups_versions WHERE version_id = ?',
array((int)$versionId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$version = $this->_fromBackend($version);
return array('id' => $versionId,
'name' => $version['version_name'],
'description' => $version['version_description'],
'active' => !empty($version['version_active']));
}
/**
* Updates a version.
*
* @param integer $versionId A version ID.
* @param string $name A version name.
* @param string $description A version description.
* @param boolean $active Whether the version is still active.
*
* @throws Whups_Exception
*/
public function updateVersion($versionId, $name, $description, $active)
{
try {
$this->_db->update(
'UPDATE whups_versions SET version_name = ?, '
. 'version_description = ?, version_active = ? '
. 'WHERE version_id = ?',
array($this->_toBackend($name),
$this->_toBackend($description),
(int)$active,
(int)$versionId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Deletes a version.
*
* @param integer $versionId A version ID.
*
* @throws Whups_Exception
*/
public function deleteVersion($versionId)
{
try {
$this->_db->delete(
'DELETE FROM whups_versions WHERE version_id = ?',
array((int)$versionId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Returns all available form replies for a ticket type.
*
* @param integer $type A type ID.
*
* @return array A hash of reply ID => reply information hash.
* @throws Whups_Exception
*/
public function getReplies($type)
{
try {
$rows = $this->_db->selectAll(
'SELECT reply_id, reply_name, reply_text '
. 'FROM whups_replies WHERE type_id = ? ORDER BY reply_name',
array((int)$type));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$info = array();
foreach ($rows as $row) {
$info[$row['reply_id']] = $this->_fromBackend($row);
}
return $info;
}
/**
* Returns a form reply.
*
* @param integer $reply_id A form reply ID.
*
* @return array A hash with all form reply information.
* @throws Whups_Exception
*/
public function getReply($reply_id)
{
try {
$reply = $this->_db->selectOne(
'SELECT reply_name, reply_text, type_id '
. 'FROM whups_replies WHERE reply_id = ?',
array((int)$reply_id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($reply);
}
/**
* Updates a form reply.
*
* @param integer $reply A reply ID.
* @param string $name A reply name.
* @param string $text A reply text.
*
* @throws Whups_Exception
*/
public function updateReply($reply, $name, $text)
{
try {
$this->_db->update(
'UPDATE whups_replies SET reply_name = ?, reply_text = ? '
. 'WHERE reply_id = ?',
array($this->_toBackend($name),
$this->_toBackend($text),
(int)$reply));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Deletes a form reply.
*
* @param integer $reply A reply ID.
*
* @throws Whups_Exception
*/
public function deleteReply($reply)
{
try {
$this->_db->delete('DELETE FROM whups_replies WHERE reply_id = ?',
array((int)$reply));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
parent::deleteReply($reply);
}
/**
* Adds a ticket listener.
*
* @param integer $ticket A ticket ID.
* @param string $user An email address.
*
* @throws Whups_Exception
*/
public function addListener($ticket, $user)
{
try {
$this->_db->insert(
'INSERT INTO whups_ticket_listeners (ticket_id, user_uid)'
. ' VALUES (?, ?)',
array((int)$ticket, $user));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Deletes a ticket listener.
*
* @param integer $ticket A ticket ID.
* @param string $user An email address.
*
* @throws Whups_Exception
*/
public function deleteListener($ticket, $user)
{
try {
$this->_db->delete(
'DELETE FROM whups_ticket_listeners WHERE ticket_id = ?'
. ' AND user_uid = ?',
array((int)$ticket, $user));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Returns all ticket listeners.
*
* @param integer $ticket A ticket ID.
* @param boolean $withowners Include ticket owners?
* @param boolean $withrequester Include ticket creators?
* @param boolean $withresponsible Include users responsible for the ticket
* queue?
*
* @return array A list of all ticket listeners.
* @throws Whups_Exception
*/
public function getListeners($ticket, $withowners = true,
$withrequester = true,
$withresponsible = false)
{
try {
$listeners = $this->_db->selectValues(
'SELECT DISTINCT user_uid FROM whups_ticket_listeners WHERE (ticket_id = ?)',
array((int)$ticket));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$users = array();
foreach ($listeners as $user) {
$users[$user] = 'listener';
}
$tinfo = $this->getTicketDetails($ticket);
if ($withresponsible) {
foreach ($this->getQueueUsers($tinfo['queue']) as $user) {
$users[$user] = 'queue';
}
}
// Tricky - handle case where owner = requester.
$requester = $tinfo['user_id_requester'];
$owner_is_requester = false;
if (isset($tinfo['owners'])) {
foreach ($tinfo['owners'] as $owner) {
$owner = str_replace('user:', '', $owner);
if ($owner == $requester) {
$owner_is_requester = true;
}
if ($withowners) {
$users[$owner] = 'owner';
} else {
if (isset($users[$owner])) {
unset($users[$owner]);
}
}
}
}
if (!$withrequester) {
if (isset($users[$requester]) &&
(!$withowners || $owner_is_requester)) {
unset($users[$requester]);
}
} elseif (!empty($requester) && !isset($users[$requester])) {
$users[$requester] = 'requester';
}
return $users;
}
/**
* Adds an attribute to a ticket type.
*
* @todo Make sure we're not adding a duplicate here (can be done in the db
* schema).
* @todo This assumes that $type_id is a valid type id.
*
* @param integer $type_id A ticket type ID.
* @param string $name An attribute name.
* @param string $desc An attribute description.
* @param string $type A form field type.
* @param array $params Additional parameters for the field type.
* @param boolean $required Whether the attribute is mandatory.
*
* @return integer The new attribute ID.
* @throws Whups_Exception
*/
public function addAttributeDesc($type_id, $name, $desc, $type, $params,
$required)
{
try {
return $this->_db->insert(
'INSERT INTO whups_attributes_desc '
. '(type_id, attribute_name, attribute_description, '
. 'attribute_type, attribute_params, attribute_required)'
. ' VALUES (?, ?, ?, ?, ?, ?)',
array((int)$type_id,
$this->_toBackend($name),
$this->_toBackend($desc),
$type,
serialize($this->_toBackend($params)),
(int)($required == 'on')));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Updates an attribute for a ticket type.
*
* @param integer $attribute_id An attribute ID.
* @param string $newname An attribute name.
* @param string $newdesc An attribute description.
* @param string $newtype A form field type.
* @param array $newparams Additional parameters for the field type.
* @param boolean $newrequired Whether the attribute is mandatory.
*
* @throws Whups_Exception
*/
public function updateAttributeDesc($attribute_id, $newname, $newdesc,
$newtype, $newparams, $newrequired)
{
try {
$this->_db->update(
'UPDATE whups_attributes_desc '
. 'SET attribute_name = ?, attribute_description = ?, '
. 'attribute_type = ?, attribute_params = ?, '
. 'attribute_required = ? WHERE attribute_id = ?',
array($this->_toBackend($newname),
$this->_toBackend($newdesc),
$newtype,
serialize($this->_toBackend($newparams)),
(int)($newrequired == 'on'),
(int)$attribute_id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
/**
* Deletes an attribute for a ticket type.
*
* @param integer $attribute_id An attribute ID.
*
* @throws Whups_Exception
*/
public function deleteAttributeDesc($attribute_id)
{
$this->_db->beginDbTransaction();
try {
$this->_db->delete(
'DELETE FROM whups_attributes_desc WHERE attribute_id = ?',
array((int)$attribute_id));
$this->_db->delete(
'DELETE FROM whups_attributes WHERE attribute_id = ?',
array((int)$attribute_id));
$this->_db->commitDbTransaction();
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
}
/**
* Returns all attributes.
*
* @return array A list of attributes hashes.
* @throws Whups_Exception
*/
public function getAllAttributes()
{
try {
$attributes = $this->_db->selectAll(
'SELECT attribute_id, attribute_name, attribute_description, '
. 'type_id FROM whups_attributes_desc');
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($attributes);
}
/**
* Returns an attribute information hash.
*
* @param integer $attribute_id An attribute ID.
*
* @return array The attribute hash.
* @throws Whups_Exception
*/
public function getAttributeDesc($attribute_id)
{
try {
$attribute = $this->_db->selectOne(
'SELECT attribute_name, attribute_description, '
. 'attribute_type, attribute_params, attribute_required '
. 'FROM whups_attributes_desc WHERE attribute_id = ?',
array((int)$attribute_id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return array(
'id' => $attribute_id,
'name' => $this->_fromBackend($attribute['attribute_name']),
'description' => $this->_fromBackend($attribute['attribute_description']),
'type' => empty($attribute['attribute_type'])
? 'text'
: $attribute['attribute_type'],
'params' => $this->_fromBackend(@unserialize($attribute['attribute_params'])),
'required' => (bool)$attribute['attribute_required']);
}
/**
* Returns an attribute name.
*
* @param integer $attribute_id An attribute ID.
*
* @return string The attribute name.
* @throws Whups_Exception
*/
public function getAttributeName($attribute_id)
{
try {
$name = $this->_db->selectValue(
'SELECT attribute_name FROM whups_attributes_desc '
. 'WHERE attribute_id = ?',
array((int)$attribute_id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($name);
}
/**
* Returns the attributes for a ticket type.
*
* @params integer $type A ticket type ID.
*
* @return array A list of attribute ID => attribute information hashes.
*/
protected function _getAttributesForType($type = null)
{
$fields = 'attribute_id, attribute_name, attribute_description, '
. 'attribute_type, attribute_params, attribute_required';
$from = 'whups_attributes_desc';
$order = 'attribute_name';
if (empty($type)) {
$fields .= ', whups_types.type_id, type_name';
$from .= ' LEFT JOIN whups_types ON '
. 'whups_attributes_desc.type_id = whups_types.type_id';
$where = '';
$order = 'type_name, ' . $order;
} else {
$where = ' WHERE type_id = ' . (int)$type;
}
$query = "SELECT $fields FROM $from$where ORDER BY $order";
try {
$attributes = $this->_db->selectAll($query);
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$results = array();
foreach ($attributes as $attribute) {
$id = $attribute['attribute_id'];
$results[$id] = $attribute;
$results[$id]['attribute_name'] =
$this->_fromBackend($attribute['attribute_name']);
if (empty($type)) {
$results[$id]['attribute_name'] .=
' (' . $attribute['type_name'] . ')';
}
$results[$id]['attribute_description'] =
$this->_fromBackend($attribute['attribute_description']);
$results[$id]['attribute_type'] =
empty($attribute['attribute_type'])
? 'text'
: $attribute['attribute_type'];
$results[$id]['attribute_params'] =
$this->_fromBackend(@unserialize($attribute['attribute_params']));
$results[$id]['attribute_required'] =
(bool)$attribute['attribute_required'];
}
return $results;
}
/**
* Returns available attribute names for a ticket type.
*
* @param integer $type_id A ticket type ID.
*
* @return array A list of attribute names.
* @throws Whups_Exception
*/
public function getAttributeNamesForType($type_id)
{
try {
$names = $this->_db->selectAll(
'SELECT attribute_name FROM whups_attributes_desc '
. 'WHERE type_id = ? ORDER BY attribute_name',
array((int)$type_id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($names);
}
/**
* Returns available attributes for a ticket type.
*
* @param integer $type_id A ticket type ID.
*
* @return array A list of attribute information hashes.
* @throws Whups_Exception
*/
public function getAttributeInfoForType($type_id)
{
try {
$info = $this->_db->selectAll(
'SELECT attribute_id, attribute_name, attribute_description '
. 'FROM whups_attributes_desc WHERE type_id = ? '
. 'ORDER BY attribute_id',
array((int)$type_id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
return $this->_fromBackend($info);
}
/**
* Save an attribute value.
*
* @param integer $ticket_id A ticket ID.
* @param integer $attribute_id An attribute ID.
* @param string $attribute_value An attribute value.
*
* @throws Whups_Exception
*/
protected function _setAttributeValue($ticket_id, $attribute_id,
$attribute_value)
{
$db_attribute_value = $this->_toBackend($attribute_value);
$this->_db->beginDbTransaction();
try {
$this->_db->delete(
'DELETE FROM whups_attributes WHERE ticket_id = ? '
. 'AND attribute_id = ?',
array($ticket_id, $attribute_id));
if (strlen($attribute_value)) {
$this->_db->insert(
'INSERT INTO whups_attributes (ticket_id, attribute_id, attribute_value) VALUES (?, ?, ?)',
array($ticket_id, $attribute_id, $db_attribute_value));
}
$this->_db->commitDbTransaction();
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
}
/**
* Returns the attribute values of a ticket.
*
* @param integer|array $ticket_id One or more ticket IDs.
*
* @return array If requesting a single ticket, an attribute ID =>
* attribute value hash. If requesting multiple tickets, a
* list of hashes with ticket ID, attribute ID and attribute
* value.
* @throws Whups_Exception
*/
public function getTicketAttributes($ticket_id)
{
if (is_array($ticket_id)) {
// No need to run a query for an empty array, and it would result
// in an invalid SQL query anyway.
if (!count($ticket_id)) {
return array();
}
try {
$attributes = $this->_db->selectAll(
'SELECT ticket_id AS id, attribute_id, attribute_value '
. 'FROM whups_attributes WHERE ticket_id IN ('
. str_repeat('?, ', count($ticket_id) - 1) . '?)',
$ticket_id);
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
} else {
try {
$attributes = $this->_db->selectAssoc(
'SELECT attribute_id, attribute_value'
. ' FROM whups_attributes WHERE ticket_id = ?',
array((int)$ticket_id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
$attributes = $this->_fromBackend($attributes);
foreach ($attributes as &$attribute) {
try {
$attribute = Horde_Serialize::unserialize(
$attribute,
Horde_Serialize::JSON);
} catch (Horde_Serialize_Exception $e) {
}
}
return $attributes;
}
/**
* Returns the attribute values and names of a ticket.
*
* @param integer|array $ticket_id One or more ticket IDs.
*
* @return array If requesting a single ticket, an attribute name =>
* attribute value hash. If requesting multiple tickets, a
* list of hashes with ticket ID, attribute ID, attribute
* name, and attribute value.
* @throws Whups_Exception
*/
public function getTicketAttributesWithNames($ticket_id)
{
if (is_array($ticket_id)) {
// No need to run a query for an empty array, and it would result
// in an invalid SQL query anyway.
if (!count($ticket_id)) {
return array();
}
try {
$attributes = $this->_db->selectAll(
'SELECT ticket_id AS id, d.attribute_name, '
. 'a.attribute_id, a.attribute_value '
. 'FROM whups_attributes a INNER JOIN '
. 'whups_attributes_desc d '
. 'ON (d.attribute_id = a.attribute_id)'
. 'WHERE a.ticket_id IN ('
. str_repeat('?, ', count($ticket_id) - 1) . '?)',
$ticket_id);
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$attributes = $this->_fromBackend($attributes);
foreach ($attributes as &$ticket) {
$ticket['attribute_value'] = $this->_json_decode($ticket['attribute_value']);
}
} else {
try {
$attributes = $this->_db->selectAssoc(
'SELECT d.attribute_name, a.attribute_value '
. 'FROM whups_attributes a INNER JOIN '
. 'whups_attributes_desc d '
. 'ON (d.attribute_id = a.attribute_id)'
. 'WHERE a.ticket_id = ? ORDER BY d.attribute_name',
array((int)$ticket_id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
$attributes = $this->_fromBackend($attributes);
foreach ($attributes as &$attribute) {
try {
$attribute = Horde_Serialize::unserialize(
$attribute,
Horde_Serialize::JSON
);
} catch (Horde_Serialize_Exception $e) {
}
}
}
return $attributes;
}
/**
* Returns attribute values and information of a ticket.
*
* @param integer $ticket_id A ticket IDs.
*
* @return array A list of hashes with attribute information and attribute
* value.
* @throws Whups_Exception
*/
protected function _getAllTicketAttributesWithNames($ticket_id)
{
try {
$attributes = $this->_db->selectAll(
'SELECT d.attribute_id, d.attribute_name, '
. 'd.attribute_description, d.attribute_type, '
. 'd.attribute_params, d.attribute_required, '
. 'a.attribute_value FROM whups_attributes_desc d '
. 'LEFT JOIN whups_tickets t ON (t.ticket_id = ?) '
. 'LEFT OUTER JOIN whups_attributes a '
. 'ON (d.attribute_id = a.attribute_id AND a.ticket_id = ?) '
. 'WHERE d.type_id = t.type_id ORDER BY d.attribute_name',
array($ticket_id, $ticket_id));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
foreach ($attributes as &$attribute) {
$attribute['attribute_name'] =
$this->_fromBackend($attribute['attribute_name']);
$attribute['attribute_description'] =
$this->_fromBackend($attribute['attribute_description']);
$attribute['attribute_type'] =
empty($attribute['attribute_type'])
? 'text'
: $attribute['attribute_type'];
$attribute['attribute_params'] =
$this->_fromBackend(@unserialize($attribute['attribute_params']));
$attribute['attribute_required'] =
(bool)$attribute['attribute_required'];
$attribute['attribute_value'] = $this->_json_decode($attribute['attribute_value']);
}
return $attributes;
}
/**
* Returns the owners of a ticket.
*
* @param mixed integer|array $ticketId One or more ticket IDs.
*
* @return array An hash of ticket ID => owner IDs
* @throws Whups_Exception
*/
public function getOwners($ticketId)
{
if (is_array($ticketId)) {
if (!count($ticketId)) {
return array();
}
try {
$owners = $this->_db->selectAll(
'SELECT ticket_id AS id, ticket_owner AS owner '
. 'FROM whups_ticket_owners WHERE ticket_id IN '
. '(' . str_repeat('?, ', count($ticketId) - 1) . '?)',
$ticketId);
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
} else {
try {
$owners = $this->_db->selectAll(
'SELECT ticket_id as id, ticket_owner as owner '
. 'FROM whups_ticket_owners WHERE ticket_id = ?',
array((int)$ticketId));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
$results = array();
foreach ($owners as $owner) {
$results[$owner['id']][] = $owner['owner'];
}
return $results;
}
/**
* Adds a new log entry
*
* @param integer $ticket_id A ticket ID.
* @param string $user A user updating the ticket.
* @param array $changes A list of changes.
* @param integer $transactionId A transaction ID to use.
*
* @return integer A transaction ID.
* @throws Whups_Exception
*/
public function updateLog($ticket_id, $user, array $changes = array(),
$transactionId = null)
{
if (is_null($transactionId)) {
$transactionId = $this->newTransaction($user);
}
foreach ($changes as $type => $values) {
if (!is_array($values)) {
$values = array($values);
}
foreach ($values as $value) {
try {
$this->_db->insert(
'INSERT INTO whups_logs (transaction_id, '
. 'ticket_id, log_type, log_value, '
. 'log_value_num) VALUES (?, ?, ?, ?, ?)',
array((int)$transactionId,
(int)$ticket_id,
$type,
$this->_toBackend((string)$value),
(int)$value));
} catch (Horde_Db_Exception $e) {
throw new Whups_Exception($e);
}
}
}
return $transactionId;
}
/**
* Create a new transaction ID.
*
* @param string $creator A transaction creator.
* @param string $creator_email The transaction creator's email address.
*
* @return integer A transaction ID.
* @throws Whups_Exception
*/
public function newTransaction($creator, $creator_email = null)
{
$insert = 'INSERT INTO whups_transactions '
. '(transaction_timestamp, transaction_user_id) VALUES(?, ?)';
$this->_db->beginDbTransaction();
try {
if ((empty($creator) || $creator < 0) && !empty($creator_email)) {
// Need to insert dummy value first so we can get the
// transaction ID.
$transactionId = $this->_db->insert($insert, array(time(), 'x'));
$creator = '-' . $transactionId . '_transaction';
$this->_db->insert(
'INSERT INTO whups_guests (guest_id, guest_email) '
. 'VALUES (?, ?)',
array((string)$creator, $creator_email));
$this->_db->update(
'UPDATE whups_transactions SET transaction_user_id = ? '
. 'WHERE transaction_id = ?',
array($creator, $transactionId));
} else {
$transactionId = $this->_db->insert($insert, array(time(), $creator));
}
} catch (Horde_Db_Exception $e) {
$this->_db->rollbackDbTransaction();
throw new Whups_Exception($e);
}
$this->_db->commitDbTransaction();
return $transactionId;
}
protected function _generateWhere($table, $fields, &$info, $type)
{
$where = '';
$this->_mapFields($info);
foreach ($fields as $field) {
if (isset($info[$field])) {
$prop = $info[$field];
if (is_array($info[$field])) {
$clauses = array();
foreach ($prop as $pprop) {
if (@settype($pprop, $type)) {
$clauses[] = "$table.$field = " . $this->_db->quoteString($pprop);
}
}
if (count($clauses)) {
$where = $this->_addWhere($where, true, implode(' OR ', $clauses));
}
} else {
$success = @settype($prop, $type);
$where = $this->_addWhere($where, !is_null($prop) && $success, "$table.$field = " . $this->_db->quoteString($prop));
}
}
}
foreach ($fields as $field) {
if (isset($info["not$field"])) {
$prop = $info["not$field"];
if (strpos($prop, ',') === false) {
$success = @settype($prop, $type);
$where = $this->_addWhere($where, $prop && $success, "$table.$field <> " . $this->_db->quoteString($prop));
} else {
$set = explode(',', $prop);
foreach ($set as $prop) {
$success = @settype($prop, $type);
$where = $this->_addWhere($where, $prop && $success, "$table.$field <> " . $this->_db->quoteString($prop));
}
}
}
}
return $where;
}
protected function _mapFields(&$info)
{
foreach ($info as $key => $val) {
if ($key === 'id') {
$info['ticket_id'] = $info['id'];
unset($info['id']);
} elseif ($key === 'state' ||
$key === 'type' ||
$key === 'queue' ||
$key === 'priority') {
$info[$key . '_id'] = $info[$key];
unset($info[$key]);
} elseif ($key === 'requester') {
$info['user_id_' . $key] = $info[$key];
unset($info[$key]);
}
}
}
protected function _addWhere($where, $condition, $clause, $conjunction = 'AND')
{
if (!empty($condition)) {
if (!empty($where)) {
$where .= " $conjunction ";
}
$where .= "($clause)";
}
return $where;
}
protected function _addDateWhere($where, $data, $type)
{
if (is_array($data)) {
if (!empty($data['from'])) {
$where = $this->_addWhere($where, true,
$type . ' >= ' . (int)$data['from']);
}
if (!empty($data['to'])) {
$where = $this->_addWhere($where, true,
$type . ' <= ' . (int)$data['to']);
}
return $where;
}
return $this->_addWhere($where, true, $type . ' = ' . (int)$data);
}
protected function _prefixTableToColumns($table, $columns)
{
$join = "";
$clause = '';
foreach ($columns as $column) {
$clause .= "$join$table.$column";
$join = ', ';
}
return $clause;
}
protected function _toBackend($value)
{
return Horde_String::convertCharset($value, 'UTF-8', $this->_db->getOption('charset'));
}
protected function _fromBackend($value)
{
return Horde_String::convertCharset($value, $this->_db->getOption('charset'), 'UTF-8');
}
}
|