xref: /plugin/bez/mdl/Model.php (revision 8a6381983135ed7de69b33e64aa0c1b16dbf69b0)
1<?php
2
3//require_once DOKU_PLUGIN.'bez/exceptions.php';
4
5//require_once 'acl.php';
6//require_once 'validator.php';
7
8//require_once 'users.php';
9//require_once 'threads.php';
10//require_once 'issuetypes.php';
11//require_once 'tasks.php';
12//require_once 'tasktypes.php';
13//require_once 'commcauses.php';
14//require_once 'timeline.php';
15
16namespace dokuwiki\plugin\bez\mdl;
17
18
19class Model {
20    /** @var \helper_plugin_sqlite */
21	protected $sqlite;
22
23	/** @var \SQLite3 */
24	protected $db;
25
26	/** @var Acl */
27    protected $acl;
28
29	//private $db;
30
31    //private $models = array('users', 'issues', 'tasks', 'tasktypes', 'commcauses', 'timeline');
32	//private $users, $issues, $tasks, $tasktypes, $commcauses, $timeline;
33
34    protected $dw_auth, $user_nick, $action, $conf;
35
36    /** @var ThreadFactory */
37    protected $threadFactory;
38
39    /** @var UserFactory  */
40    protected $userFactory;
41
42    /** @var LabelFactory */
43    protected $labelFactory;
44
45    /** @var Thread_commentFactory */
46    protected $thread_commentFactory;
47
48    /** @var TaskFactory */
49    protected $taskFactory;
50
51	public function __get($property) {
52        $models = array('userFactory', 'threadFactory', 'labelFactory', 'thread_commentFactory', 'taskFactory');
53		if (in_array($property, $models) ||
54            in_array($property, array('sqlite', 'db', 'acl', 'dw_auth', 'user_nick', 'action', 'conf'))) {
55			return $this->$property;
56		}
57	}
58
59	public function __construct($dw_auth, $user_nick, $action, $conf) {
60		$this->dw_auth = $dw_auth;
61		$this->user_nick = $user_nick;
62		$this->action = $action;
63        $this->conf = $conf;
64
65//		$db_path = DOKU_INC . 'data/bez.sqlite';
66//		//if database not exists
67//		if (!file_exists($db_path)) {
68//			$this->db = new PDO('sqlite:/' . $db_path);
69//			$schema = file_get_contents(DOKU_PLUGIN . 'bez/mdl/schema.sql');
70//			if ($schema === false) {
71//				throw new Exception('cannot find schema file: '.$schema);
72//			}
73//			$this->db->exec($schema);
74//		} else {
75//			$this->db = new PDO('sqlite:/' . $db_path);
76//		}
77//		$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
78//
79//        //convert NULLS to empty strings
80//        $this->db->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_TO_STRING);
81
82        $this->sqlite = plugin_load('helper', 'sqlite');
83        if(!$this->sqlite) {
84            throw new \Exception('Couldn\'t load sqlite.');
85        }
86
87        if($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) {
88            throw new \Exception('Couldn\'t load PDO sqlite.');
89        }
90        $this->sqlite->getAdapter()->setUseNativeAlter(true);
91
92        // initialize the database connection
93        if(!$this->sqlite->init('b3p', DOKU_PLUGIN . 'bez/db/')) {
94            throw new \Exception('Couldn\'t init sqlite.');
95        }
96
97        $this->db = $this->sqlite->getAdapter()->getDb();
98
99        $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
100
101        $this->acl = new Acl($this);
102
103        $this->userFactory = new UserFactory($this);
104
105        $this->threadFactory = new ThreadFactory($this);
106
107        $this->labelFactory = new LabelFactory($this);
108
109        $this->thread_commentFactory = new Thread_commentFactory($this);
110
111        $this->taskFactory = new TaskFactory($this);
112
113//        $this->acl = new BEZ_mdl_Acl($this);
114//
115//        $this->issues = new BEZ_mdl_Issues($this);
116//        $this->tasks = new BEZ_mdl_Tasks($this);
117//
118//		$this->users = new BEZ_mdl_Users($this);
119//
120//		$this->issuetypes = new BEZ_mdl_Issuetypes($this);
121//
122//		$this->tasktypes = new BEZ_mdl_Tasktypes($this);
123//
124//		$this->commcauses = new BEZ_mdl_Commcauses($this);
125//        $this->timeline = new BEZ_mdl_Timeline($this);
126	}
127
128//	public function __destruct() {
129//		//http://stackoverflow.com/questions/18277233/pdo-closing-connection
130//		$this->db = NULL;
131//	}
132}
133