1: <?php
2:
3:
4:
5:
6: define('SESHA_SORT_STOCKID', 100);
7:
8: define('SESHA_SORT_NAME', 101);
9:
10: define('SESHA_SORT_NOTE', 102);
11:
12: define('SESHA_SORT_ASCEND', 0);
13:
14: define('SESHA_SORT_DESCEND', 1);
15:
16:
17:
18: define('SESHA_SEARCH_ID', 1);
19: define('SESHA_SEARCH_NAME', 2);
20: define('SESHA_SEARCH_NOTE', 4);
21: define('SESHA_SEARCH_PROPERTY', 8);
22:
23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:
35: class Sesha
36: {
37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48:
49: function listStock($sortby = null, $sortdir = null, $category_id = null,
50: $what = null, $where = null)
51: {
52: global $prefs;
53:
54: if (is_null($sortby)) {
55: $sortby = $prefs->getValue('sortby');
56: }
57: if (is_null($sortdir)) {
58: $sortdir = $prefs->getValue('sortdir');
59: }
60:
61:
62: $sort_functions = array(
63: SESHA_SORT_STOCKID => 'ByStockID',
64: SESHA_SORT_NAME => 'ByName',
65: SESHA_SORT_NOTE => 'ByNote',
66: );
67:
68: $list_property_ids = @unserialize($prefs->getValue('list_properties'));
69:
70:
71: $sesha_driver = $GLOBALS['injector']->getInstance('Sesha_Factory_Driver')->create();
72: if (!is_null($what) && !is_null($where)) {
73: $inventory = $sesha_driver->searchStock($what, $where, $list_property_ids);
74: } else {
75: $inventory = $sesha_driver->listStock($category_id, $list_property_ids);
76: }
77:
78:
79: if (count($inventory)) {
80: $prefix = ($sortdir == SESHA_SORT_DESCEND) ? '_rsort' : '_sort';
81: if (isset($sort_functions[$sortby])) {
82: uasort($inventory, array('Sesha', $prefix .
83: $sort_functions[$sortby]));
84: } elseif (substr($sortby, 0, 1) == 'p' && in_array(substr($sortby, 1), $list_property_ids)) {
85: $GLOBALS['_sort_property'] = $sortby;
86: uasort($inventory, array('Sesha', $prefix . 'ByProperty'));
87: }
88: }
89:
90: return $inventory;
91: }
92:
93: 94: 95: 96: 97:
98: function listCategories()
99: {
100: $sesha_driver = $GLOBALS['injector']->getInstance('Sesha_Factory_Driver')->create();
101: return $sesha_driver->getCategories();
102: }
103:
104: 105: 106: 107: 108: 109: 110:
111: function getStringlistArray($string)
112: {
113: $string = str_replace("'", "\'", $string);
114: $values = explode(',', $string);
115:
116: foreach ($values as $value) {
117: $value = trim($value);
118: $value_array[$value] = $value;
119: }
120:
121: return $value_array;
122: }
123:
124: 125: 126: 127: 128: 129: 130: 131: 132:
133: function _sortByStockID($a, $b)
134: {
135: if ($a['stock_id'] == $b['stock_id']) return 0;
136: return ($a['stock_id'] > $b['stock_id']) ? 1 : -1;
137: }
138:
139: 140: 141: 142: 143: 144: 145: 146: 147:
148: function _rsortByStockID($a, $b)
149: {
150: if ($a['stock_id'] == $b['stock_id']) return 0;
151: return ($a['stock_id'] > $b['stock_id']) ? -1 : 1;
152: }
153:
154: 155: 156: 157: 158: 159: 160: 161: 162:
163: function _sortByName($a, $b)
164: {
165: if ($a['stock_name'] == $b['stock_name']) return 0;
166: return ($a['stock_name'] > $b['stock_name']) ? 1 : -1;
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176: 177:
178: function _rsortByName($a, $b)
179: {
180: if ($a['stock_name'] == $b['stock_name']) return 0;
181: return ($a['stock_name'] > $b['stock_name']) ? -1 : 1;
182: }
183:
184: 185: 186: 187: 188: 189: 190: 191: 192:
193: function _sortByProperty($a, $b)
194: {
195: if ($a[$GLOBALS['_sort_property']] == $b[$GLOBALS['_sort_property']]) return 0;
196: return ($a[$GLOBALS['_sort_property']] > $b[$GLOBALS['_sort_property']]) ? 1 : -1;
197: }
198:
199: 200: 201: 202: 203: 204: 205: 206: 207:
208: function _rsortByProperty($a, $b)
209: {
210: if ($a[$GLOBALS['_sort_property']] == $b[$GLOBALS['_sort_property']]) return 0;
211: return ($a[$GLOBALS['_sort_property']] > $b[$GLOBALS['_sort_property']]) ? -1 : 1;
212: }
213:
214: 215: 216: 217: 218: 219: 220: 221: 222:
223: function _sortByNote($a, $b)
224: {
225: if ($a['note'] == $b['note']) return 0;
226: return ($a['note'] > $b['note']) ? 1 : -1;
227: }
228:
229: 230: 231: 232: 233: 234: 235: 236: 237:
238: function _rsortByNote($a, $b)
239: {
240: if ($a['note'] == $b['note']) return 0;
241: return ($a['note'] > $b['note']) ? -1 : 1;
242: }
243:
244: 245: 246:
247: function ($returnType = 'object')
248: {
249: global $registry, $conf, $browser, $print_link, $perms;
250: $perms = $GLOBALS['injector']->getInstance('Horde_Perms');
251: $menu = new Horde_Menu();
252: $menu->add(Horde::url('list.php'), _("_List Stock"), 'sesha.png', null, null, null, basename($_SERVER['PHP_SELF']) == 'index.php' ? 'current' : null);
253: if (Sesha::isAdmin(Horde_Perms::READ)|| $perms->hasPermission('sesha:addStock', $GLOBALS['registry']->getAuth(), Horde_Perms::READ)) {
254: $menu->add(Horde::url(Horde_Util::addParameter('stock.php', 'actionId', 'add_stock')), _("_Add Stock"), 'stock.png');
255: $menu->add(Horde::url('admin.php'), _("Admin"), 'sesha.png');
256: }
257: $menu->add(Horde::url('search.php'), _("_Search"), 'search.png');
258:
259:
260: if ($conf['menu']['print'] && isset($print_link) && $browser->hasFeature('javascript')) {
261: $menu->add("javascript:popup('$print_link'); return false;", _("_Print"), 'print.png');
262: }
263:
264: if ($returnType == 'object') {
265: return $menu;
266: } else {
267: return $menu->render();
268: }
269: }
270:
271: public static function isAdmin($permLevel = Horde_Perms::DELETE)
272: {
273: return ($GLOBALS['registry']->isAdmin() || $GLOBALS['injector']->getInstance('Horde_Perms')->hasPermission('sesha:admin', $GLOBALS['registry']->getAuth(), $permLevel));
274: }
275: }
276: