Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
suite_manager_edit_fns.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/page/page_edit_fns.inc';
18 
31 {
32 
33 
40  function __construct($assetid=0)
41  {
42  parent::__construct($assetid);
43  unset($this->static_screens['settings']);
44  unset($this->static_screens['preview']);
45  unset($this->static_screens['lookupValues']);
46  unset($this->static_screens['metadata']);
47  unset($this->static_screens['metadataSchemas']);
48  unset($this->static_screens['workflow']);
49  unset($this->static_screens['tagging']);
50  unset($this->static_screens['dependants']);
51  unset($this->static_screens['linking']);
52  unset($this->static_screens['roles']);
53  unset($this->static_screens['layouts']);
54 
55  }//end constructor
56 
57 
68  function paintSystemId(&$asset, &$o, $prefix)
69  {
70  $wa = $asset->writeAccess('attributes');
71 
72  $current_product = $asset->getCurrentProduct();
73  if (empty($current_product)) {
74  echo translate('suite_not_registered');
75  } else {
76  $o->openField(translate('suite_system_id'));
77  echo array_get_index($current_product, 'systemid', '');
78  $o->closeField();
79  $o->openField(translate('suite_system_name'));
80  $system_name = array_get_index($current_product['connection'], 'name', '');
81  if ($wa) {
82  echo text_box($prefix.'_current_name', $system_name, 30);
83  } else {
84  echo $system_name;
85  }
86  $o->closeField();
87  $o->openField(translate('suite_system_url'));
88  echo array_get_index($current_product, 'url', '');
89  $o->closeField();
90  }//end if
91 
92  if ($wa) {
93  $o->openField(translate('suite_tools_register'));
94  $click = '';
95  if (!empty($current_product)) {
96  $click = 'if (this.checked === true) { if (!confirm(\''.translate('js_confirm_msg').'\')) { this.checked= false }};';
97  }
98  echo check_box($prefix.'_register', '1', FALSE, $click);
99  echo label(translate('suite_register'), $prefix.'_register');
100  $o->closeField();
101  if (!empty($current_product)) {
102  $o->openField(translate('suite_tools_deregister'));
103  echo check_box($prefix.'_deregister', '1', FALSE, 'if (this.checked == true) { if (!confirm(\''.translate('js_confirm_msg').'\')) { this.checked = false } };');
104  echo label(translate('suite_deregister'), $prefix.'_deregister');
105  $o->closeField();
106  }
107  }
108 
109  return TRUE;
110 
111  }//end paintSystemId()
112 
113 
124  function processSystemId(&$asset, &$o, $prefix)
125  {
126  $wa = $asset->writeAccess('attributes');
127  $current_product = $asset->getCurrentProduct();
128 
129  $register = array_get_index($_POST, $prefix.'_register', '0');
130  $deregister = array_get_index($_POST, $prefix.'_deregister', '0');
131 
132  if ($deregister == '1' && !empty($current_product)) {
133  $suiteid = array_get_index($current_product, 'suiteid', '0');
134  if (!empty($suiteid)) {
135  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
136  try {
137  $asset->removeProduct($suiteid, TRUE);
138  } catch (PDOException $e) {
139  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
140  return FALSE;
141  }//end try
142  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
143 
144  // Remove happened, so finish processing (you cannot deregister and then register on the same screen)
145  return TRUE;
146  }//end if
147  }//end if
148 
149  if ($register == '1') {
150  if (empty($current_product)) {
151  // New product, register.
152  $systemid = self::_getSystemid();
153  $url = $asset->getUrl();
154  $connection = array(
155  'name' => 'Squiz Matrix',
156  );
157  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
158  try {
159  $suiteid = $asset->registerProduct($systemid, 'Squiz Matrix', $url, $connection);
160  $asset->updateProduct($suiteid, 'is_current', TRUE);
161  $asset->generateSystemKeyPair();
162  } catch (PDOException $e) {
163  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
164  return FALSE;
165  }//end try
166  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
167  } else {
168  // New systemid, keep old details.
169  $newid = self::_getSystemid();
170  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
171  try {
172  $asset->updateProduct($current_product['suiteid'], 'systemid', $newid);
173  } catch (PDOException $e) {
174  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
175  return FALSE;
176  }//end try
177  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
178  }//end if
179  }//end if
180 
181  // Finally, save the name by reloading the current product.
182  $current_product = $asset->getCurrentProduct();
183  if ($wa && !empty($current_product)) {
184  $connection = array_get_index($current_product, 'connection', array());
185  $current_name = array_get_index($connection, 'name', '');
186  $new_name = array_get_index($_POST, $prefix.'_current_name', '');
187  if (!empty($new_name) && $new_name !== $current_name) {
188  // Change the name
189  $connection['name'] = $new_name;
190  $asset->updateProduct($current_product['suiteid'], 'connection', $connection);
191 
192  // Send the request to other systems
193  $message = array(
194  'updateType' => 'name',
195  'updateValue' => $new_name,
196  );
197  $liveProducts = $asset->getLiveProducts();
198  foreach ($liveProducts as $liveProduct) {
199  $response = $asset->sendRequest($liveProduct['suiteid'], 'xml', 'SquizSuite', 'suiteProductUpdate', $message);
200  }//end foreach
201  }//end if
202  }//end if
203 
204  return TRUE;
205 
206  }//end processSystemId()
207 
208 
215  private function _getSystemid()
216  {
217  $systemid = 'matrix-';
218  $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
219  $charsLen = strlen($chars);
220  $randLen = 10;
221  for ($i = 0; $i < $randLen; $i++) {
222  $idx = rand(0, ($charsLen - 1));
223  $systemid .= $chars[$idx];
224  }
225 
226  return $systemid;
227 
228  }//end _getSystemid()
229 
230 
241  function paintProducts(&$asset, &$o, $prefix)
242  {
243  $wa = $asset->writeAccess('attributes');
244 
245  ?>
246  <table class="sq-backend-table" style="width:100%">
247  <tr>
248  <td class="sq-backend-table-header" width="20%"><?php echo translate('suite_system_id'); ?></td>
249  <td class="sq-backend-table-header" width="50%"><?php echo translate('suite_system_name'); ?></td>
250  <td class="sq-backend-table-header" width="20%"><?php echo translate('suite_system_type'); ?></td>
251  <td class="sq-backend-table-header" width="20%"><?php echo translate('suite_status'); ?></td>
252  <?php if ($wa) { ?><td class="sq-backend-table-header"><?php echo translate('suite_activate'); ?></td><?php } ?>
253  <?php if ($wa) { ?><td class="sq-backend-table-header"><?php echo translate('delete'); ?>?</td><?php } ?>
254  </tr>
255  <?php
256 
257  $products = $asset->getProducts(FALSE, TRUE);
258  foreach ($products as $product) {
259  $id = $product['suiteid'];
260 
261  echo '<tr>';
262  echo '<td>';
263  echo $product['systemid'];
264  echo '</td>';
265  echo '<td>';
266  echo array_get_index($product['connection'], 'name', '');
267  echo '</td>';
268  echo '<td>';
269  echo $product['type'];
270  echo '</td>';
271  echo '<td>';
272  echo ucwords($product['status']);
273  echo '</td>';
274 
275  if ($wa) {
276  echo '<td>';
277  if ($product['type'] != 'unknown' && $product['status'] !== 'live' && $product['status'] !== 'failed') {
278  check_box($prefix.'_activate[]', $id);
279  }
280  echo '</td>';
281  echo '<td>';
282  $checked = FALSE;
283  if ($product['sync_status'] === 'D') {
284  $checked = TRUE;
285  }
286  check_box($prefix.'_remove[]', $id, $checked);
287  echo '</td>';
288  }//end if
289  echo '</tr>';
290 
291  // Further for Matrix products, add a username/password field.
292  if ($wa && $product['type'] == 'Squiz Matrix') {
293  echo '<tr>';
294  echo '<td>'.translate('username').'</td>';
295  echo '<td colspan="2">';
296  echo text_box($prefix.'_username['.$product['suiteid'].']', array_get_index($product['connection'], 'username', ''), 30);
297  echo '</td>';
298  echo '<td>'.translate('password').'</td>';
299  echo '<td colspan="2">';
300  echo password_box($prefix.'_password['.$product['suiteid'].']', array_get_index($product['connection'], 'password', ''), 30);
301  echo '</td>';
302  echo '</tr>';
303  }//end if
304  }//end foreach
305 
306  echo '</table>';
307 
308  return TRUE;
309 
310  }//end paintProducts()
311 
312 
323  function processProducts(&$asset, &$o, $prefix)
324  {
325  $activate = array_get_index($_POST, $prefix.'_activate', Array());
326  $remove = array_get_index($_POST, $prefix.'_remove', Array());
327 
328  foreach ($activate as $activate_system) {
329  // If found here, then removing the system, so skip activation...
330  if (in_array($activate_system, $remove)) continue;
331 
332  $response = $asset->sendMessage($activate_system, 'suiteConnectAck');
333 
334  if (!headers_sent()) {
335  header('Content-type: text/html;');
336  }
337 
338  if ($response['curlInfo']['http_code'] !== 200) {
339  trigger_localised_error('SQS0021', E_USER_WARNING, $response['curlInfo']['http_code']);
340  }//end if
341  }//end foreach
342 
343  $live_products = $asset->getProducts(FALSE, FALSE, array('Squiz Update'), array('live'));
344  $live_prod_ids = array();
345  foreach ($live_products as $live_product) {
346  $live_prod_ids[] = $live_product['suiteid'];
347  }
348 
349  $updateSyncStatus = TRUE;
350  $productsToSyncAfter = array_diff($live_prod_ids, $remove);
351  if (empty($productsToSyncAfter) === TRUE) {
352  $updateSyncStatus = FALSE;
353  }
354 
355  foreach ($remove as $remove_system) {
356  if ($updateSyncStatus === TRUE) {
357  $asset->updateProduct($remove_system, 'sync_status', 'D');
358  } else {
359  $asset->removeProduct($remove_system);
360  }
361  }//end foreach
362 
363  // Matrix usernames and passwords save.
364  $usernames = array_get_index($_POST, $prefix.'_username', array());
365  $passwords = array_get_index($_POST, $prefix.'_password', array());
366  $broadcast = FALSE;
367  foreach ($usernames as $suiteid => $username) {
368  $product = $asset->getProduct($suiteid);
369  if (!empty($product)) {
370  $current_user = array_get_index($product['connection'], 'username', '');
371  if (!empty($username) && $username !== $current_user) {
372  $connection = array_get_index($product, 'connection', array());
373  $connection['username'] = $username;
374  $asset->updateProduct($suiteid, 'connection', $connection);
375  }
376  }//end if
377  }//end foreach
378 
379  foreach ($passwords as $suiteid => $password) {
380  $product = $asset->getProduct($suiteid);
381  if (!empty($product)) {
382  $current_pass = array_get_index($product['connection'], 'password', '');
383  if (!empty($password) && $password !== $current_pass) {
384  $connection = array_get_index($product, 'connection', array());
385  $connection['password'] = $password;
386  $asset->updateProduct($suiteid, 'connection', $connection);
387  }
388  }//end if
389  }//end foreach
390 
391  return TRUE;
392 
393  }//end processProducts()
394 
395 
406  function paintRegister(&$asset, &$o, $prefix)
407  {
408  $wa = $asset->writeAccess('attributes');
409  $current = $asset->getCurrentProduct();
410 
411  if ($wa && !empty($current)) {
412  echo translate('suite_new_product_url').' ';
413  text_box($prefix.'_new_product', '');
414  }//end if
415 
416  return TRUE;
417 
418  }//end paintRegister()
419 
420 
431  function processRegister(&$asset, &$o, $prefix)
432  {
433  $product_url = array_get_index($_POST, $prefix.'_new_product', '');
434  $product_url = trim($product_url);
435  $product_url = rtrim($product_url, '/');
436 
437  if (!empty($product_url)) {
438  if (strpos($product_url, 'http://') !== 0 && strpos($product_url, 'https://') !== 0) {
439  $product_url = 'http://'.$product_url;
440  }
441 
442  if (strpos($product_url, '__api') !== (strlen($product_url) - 5)) {
443  $product_url .= '/__api';
444  }
445 
446  $response = $asset->sendMessage($product_url, 'suiteConnect');
447 
448  if ($response['curlInfo']['http_code'] === 404) {
449  // It might be another Matrix system, try without __api
450  $product_url = substr($product_url, 0, strpos($product_url, '__api'));
451  $response = $asset->sendMessage($product_url, 'suiteConnect');
452  }//end if
453 
454  if ($response['curlInfo']['http_code'] === 200) {
455  $tempid = uniqid();
456  $systemType = 'unknown';
457  $systemName = 'Unknown System';
458  $connection = array('name' => $systemName);
459  $suiteid = $asset->registerProduct($tempid, $systemType, $product_url, $connection);
460  $asset->updateProduct($suiteid, 'status', 'pending');
461  } else {
462  trigger_localised_error('SQS0020', E_USER_WARNING, $response['curlInfo']['http_code']);
463  }//end if
464  }//end if
465 
466  return TRUE;
467 
468  }//end processRegister()
469 
470 
471 }//end class
472 ?>