PluginProbe
Webling / 3.2.0
Webling v3.2.0
trunk 2.0 3.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.3.0 3.4.0 3.4.1 3.5.0 3.6.0 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.9.0 3.9.1 3.9.2
webling / src / WeblingAPI / WeblingApiHelper.php

WeblingApiHelper.php in Webling 3.2.0, at src/WeblingAPI/WeblingApiHelper.php

193 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Singleton class
5 *
6 */
7 class WeblingApiHelper {
8
9 private $client;
10
11 private $cache;
12
13 public static $invisibleFields = ['file', 'image'];
14
15 public static $immutableFields = ['file', 'image', 'autoincrement'];
16
17 /**
18 * Call this method to get singleton
19 *
20 * @return WeblingApiHelper
21 */
22 public static function Instance() {
23 static $inst = null;
24 if ($inst === null) {
25 $inst = new WeblingApiHelper();
26 }
27 return $inst;
28 }
29
30 /**
31 * Private constructor so nobody else can instance it
32 *
33 */
34 private function __construct() {
35 $options = get_option('webling-options', array());
36 if (!isset($options['host'])) {
37 $options['host'] = '';
38 }
39 if (!isset($options['apikey'])) {
40 $options['apikey'] = '';
41 }
42 $this->client = new \Webling\API\Client($options['host'], $options['apikey'], array('useragent' => $this->getUserAgent()));
43 $this->cache = new \Webling\Cache\WordpressCache($this->client);
44 }
45
46 public function client() {
47 return $this->client;
48 }
49
50 public function clearCache() {
51 $this->cache->clearCache();
52 }
53
54 public function hasMemberReadAccess() {
55 // check if /membergroup has root objects
56 $rootmembergroup = $this->cache->getRoot('membergroup');
57 if (isset($rootmembergroup['roots']) && is_array($rootmembergroup['roots']) && count($rootmembergroup['roots']) > 0) {
58 return true;
59 }
60 return false;
61 }
62
63 public function hasMemberWriteAccess() {
64 // check if any of the membergroups is writeable
65 $tree = $this->getMembergroupTree();
66 $writeable_count = 0;
67 array_walk_recursive($tree, function($value, $key) use (&$writeable_count) {
68 if ($key == 'writeable' && $value === true) {
69 $writeable_count++;
70 }
71 });
72 if ($writeable_count > 0) {
73 return true;
74 }
75 return false;
76 }
77
78 public function getMembergroupTree() {
79 $rootmembergroup = $this->cache->getRoot('membergroup');
80 $roots = array();
81 if (is_array($rootmembergroup['roots'])) {
82 foreach ($rootmembergroup['roots'] as $rootGroupId) {
83 $roots[$rootGroupId] = array(
84 'title' => $this->getObjectLabel('membergroup', $rootGroupId),
85 'writeable' => $this->objectIsWriteable('membergroup', $rootGroupId),
86 'childs' => $this->recursiveMembergroupChilds($rootGroupId)
87 );
88 }
89 }
90 return $roots;
91 }
92
93 public function getMemberFields() {
94 $definitions = $this->cache->getRoot('definition');
95 $properties = array();
96 if (isset($definitions['member']['properties'])) {
97 foreach ($definitions['member']['properties'] as $propertyName => $propertyConf) {
98 $properties[$propertyConf['id']] = $propertyName;
99 }
100 }
101 return $properties;
102 }
103
104 public function getVisibleMemberFields() {
105 $definitions = $this->cache->getRoot('definition');
106 $properties = array();
107 if (isset($definitions['member']['properties'])) {
108 foreach ($definitions['member']['properties'] as $propertyName => $propertyConf) {
109 if (!in_array($propertyConf['datatype'], self::$invisibleFields)) {
110 $properties[$propertyConf['id']] = $propertyName;
111 }
112 }
113 }
114 return $properties;
115 }
116
117 public function getMutableMemberFields() {
118 $definitions = $this->cache->getRoot('definition');
119 $properties = array();
120 if (isset($definitions['member']['properties'])) {
121 foreach ($definitions['member']['properties'] as $propertyName => $propertyConf) {
122 if (!in_array($propertyConf['datatype'], self::$immutableFields)) {
123 $properties[$propertyConf['id']] = $propertyName;
124 }
125 }
126 }
127 return $properties;
128 }
129
130 public function getMemberFieldDefinitionsById() {
131 $definitions = $this->cache->getRoot('definition');
132 $properties = array();
133 if (isset($definitions['member']['properties'])) {
134 foreach ($definitions['member']['properties'] as $propertyName => $propertyConf) {
135 $properties[$propertyConf['id']] = $propertyConf;
136 }
137 }
138 return $properties;
139 }
140
141 public function getMemberProperties() {
142 $definitions = $this->cache->getRoot('definition');
143 return $definitions['member']['properties'];
144 }
145
146 public function getMemberTitleFields() {
147 $definitions = $this->cache->getRoot('definition');
148 $properties = array();
149 if (isset($definitions['member']['label'])) {
150 foreach ($definitions['member']['label'] as $propertyName) {
151 $properties[] = $propertyName;
152 }
153 }
154 return $properties;
155 }
156
157 private function recursiveMembergroupChilds($objectId) {
158 $childs = array();
159
160 $obj = $this->cache->getObject('membergroup', $objectId);
161 if (isset($obj['children']['membergroup'])) {
162 foreach ($obj['children']['membergroup'] as $childId) {
163 $childs[$childId] = array(
164 'title' => $this->getObjectLabel('membergroup', $childId),
165 'writeable' => $this->objectIsWriteable('membergroup', $childId),
166 'childs' => $this->recursiveMembergroupChilds($childId)
167 );
168 }
169 }
170 return $childs;
171 }
172
173 public function getObjectLabel($type, $objectId) {
174 $obj = $this->cache->getObject($type, $objectId);
175 if (isset($obj['properties']['title'])) {
176 return $obj['properties']['title'];
177 }
178 return $type.'_'.$objectId;
179 }
180
181 public function objectIsWriteable($type, $objectId) {
182 $obj = $this->cache->getObject($type, $objectId);
183 if (isset($obj['readonly'])) {
184 return !$obj['readonly'];
185 }
186 return false;
187 }
188
189 public function getUserAgent() {
190 return 'Webling-WP-Plugin/' . WEBLING_DB_VERSION;
191 }
192 }
193