1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16: 17: 18:
19: class Kronolith_Form_EditResource extends Horde_Form
20: {
21: 22: 23: 24: 25:
26: protected $_resource;
27:
28: 29: 30:
31: public function __construct($vars, $resource)
32: {
33: $this->_resource = $resource;
34: parent::__construct($vars, sprintf(_("Edit %s"), $resource->get('name')));
35:
36: $responses = array(Kronolith_Resource::RESPONSETYPE_ALWAYS_ACCEPT => _("Always Accept"),
37: Kronolith_Resource::RESPONSETYPE_ALWAYS_DECLINE => _("Always Decline"),
38: Kronolith_Resource::RESPONSETYPE_AUTO => _("Automatically"),
39: Kronolith_Resource::RESPONSETYPE_MANUAL => _("Manual"),
40: Kronolith_Resource::RESPONSETYPE_NONE => _("None"));
41:
42:
43: $groups = Kronolith::getDriver('Resource')
44: ->listResources(Horde_Perms::READ,
45: array('type' => Kronolith_Resource::TYPE_GROUP));
46: $enum = array();
47: foreach ($groups as $id => $group) {
48: $enum[$id] = $group->get('name');
49: }
50:
51: $this->addHidden('', 'c', 'text', true);
52: $this->addVariable(_("Name"), 'name', 'text', true);
53: $this->addVariable(_("Email"), 'email', 'email', false);
54: $this->addVariable(_("Description"), 'description', 'longtext', false, false, null, array(4, 60));
55: $this->addVariable(_("Response type"), 'responsetype', 'enum', true, false, null, array('enum' => $responses));
56: $this->addVariable(_("Groups"), 'category', 'multienum', false, false, null, array('enum' => $enum));
57: $this->setButtons(array(_("Save")));
58: }
59:
60: 61: 62:
63: public function execute()
64: {
65: $original_name = $this->_resource->get('name');
66: $new_name = $this->_vars->get('name');
67: $this->_resource->set('name', $new_name);
68: $this->_resource->set('description', $this->_vars->get('description'));
69: $this->_resource->set('response_type', $this->_vars->get('responsetype'));
70: $this->_resource->set('email', $this->_vars->get('email'));
71:
72:
73: $driver = Kronolith::getDriver('Resource');
74: $existing_groups = $driver->getGroupMemberships($this->_resource->getId());
75: $new_groups = $this->_vars->get('category');
76: $new_groups = (is_null($new_groups) ? array() : $new_groups);
77: foreach ($existing_groups as $gid) {
78: $i = array_search($gid, $new_groups);
79: if ($i === false) {
80:
81: $group = $driver->getResource($gid);
82: $members = $group->get('members');
83: $idx = array_search($this->_resource->getId(), $members);
84: if ($idx !== false) {
85: unset($members[$idx]);
86: reset($members);
87: $group->set('members', $members);
88: $group->save();
89: }
90: } else {
91:
92:
93: unset($new_groups[$i]);
94: }
95: }
96:
97: reset($new_groups);
98: foreach ($new_groups as $gid) {
99: $group = $driver->getResource($gid);
100: $members = $group->get('members');
101: $members[] = $this->_resource->getId();
102: $group->set('members', $members);
103: $group->save();
104: }
105:
106: try {
107: $result = $this->_resource->save();
108: } catch (Exception $e) {
109: throw new Kronolith_Exception(sprintf(_("Unable to save resource \"%s\": %s"), $new_name, $e->getMessage()));
110: }
111:
112: return $this->_resource;
113: }
114:
115: }
116: