PluginProbe
UpStream: a Project Management Plugin for WordPress / 1.39.0
UpStream: a Project Management Plugin for WordPress v1.39.0
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / includes / model / UpStream_Model_Manager.php

UpStream_Model_Manager.php in UpStream: a Project Management Plugin for WordPress 1.39.0, at includes/model/UpStream_Model_Manager.php

225 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 // Exit if accessed directly
5 if ( ! defined('ABSPATH')) {
6 exit;
7 }
8
9 class UpStream_Model_Manager
10 {
11 protected static $instance;
12
13 protected $objects = [];
14
15 public function reset()
16 {
17 $this->objects = [];
18 }
19
20 public function getByID($object_type, $object_id, $parent_type = null, $parent_id = 0)
21 {
22 if (!in_array($object_type, [ UPSTREAM_ITEM_TYPE_CLIENT, UPSTREAM_ITEM_TYPE_PROJECT, UPSTREAM_ITEM_TYPE_MILESTONE, UPSTREAM_ITEM_TYPE_TASK, UPSTREAM_ITEM_TYPE_BUG, UPSTREAM_ITEM_TYPE_FILE ])) {
23 throw new \UpStream_Model_ArgumentException(sprintf(__('Item type %s is not valid.', 'upstream'), $object_type));
24 } else if (!$parent_id && in_array($object_type, [ UPSTREAM_ITEM_TYPE_TASK, UPSTREAM_ITEM_TYPE_BUG, UPSTREAM_ITEM_TYPE_FILE ])) {
25
26 }
27
28 if (empty($this->objects[$object_type]) || empty($this->objects[$object_type][$object_id])) {
29 $this->loadObject($object_type, $object_id, $parent_type, $parent_id);
30 }
31
32 if (empty($this->objects[$object_type][$object_id])) {
33 throw new UpStream_Model_ArgumentException(sprintf(__('This (ID = %s, TYPE = %s, PARENT ID = %s, PARENT TYPE = %s) is not a valid object', 'upstream'), $object_id, $object_type, $parent_id, $parent_type));
34 }
35
36 return $this->objects[$object_type][$object_id];
37 }
38
39 protected function loadObject($object_type, $object_id, $parent_type, $parent_id)
40 {
41 // TODO: add exceptions
42 if (UPSTREAM_ITEM_TYPE_PROJECT === $object_type) {
43
44 $project = new UpStream_Model_Project($object_id);
45 $this->objects[$object_type][$object_id] = $project;
46
47 foreach ($project->tasks() as $item) {
48 $this->objects[UPSTREAM_ITEM_TYPE_TASK][$item->id] = $item;
49 }
50
51 foreach ($project->bugs() as $item) {
52 $this->objects[UPSTREAM_ITEM_TYPE_BUG][$item->id] = $item;
53 }
54
55 foreach ($project->files() as $item) {
56 $this->objects[UPSTREAM_ITEM_TYPE_FILE][$item->id] = $item;
57 }
58
59 } else if (UPSTREAM_ITEM_TYPE_MILESTONE === $object_type) {
60 $this->objects[$object_type][$object_id] = new UpStream_Model_Milestone($object_id);
61 } else if (UPSTREAM_ITEM_TYPE_CLIENT === $object_type) {
62 $this->objects[$object_type][$object_id] = new UpStream_Model_Client($object_id);
63 } else if (UPSTREAM_ITEM_TYPE_TASK === $object_type) {
64 $this->loadObject($parent_type, $parent_id, null, null);
65 } else if (UPSTREAM_ITEM_TYPE_BUG === $object_type) {
66 $this->loadObject($parent_type, $parent_id, null, null);
67 } else if (UPSTREAM_ITEM_TYPE_FILE === $object_type) {
68 $this->loadObject($parent_type, $parent_id, null, null);
69 }
70
71 }
72
73 public function loadAll()
74 {
75 $posts = get_posts([
76 'post_type' => 'project',
77 'post_status' => 'publish',
78 'numberposts' => -1
79 ]);
80
81 foreach ($posts as $post) {
82 $this->getByID(UPSTREAM_ITEM_TYPE_PROJECT, $post->ID);
83 }
84
85 $posts = get_posts([
86 'post_type' => 'upst_milestone',
87 'post_status' => 'publish',
88 'numberposts' => -1
89 ]);
90
91 foreach ($posts as $post) {
92 $this->getByID(UPSTREAM_ITEM_TYPE_MILESTONE, $post->ID);
93 }
94
95 $posts = get_posts([
96 'post_type' => 'client',
97 'post_status' => 'publish',
98 'numberposts' => -1
99 ]);
100
101 foreach ($posts as $post) {
102 $this->getByID(UPSTREAM_ITEM_TYPE_CLIENT, $post->ID);
103 }
104 }
105
106 public function findAllByCallback($callback)
107 {
108 $return_items = [];
109
110 if (count($this->objects) == 0) {
111 $this->loadAll();
112 }
113
114 foreach ($this->objects as $type => $items) {
115 foreach ($items as $id => $item) {
116 $r = $callback($item);
117 if ($r) {
118 $return_items[] = $item;
119 }
120 }
121 }
122
123 return $return_items;
124 }
125
126 public function findAccessibleProjects()
127 {
128 $return_items = [];
129
130 if (count($this->objects) == 0) {
131 $this->loadAll();
132 }
133
134 foreach ($this->objects[UPSTREAM_ITEM_TYPE_PROJECT] as $id => $item) {
135 $access = upstream_can_access_object('view_project', UPSTREAM_ITEM_TYPE_PROJECT,
136 $id, null, 0, UPSTREAM_PERMISSIONS_ACTION_VIEW);
137 if ($access) {
138 $return_items[] = $item;
139 }
140 }
141
142 usort($return_items, function($item1, $item2) { return strcasecmp($item1->title, $item2->title); });
143
144 return $return_items;
145
146 }
147
148 public function createObject($object_type, $title, $createdBy, $parentId = 0)
149 {
150 switch ($object_type) {
151
152 case UPSTREAM_ITEM_TYPE_PROJECT:
153 return \UpStream_Model_Project::create($title, $createdBy);
154
155 case UPSTREAM_ITEM_TYPE_MILESTONE:
156 return \UpStream_Model_Milestone::create($title, $createdBy, $parentId);
157
158 case UPSTREAM_ITEM_TYPE_CLIENT:
159 return \UpStream_Model_Client::create($title, $createdBy);
160
161 case UPSTREAM_ITEM_TYPE_TASK:
162 $parent = $this->getByID(UPSTREAM_ITEM_TYPE_PROJECT, $parentId);
163 return \UpStream_Model_Task::create($parent, $title, $createdBy);
164
165 case UPSTREAM_ITEM_TYPE_FILE:
166 $parent = $this->getByID(UPSTREAM_ITEM_TYPE_PROJECT, $parentId);
167 return \UpStream_Model_File::create($parent, $title, $createdBy);
168
169 case UPSTREAM_ITEM_TYPE_BUG:
170 $parent = $this->getByID(UPSTREAM_ITEM_TYPE_PROJECT, $parentId);
171 return \UpStream_Model_Bug::create($parent, $title, $createdBy);
172
173 default:
174 throw new \UpStream_Model_ArgumentException(sprintf(__('Item type %s is not valid.', 'upstream'), $object_type));
175 }
176 }
177
178 public function deleteObject($object_type, $object_id, $parent_type, $parent_id)
179 {
180 // throws exception if the object doesn't exist...
181 $obj = $this->getByID($object_type, $object_id, $parent_type, $parent_id);
182
183 switch ($object_type) {
184
185 case UPSTREAM_ITEM_TYPE_PROJECT:
186 wp_delete_post($object_id);
187 break;
188
189 case UPSTREAM_ITEM_TYPE_MILESTONE:
190 if (class_exists('\UpStream\Factory')) {
191 $milestone = \UpStream\Factory::getMilestone($object_id);
192
193 if ( ! empty($milestone)) {
194 $milestone->delete();
195 }
196 }
197 break;
198
199 case UPSTREAM_ITEM_TYPE_CLIENT:
200
201 case UPSTREAM_ITEM_TYPE_TASK:
202
203 case UPSTREAM_ITEM_TYPE_FILE:
204
205 case UPSTREAM_ITEM_TYPE_BUG:
206
207 default:
208 throw new \UpStream_Model_ArgumentException(sprintf(__('Item type %s is not valid.', 'upstream'), $object_type));
209 }
210
211 unset($this->objects[$object_type][$object_id]);
212
213 }
214
215 public static function get_instance()
216 {
217 if (empty(static::$instance)) {
218 $instance = new self;
219 static::$instance = $instance;
220 }
221
222 return static::$instance;
223 }
224
225 }