xref: /plugin/bez/mdl/Model.php (revision de02284c1e90f3c0d8df29c1c019b3ef912eafd9)
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	public function __get($property) {
46        $models = array('userFactory', 'threadFactory', 'labelFactory');
47		if (in_array($property, $models) ||
48            in_array($property, array('sqlite', 'db', 'acl', 'dw_auth', 'user_nick', 'action', 'conf'))) {
49			return $this->$property;
50		}
51	}
52
53	public function __construct($dw_auth, $user_nick, $action, $conf) {
54		$this->dw_auth = $dw_auth;
55		$this->user_nick = $user_nick;
56		$this->action = $action;
57        $this->conf = $conf;
58
59//		$db_path = DOKU_INC . 'data/bez.sqlite';
60//		//if database not exists
61//		if (!file_exists($db_path)) {
62//			$this->db = new PDO('sqlite:/' . $db_path);
63//			$schema = file_get_contents(DOKU_PLUGIN . 'bez/mdl/schema.sql');
64//			if ($schema === false) {
65//				throw new Exception('cannot find schema file: '.$schema);
66//			}
67//			$this->db->exec($schema);
68//		} else {
69//			$this->db = new PDO('sqlite:/' . $db_path);
70//		}
71//		$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
72//
73//        //convert NULLS to empty strings
74//        $this->db->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_TO_STRING);
75
76        $this->sqlite = plugin_load('helper', 'sqlite');
77        if(!$this->sqlite) {
78            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.');
79            return;
80        }
81
82        if($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) {
83            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.');
84            $this->sqlite = null;
85            return;
86        }
87        $this->sqlite->getAdapter()->setUseNativeAlter(true);
88
89        // initialize the database connection
90        if(!$this->sqlite->init('b3p', DOKU_PLUGIN . 'bez/db/')) {
91            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.');
92            $this->sqlite = null;
93            return;
94        }
95
96        $this->db = $this->sqlite->getAdapter()->getDb();
97
98        $this->acl = new Acl($this);
99
100        $this->threadFactory = new ThreadFactory($this);
101        $this->userFactory = new UserFactory($this);
102        $this->labelFactory = new LabelFactory($this);
103
104//        $this->acl = new BEZ_mdl_Acl($this);
105//
106//        $this->issues = new BEZ_mdl_Issues($this);
107//        $this->tasks = new BEZ_mdl_Tasks($this);
108//
109//		$this->users = new BEZ_mdl_Users($this);
110//
111//		$this->issuetypes = new BEZ_mdl_Issuetypes($this);
112//
113//		$this->tasktypes = new BEZ_mdl_Tasktypes($this);
114//
115//		$this->commcauses = new BEZ_mdl_Commcauses($this);
116//        $this->timeline = new BEZ_mdl_Timeline($this);
117	}
118
119//	public function __destruct() {
120//		//http://stackoverflow.com/questions/18277233/pdo-closing-connection
121//		$this->db = NULL;
122//	}
123}
124