Merge branch 'master' of http://git.tine20.org/git/Syncope
[tine20] / tine20 / library / Syncope / lib / Syncope / Data / Contacts.php
1 <?php
2
3 /**
4  * Syncope
5  *
6  * @package     Model
7  * @license     http://www.tine20.org/licenses/lgpl.html LGPL Version 3
8  * @copyright   Copyright (c) 2009-2012 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Lars Kneschke <l.kneschke@metaways.de>
10  */
11
12 /**
13  * class to handle ActiveSync Sync command
14  *
15  * @package     Model
16  */
17
18 class Syncope_Data_Contacts extends Syncope_Data_AData
19 {
20     public function appendXML(DOMElement $_domParrent, $_collectionData, $_serverId)
21     {
22         $_domParrent->ownerDocument->documentElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:Contacts', 'uri:Contacts');
23         
24         foreach (Syncope_Data_AData::$entries[get_class($this)][$_collectionData["collectionId"]][$_serverId] as $key => $value) {
25             // create a new DOMElement ...
26             $node = new DOMElement($key, null, 'uri:Contacts');
27             
28             // ... append it to parent node aka append it to the document ...
29             $_domParrent->appendChild($node);
30             
31             // ... and now add the content (DomText takes care of special chars)
32             $node->appendChild(new DOMText($value));
33         }
34         
35     }
36     
37     public function createEntry($_folderId, SimpleXMLElement $_entry)
38     {
39         $xmlData = $_entry->children('uri:Contacts');
40         
41         $id = sha1(mt_rand(). microtime());
42         
43         Syncope_Data_AData::$entries[get_class($this)][$_folderId][$id] = array(
44             'FirstName' => (string)$xmlData->FirstName, 
45             'LastName'  => (string)$xmlData->LastName
46         );
47         
48         return $id;
49     }
50     
51     public function updateEntry($_folderId, $_serverId, SimpleXMLElement $_entry)
52     {
53         $xmlData = $_entry->children('uri:Contacts');
54         
55         Syncope_Data_AData::$entries[get_class($this)][$_folderId][$_serverId] = array(
56             'FirstName' => (string)$xmlData->FirstName, 
57             'LastName'  => (string)$xmlData->LastName
58         );
59     }        
60     
61     protected function _initData()
62     {
63         /**
64         * used by unit tests only to simulated added folders
65         */
66         if (!isset(Syncope_Data_AData::$folders[get_class($this)])) {
67             Syncope_Data_AData::$folders[get_class($this)] = array(
68                 'addressbookFolderId' => array(
69                     'folderId'    => 'addressbookFolderId',
70                     'parentId'    => null,
71                     'displayName' => 'Default Contacts Folder',
72                     'type'        => Syncope_Command_FolderSync::FOLDERTYPE_CONTACT
73                 ),
74                 'anotherAddressbookFolderId' => array(
75                     'folderId'    => 'anotherAddressbookFolderId',
76                     'parentId'    => null,
77                     'displayName' => 'Another Contacts Folder',
78                     'type'        => Syncope_Command_FolderSync::FOLDERTYPE_CONTACT_USER_CREATED
79                 )
80             );
81         }
82         
83         /**
84          * used by unit tests only to simulated added folders
85          */
86         if (!isset(Syncope_Data_AData::$entries[get_class($this)])) {
87             Syncope_Data_AData::$entries[get_class($this)] = array(
88                         'addressbookFolderId' => array(
89                         'contact1' => array(
90                                 'FirstName' => 'Lars', 
91                                 'LastName'  => 'Kneschke'
92                         ),
93                         'contact2' => array(
94                                 'FirstName' => 'Cornelius', 
95                                 'LastName'  => 'WeiƟ'
96                             )
97                     )
98             );
99         }
100     }
101 }
102