1<?php 2use dokuwiki\plugin\struct\meta\AccessTable; 3use dokuwiki\plugin\struct\meta\SchemaImporter; 4use dokuwiki\plugin\struct\meta\StructException; 5 6/** 7 * DokuWiki Plugin swarmwebhook (Helper Component) 8 * 9 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 10 * @author Michael Große <mic.grosse@googlemail.com> 11 */ 12 13class helper_plugin_swarmwebhook extends DokuWiki_Plugin 14{ 15 16 /** 17 * Transforms a timestamp and the timezone offset as provided in the payload into an DateTimeInterface instance 18 * 19 * @param int $timestamp 20 * @param int $payloadTimezoneOffset offset to UTC in minutes 21 * 22 * @return DateTimeInterface 23 */ 24 public function getDateTimeInstance($timestamp, $payloadTimezoneOffset) 25 { 26 $tzSign = $payloadTimezoneOffset >= 0 ? '+' : '-'; 27 $offsetInHours = $payloadTimezoneOffset / 60; 28 $tz = $tzSign . str_pad($offsetInHours * 100, 4, '0', STR_PAD_LEFT); 29 $dateTime = new DateTime('now', new DateTimeZone($tz)); 30 $dateTime->setTimestamp($timestamp); 31 return $dateTime; 32 } 33 34 /** 35 * @param array $data associative array in the form of [columnname => columnvalue] 36 */ 37 public function saveDataToLookup(array $data) 38 { 39 /** @var helper_plugin_struct $structHelper */ 40 $structHelper = plugin_load('helper', 'struct'); 41 $access = AccessTable::byTableName('swarm', 0, 0); 42 if (method_exists($structHelper, 'saveLookupData')) { 43 $structHelper->saveLookupData($access, $data); 44 return; 45 } 46 47 // old struct version - we save the data ourselves! 48 49 dbglog('Please update your struct plugin!'); 50 if (!$access->getSchema()->isEditable()) { 51 throw new StructException('lookup save error: no permission for schema'); 52 } 53 $validator = $access->getValidator($data); 54 if (!$validator->validate()) { 55 throw new StructException("Validation failed:\n%s", implode("\n", $validator->getErrors())); 56 } 57 if (!$validator->saveData()) { 58 throw new StructException('No data saved'); 59 } 60 } 61 62 /** 63 * Deletes a checkin from the lookup 64 * 65 * @param string $checkinid 66 */ 67 public function deleteCheckinFromLookup($checkinid) 68 { 69 $tablename = 'swarm'; 70 71 /** @var remote_plugin_struct $remote */ 72 $remote = plugin_load('remote', 'struct'); 73 $rows = $remote->getAggregationData( 74 [$tablename], 75 ['%rowid%'], 76 [['logic'=> 'and', 'condition' => "checkinid = $checkinid"]] 77 ); 78 79 $pids = array_column($rows, '%rowid%'); 80 81 if (empty($pids)) { 82 return; 83 } 84 foreach ($pids as $pid) { // should only be a single entry 85 $schemadata = AccessTable::byTableName($tablename, $pid); 86 if (!$schemadata->getSchema()->isEditable()) { 87 throw new StructException('lookup delete error: no permission for schema'); 88 } 89 $schemadata->clearData(); 90 } 91 } 92 93 /** 94 * Create a new struct schema from the struct json file in the plugin dir 95 */ 96 public function createNewSwarmSchema() 97 { 98 $json = file_get_contents(__DIR__ . '/swarm.struct.json'); 99 $builder = new SchemaImporter('swarm', $json, true); 100 if (!$builder->build()) { 101 msg('something went wrong while saving', -1); 102 } 103 touch(action_plugin_struct_cache::getSchemaRefreshFile()); 104 } 105} 106