Overview

Packages

  • None
  • Sesha

Classes

  • Horde_Core_UI_VarRenderer_Stockedit_Html
  • Sesha
  • Sesha_Api
  • Sesha_Driver
  • Sesha_Driver_Sql
  • Sesha_Exception
  • Sesha_Forms_Category
  • Sesha_Forms_CategoryDelete
  • Sesha_Forms_CategoryList
  • Sesha_Forms_Property
  • Sesha_Forms_PropertyDelete
  • Sesha_Forms_PropertyList
  • Sesha_Forms_Search
  • Sesha_Forms_Stock
  • Sesha_Forms_Type_Client
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: // Sorting Constants
  4: 
  5: /** Sort by stock id. */
  6: define('SESHA_SORT_STOCKID', 100);
  7: /** Sort by stock name. */
  8: define('SESHA_SORT_NAME', 101);
  9: /** Sort by stock note. */
 10: define('SESHA_SORT_NOTE', 102);
 11: /** Sort in ascending order. */
 12: define('SESHA_SORT_ASCEND', 0);
 13: /** Sort in descending order. */
 14: define('SESHA_SORT_DESCEND', 1);
 15: 
 16: // Search Field Constants
 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:  * This is the base Sesha class.
 25:  *
 26:  * Copyright 2004-2007 Andrew Coleman <mercury@appisolutions.net>
 27:  * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
 28:  *
 29:  * See the enclosed file COPYING for license information (GPL). If you
 30:  * did not receive this file, see http://www.horde.org/licenses/gpl.
 31:  *
 32:  * @author  Andrew Coleman <mercury@appisolutions.net>
 33:  * @package Sesha
 34:  */
 35: class Sesha
 36: {
 37:     /**
 38:      * This function will return the inventory based on current category
 39:      * filters.
 40:      *
 41:      * @param constant $sortby       The field to sort the inventory on.
 42:      * @param constant $sortdir      The direction to sort the inventory.
 43:      * @param integer  $category_id  The category ID of stock to fetch.
 44:      * @param string   $what         The criteria to search on.
 45:      * @param integer  $where        The locations to search in (bitmask).
 46:      *
 47:      * @return mixed  Array of inventory on success; PEAR_Error on failure.
 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:         // Sorting functions
 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:         // Retrieve the inventory from the storage driver
 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:         // Sort the inventory if there is a sort function defined
 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:      * This function will return the list of available categories.
 95:      *
 96:      * @return mixed  Array of categories on success; PEAR_Error on failure.
 97:      */
 98:     function listCategories()
 99:     {
100:         $sesha_driver = $GLOBALS['injector']->getInstance('Sesha_Factory_Driver')->create();
101:         return $sesha_driver->getCategories();
102:     }
103: 
104:     /**
105:      * Returns a Hord_Form_Type_stringlist value split to an array.
106:      *
107:      * @param string $string  A comma separated string list.
108:      *
109:      * @return array  The string list as an array.
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:      * Comparison function for sorting inventory stock by id.
126:      *
127:      * @param array $a  Item one.
128:      * @param array $b  Item two.
129:      *
130:      * @return integer  1 if item one is greater, -1 if item two is greater;
131:      *                  0 if they are equal.
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:      * Comparison function for reverse sorting stock by id.
141:      *
142:      * @param array $a  Item one.
143:      * @param array $b  Item two.
144:      *
145:      * @return integer  -1 if item one is greater, 1 if item two is greater;
146:      *                  0 if they are equal.
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:      * Comparison function for sorting inventory stock by name.
156:      *
157:      * @param array $a  Item one.
158:      * @param array $b  Item two.
159:      *
160:      * @return integer  1 if item one is greater, -1 if item two is greater;
161:      *                  0 if they are equal.
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:      * Comparison function for reverse sorting stock by name.
171:      *
172:      * @param array $a  Item one.
173:      * @param array $b  Item two.
174:      *
175:      * @return integer  -1 if item one is greater, 1 if item two is greater;
176:      *                  0 if they are equal.
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:      * Comparison function for sorting inventory stock by a property.
186:      *
187:      * @param array $a  Item one.
188:      * @param array $b  Item two.
189:      *
190:      * @return integer  1 if item one is greater, -1 if item two is greater;
191:      *                  0 if they are equal.
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:      * Comparison function for reverse sorting stock by a property.
201:      *
202:      * @param array $a  Item one.
203:      * @param array $b  Item two.
204:      *
205:      * @return integer  -1 if item one is greater, 1 if item two is greater;
206:      *                  0 if they are equal.
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:      * Comparison function for sorting inventory stock by note.
216:      *
217:      * @param array $a  Item one.
218:      * @param array $b  Item two.
219:      *
220:      * @return integer  1 if item one is greater, -1 if item two is greater;
221:      *                  0 if they are equal.
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:      * Comparison function for reverse sorting stock by note.
231:      *
232:      * @param array $a  Item one.
233:      * @param array $b  Item two.
234:      *
235:      * @return integer  -1 if item one is greater, 1 if item two is greater;
236:      *                  0 if they are equal.
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:      * Build Sesha's list of menu items.
246:      */
247:     function getMenu($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:         /* Print. */
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: 
API documentation generated by ApiGen