Merge branch '2013.10'
[tine20] / tine20 / Tool / Application / templates / Controller.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     ExampleApplication
6  * @subpackage  Controller
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Cornelius Weiss <c.weiss@metaways.de>
9  * @copyright   Copyright (c) 2007-2011 Metaways Infosystems GmbH (http://www.metaways.de)
10  *
11  */
12
13 /**
14  * ExampleApplication Controller (composite)
15  * 
16  * The ExampleApplication 2.0 Controller manages access (acl) to the different backends and supports
17  * a common interface to the servers/views
18  * 
19  * @package ExampleApplication
20  * @subpackage  Controller
21  */
22 class ExampleApplication_Controller extends Tinebase_Controller_Event implements Tinebase_Container_Interface
23 {
24     /**
25      * holds the default Model of this application
26      * @var string
27      */
28     protected static $_defaultModel = 'ExampleApplication_Model_ExampleRecord';
29     
30     /**
31      * the constructor
32      *
33      * don't use the constructor. use the singleton 
34      */
35     private function __construct() {
36         $this->_applicationName = 'ExampleApplication';
37     }
38     
39     /**
40      * don't clone. Use the singleton.
41      *
42      */
43     private function __clone() 
44     {
45     }
46     
47     /**
48      * holds self
49      * @var ExampleApplication_Controller
50      */
51     private static $_instance = NULL;
52     
53     /**
54      * singleton
55      *
56      * @return ExampleApplication_Controller
57      */
58     public static function getInstance() 
59     {
60         if (self::$_instance === NULL) {
61             self::$_instance = new ExampleApplication_Controller();
62         }
63         return self::$_instance;
64     }
65     
66     /**
67      * creates the initial folder for new accounts
68      *
69      * @param mixed[int|Tinebase_Model_User] $_account   the accountd object
70      * @return Tinebase_Record_RecordSet                            of subtype Tinebase_Model_Container
71      */
72     public function createPersonalFolder($_accountId)
73     {
74         $translation = Tinebase_Translation::getTranslation('ExampleApplication');
75         
76         $account = Tinebase_User::getInstance()->getUserById($_accountId);
77         
78         $newContainer = new Tinebase_Model_Container(array(
79             'name'              => sprintf($translation->_("%s's personal example records"), $account->accountFullName),
80             'type'              => Tinebase_Model_Container::TYPE_PERSONAL,
81             'owner_id'          => $_accountId,
82             'backend'           => 'Sql',
83             'application_id'    => Tinebase_Application::getInstance()->getApplicationByName('ExampleApplication')->getId(),
84             'model'             => static::$_defaultModel
85         ));
86         
87         $personalContainer = Tinebase_Container::getInstance()->addContainer($newContainer);
88         $container = new Tinebase_Record_RecordSet('Tinebase_Model_Container', array($personalContainer));
89         
90         return $container;
91     }
92
93     /**
94      * event handler function
95      * 
96      * all events get routed through this function
97      *
98      * @param Tinebase_Event_Abstract $_eventObject the eventObject
99      */
100     protected function _handleEvent(Tinebase_Event_Abstract $_eventObject)
101     {
102         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . ' (' . __LINE__ . ') handle event of type ' . get_class($_eventObject));
103         
104         switch(get_class($_eventObject)) {
105             case 'Admin_Event_AddAccount':
106                 $this->createPersonalFolder($_eventObject->account);
107                 break;
108             case 'Admin_Event_DeleteAccount':
109                 #$this->deletePersonalFolder($_eventObject->account);
110                 break;
111         }
112     }
113 }