1: <?php
2: /**
3: * Handles api version 1 of the creation date attribute.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_Format
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL
11: * @link http://www.horde.org/libraries/Horde_Kolab_Format
12: */
13:
14: /**
15: * Handles api version 1 of the creation date attribute.
16: *
17: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you did not
20: * receive this file, see
21: * http://www.horde.org/licenses/lgpl21.
22: *
23: * @since Horde_Kolab_Format 1.1.0
24: *
25: * @category Kolab
26: * @package Kolab_Format
27: * @author Gunnar Wrobel <wrobel@pardus.de>
28: * @license http://www.horde.org/licenses/lgpl21 LGPL
29: * @link http://www.horde.org/libraries/Horde_Kolab_Format
30: */
31: class Horde_Kolab_Format_Xml_Type_CreationDate_V1
32: extends Horde_Kolab_Format_Xml_Type_AutomaticDate_V1
33: {
34: /**
35: * Update the specified attribute.
36: *
37: * @param string $name The name of the the
38: * attribute to be updated.
39: * @param array $attributes The data array that holds
40: * all attribute values.
41: * @param DOMNode $parent_node The parent node of the
42: * node that should be
43: * updated.
44: * @param Horde_Kolab_Format_Xml_Helper $helper A XML helper instance.
45: * @param array $params Additional parameters
46: * for this write operation.
47: *
48: * @return DOMNode|boolean The new/updated child node or false if this
49: * failed.
50: *
51: * @throws Horde_Kolab_Format_Exception If converting the data to XML failed.
52: */
53: public function save(
54: $name,
55: $attributes,
56: $parent_node,
57: Horde_Kolab_Format_Xml_Helper $helper,
58: $params = array()
59: )
60: {
61: $node = $helper->findNodeRelativeTo(
62: './' . $name, $parent_node
63: );
64: if ($node !== false) {
65: if (isset($attributes[$name]) &&
66: ($old = $this->loadNodeValue($node, $params)) != $attributes[$name]) {
67: if (!$this->isRelaxed($params)) {
68: throw new Horde_Kolab_Format_Exception(
69: sprintf(
70: 'Not attempting to overwrite old %s %s with new value %s!',
71: $name,
72: Horde_Kolab_Format_Date::encodeDateTime($old),
73: Horde_Kolab_Format_Date::encodeDateTime($attributes[$name])
74: )
75: );
76: }
77: } else {
78: return $node;
79: }
80: }
81: $result = $this->saveNodeValue(
82: $name,
83: $this->generateWriteValue($name, $attributes, $params),
84: $parent_node,
85: $helper,
86: $params,
87: $node
88: );
89: return ($node !== false) ? $node : $result;
90: }
91:
92: /**
93: * Generate the value that should be written to the node. Override in the
94: * extending classes.
95: *
96: * @param string $name The name of the the attribute
97: * to be updated.
98: * @param array $attributes The data array that holds all
99: * attribute values.
100: * @param array $params The parameters for this write operation.
101: *
102: * @return mixed The value to be written.
103: */
104: protected function generateWriteValue($name, $attributes, $params)
105: {
106: if (isset($attributes[$name])) {
107: return Horde_Kolab_Format_Date::encodeDateTime(
108: $attributes[$name]
109: );
110: } else {
111: return Horde_Kolab_Format_Date::encodeDateTime(time());
112: }
113: }
114:
115: }
116: