Merge branch '2015.11-develop' into 2016.03
[tine20] / tine20 / Projects / Controller.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     Projects
6  * @subpackage  Controller
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Philipp Schüle <p.schuele@metaways.de>
9  * @copyright   Copyright (c) 2011 Metaways Infosystems GmbH (http://www.metaways.de)
10  *
11  */
12
13 /**
14  * Projects Controller (composite)
15  * 
16  * The Projects 2.0 Controller manages access (acl) to the different backends and supports
17  * a common interface to the servers/views
18  * 
19  * @package Projects
20  * @subpackage  Controller
21  */
22 class Projects_Controller extends Tinebase_Controller_Event implements Tinebase_Container_Interface
23 {
24     /**
25      * the constructor
26      *
27      * don't use the constructor. use the singleton 
28      */
29     private function __construct() {
30         $this->_applicationName = 'Projects';
31     }
32     
33     /**
34      * holds the default Model of this application
35      * @var string
36      */
37     protected static $_defaultModel = 'Projects_Model_Project';
38     
39     /**
40      * don't clone. Use the singleton.
41      *
42      */
43     private function __clone() 
44     {
45     }
46     
47     /**
48      * holds self
49      * @var Projects_Controller
50      */
51     private static $_instance = NULL;
52     
53     /**
54      * singleton
55      *
56      * @return Projects_Controller
57      */
58     public static function getInstance() 
59     {
60         if (self::$_instance === NULL) {
61             self::$_instance = new Projects_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($_account)
73     {
74         $translation = Tinebase_Translation::getTranslation($this->_applicationName);
75         
76         $account = Tinebase_User::getInstance()->getUserById($_account);
77         
78         $newContainer = new Tinebase_Model_Container(array(
79             'name'              => sprintf($translation->_("%s's personal Projects"), $account->accountFullName),
80             'type'              => Tinebase_Model_Container::TYPE_PERSONAL,
81             'owner_id'          => $account->getId(),
82             'backend'           => 'Sql',
83             'application_id'    => Tinebase_Application::getInstance()->getApplicationByName($this->_applicationName)->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 'Tinebase_Event_User_DeleteAccount':
109                 /**
110                  * @var Tinebase_Event_User_DeleteAccount $_eventObject
111                  */
112                 if ($_eventObject->deletePersonalContainers()) {
113                     $this->deletePersonalFolder($_eventObject->account);
114                 }
115                 break;
116         }
117     }
118 }