Overview

Packages

  • Hermes
  • Horde
    • Data
  • Kronolith
  • None

Classes

  • Hermes_Application
  • Hermes_Exception
  • Hermes_Form_JobType_Add
  • Hermes_Slice
  • Hermes_Table
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Hermes_Slice:: Lightweight wrapper around a single timeslice
  4:  *
  5:  * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file LICENSE for license information (BSD). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/bsdl.php.
  9:  *
 10:  * @author Michael J Rubinsky <mrubinsk@horde.org>
 11:  */
 12: class Hermes_Slice implements ArrayAccess, IteratorAggregate
 13: {
 14:     /**
 15:      * Slice properties
 16:      *
 17:      * @var array
 18:      */
 19:     protected $_properties;
 20: 
 21:     public function __construct(array $properties = array())
 22:     {
 23:         $this->_properties = $properties;
 24:     }
 25: 
 26:     public function toArray()
 27:     {
 28:         return $this->_properties;
 29:     }
 30: 
 31:     /**
 32:      * Populate object from a json object.
 33:      *
 34:      * @param stdClass Hash containing slice data. @see self::toJson()
 35:      */
 36:     public function fromJson(stdClass $json)
 37:     {
 38:         $this->_properties = array (
 39:             'client' => $json->c,
 40:             'costobject' => $json->co,
 41:             'c_costobject_name' => $json->con,
 42:             'date' => $json->d,
 43:             'description' => $json->desc,
 44:             'employee' => $json->e,
 45:             'hours' => $json->h,
 46:             'id' => $json->i,
 47:             'note' => $json->n,
 48:             'rate' => $json->r,
 49:             'submitted' => $json->s,
 50:             'type' => $json->t,
 51:             '_type_name' => $json->tn,
 52:         );
 53:     }
 54: 
 55:     /**
 56:      * Populate this slice from a time entry form.
 57:      * Assumes the values are POSTed.
 58:      */
 59:     public function readForm()
 60:     {
 61:         // Required
 62:         $this->_properties['date'] = new Horde_Date(Horde_Util::getPost('start_date'));
 63:         $this->_properties['hours'] = Horde_Util::getPost('hours');
 64:         $this->_properties['description'] = Horde_Util::getPost('description');
 65:         $this->_properties['id'] = Horde_Util::getPost('id', 0);
 66:         $this->_properties['billable'] = Horde_Util::getPost('billable') ? 1 : 0;
 67: 
 68:         // Optional
 69:         $client = Horde_Util::getPost('client');
 70:         $this->_properties['client'] = empty($client) ? '' : $client;
 71:         $this->_properties['type'] = Horde_Util::getPost('type');
 72:         $this->_properties['costobject'] = Horde_Util::getPost('costobject');
 73:         $this->_properties['note'] = Horde_Util::getPost('notes');
 74:     }
 75: 
 76:     /**
 77:      * Get the json representation of this slice. The resulting json contains
 78:      * the following properties
 79:      *<pre>
 80:      * c    - client id
 81:      * cn   - client object
 82:      * co   - costobject id
 83:      * con  - costobject name
 84:      * d    - date
 85:      * desc - description
 86:      * e    - employee
 87:      * h    - hours
 88:      * i    - slice id
 89:      * n    - note
 90:      * r    - rate
 91:      * s    - submitted
 92:      * t    - type id
 93:      * tn   - type name
 94:      * b    - billable
 95:      *</pre>
 96:      *
 97:      * @return array
 98:      */
 99:     public function toJson()
100:     {
101:         // @TODO: DO we need the *entire* contact object?
102:         $cn = $GLOBALS['registry']->clients->getClients(array($this->_properties['client']));
103:         $json = array (
104:             'c' => $this->_properties['client'],
105:             'cn' => current($cn),
106:             'co' => $this->_properties['costobject'],
107:             'con' => $this->_properties['_costobject_name'],
108:             'd' => $this->_properties['date']->dateString(),
109:             'desc' => $this->_properties['description'],
110:             'e' => $this->_properties['employee'],
111:             'h' => $this->_properties['hours'],
112:             'i' => $this->_properties['id'],
113:             'n' => $this->_properties['note'],
114:             'r' => $this->_properties['rate'],
115:             's' => $this->_properties['submitted'],
116:             't' => $this->_properties['type'],
117:             'tn' => $this->_properties['_type_name'],
118:             'b'  => $this->_properties['billable']
119:         );
120: 
121:         return $json;
122:     }
123: 
124:     /**
125:      * ArrayAccess::offsetExists
126:      *
127:      * @param mixed $offset
128:      *
129:      * @return boolean
130:      */
131:     public function offsetExists($offset)
132:     {
133:         return isset($this->_properties[$offset]);
134:     }
135: 
136:     /**
137:      * ArrayAccess::offsetGet
138:      *
139:      * @param mixed $offset
140:      *
141:      * @return mixed
142:      */
143:     public function offsetGet($offset)
144:     {
145:         return array_key_exists($offset, $this->_properties) ? $this->_properties[$offset] : null;
146:     }
147: 
148:     /**
149:      * ArrayAccess::offsetSet
150:      *
151:      * @param mixed $offset
152:      * @param mixed $value
153:      *
154:      * @return void
155:      */
156:     public function offsetSet($offset, $value)
157:     {
158:         $this->_properties[$offset] = $value;
159:     }
160: 
161:     /**
162:      * ArrayAccess::offsetUnset
163:      *
164:      * @param mixed $offset
165:      *
166:      * @return void
167:      */
168:     public function offsetUnset($offset)
169:     {
170:         unset($this->_properties[$offset]);
171:     }
172: 
173:     /**
174:      * IteratorAggregate::getIterator
175:      *
176:      */
177:     public function getIterator()
178:     {
179:         return new ArrayIterator($this->_properties);
180:     }
181: 
182: }
API documentation generated by ApiGen