Hermes::getCostObjects PHP Method

getCostObjects() public static method

Return a list of cost objects exported by available APIs, optionally filtered by client_ids.
public static getCostObjects ( $client_ids = null, $external_only = false )
    public static function getCostObjects($client_ids = null, $external_only = false)
    {
        global $registry;
        $criteria = array('user' => $registry->getAuth(), 'active' => true);
        if (empty($client_ids)) {
            $client_ids = array('');
        } elseif (!is_array($client_ids)) {
            $client_ids = array($client_ids);
        }
        $costobjects = array();
        foreach ($client_ids as $client_id) {
            $criteria['client_id'] = $client_id;
            foreach ($registry->listApps() as $app) {
                if ($external_only && $app == 'hermes' || !$registry->hasMethod('listCostObjects', $app)) {
                    continue;
                }
                try {
                    $result = $registry->callByPackage($app, 'listCostObjects', array($criteria));
                } catch (Horde_Exception $e) {
                    $GLOBALS['notification']->push(sprintf(_("Error retrieving cost objects from \"%s\": %s"), $registry->get('name', $app), $e->getMessage()), 'horde.error');
                    continue;
                }
                foreach (array_keys($result) as $catkey) {
                    foreach (array_keys($result[$catkey]['objects']) as $okey) {
                        $result[$catkey]['objects'][$okey]['id'] = $app . ':' . $result[$catkey]['objects'][$okey]['id'];
                    }
                }
                if ($app == $registry->getApp()) {
                    $costobjects = array_merge($result, $costobjects);
                } else {
                    $costobjects = array_merge($costobjects, $result);
                }
            }
        }
        return $costobjects;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Get the list of Hermes-only deliverables for the requested client.
  *  - c:   The client id
  *  - id:  The optional deliverable id, if requesting a specific deliverable.
  */
 public function listDeliverables()
 {
     global $injector;
     if (!empty($this->vars->id)) {
         $params = array('id' => $this->vars->id);
         return array_values($injector->getInstance('Hermes_Driver')->listDeliverables($params));
     }
     // Only poll Hermes' deliverables if we have a client id since they
     // are ALWAYS tied to a client. Otherwise, just return the list of
     // external cost objects.
     $client_id = !empty($this->vars->c) ? $this->vars->c : null;
     if (!empty($client_id)) {
         $objs = array_values($injector->getInstance('Hermes_Driver')->listDeliverables(array('client_id' => $client_id)));
         foreach ($objs as &$obj) {
             $obj['id'] = 'hermes:' . $obj['id'];
             $obj['hours'] = 0;
             foreach ($injector->getInstance('Hermes_Driver')->getHours(array('costobject' => $obj['id'])) as $slice) {
                 $obj['hours'] += $slice['hours'];
             }
         }
         return $objs;
     }
     $elts = array();
     foreach (Hermes::getCostObjects($client_id, true) as $category) {
         Horde_Array::arraySort($category['objects'], 'name');
         foreach ($category['objects'] as $object) {
             $hours = 0;
             foreach ($injector->getInstance('Hermes_Driver')->getHours(array('costobject' => $object['id'])) as $slice) {
                 $hours += $slice['hours'];
             }
             $elts[] = array('id' => $object['id'], 'client_id' => false, 'name' => sprintf('%s (%s)', htmlspecialchars(Horde_String::truncate($object['name'], 80)), htmlspecialchars($category['category'])), 'parent' => empty($object['parent']) ? 0 : $object['parent'], 'estimate' => empty($object['estimate']) ? 0 : $object['estimate'], 'active' => true, 'is_external' => true, 'hours' => $hours);
         }
     }
     return array_values($elts);
 }