1<?php 2 3use dokuwiki\plugin\structpublish\meta\Constants; 4use dokuwiki\plugin\structpublish\meta\Revision; 5 6class action_plugin_structpublish_publish extends DokuWiki_Action_Plugin 7{ 8 /** @var \helper_plugin_structpublish_db */ 9 protected $dbHelper; 10 11 /** @inheritDoc */ 12 public function register(Doku_Event_Handler $controller) 13 { 14 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleApprove'); 15 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handlePublish'); 16 } 17 18 /** 19 * Handle the publish button and version field 20 * 21 * @param Doku_Event $event 22 * @return void 23 */ 24 public function handlePublish(Doku_Event $event) 25 { 26 if ($event->data != 'show') return; 27 28 $this->dbHelper = plugin_load('helper', 'structpublish_db'); 29 30 global $INPUT; 31 $in = $INPUT->arr('structpublish'); 32 if (!$in || !isset($in[Constants::ACTION_PUBLISH])) { 33 return; 34 } 35 36 if(checkSecurityToken()) { 37 $this->saveRevision(Constants::STATUS_PUBLISHED, $INPUT->str('version')); 38 $this->updateSchemaData(); 39 } 40 } 41 42 /** 43 * Handle the approve button 44 * 45 * @param Doku_Event $event 46 * @return void 47 */ 48 public function handleApprove(Doku_Event $event) 49 { 50 if ($event->data != 'show') return; 51 52 $this->dbHelper = plugin_load('helper', 'structpublish_db'); 53 54 global $INPUT; 55 $in = $INPUT->arr('structpublish'); 56 if (!$in || !isset($in[Constants::ACTION_APPROVE])) { 57 return; 58 } 59 60 if(checkSecurityToken()) { 61 $this->saveRevision(Constants::STATUS_APPROVED); 62 } 63 } 64 65 /** 66 * Save publish data 67 * 68 * @todo check user role 69 * @param string $status 70 * @return void 71 */ 72 protected function saveRevision($status, $newversion='') 73 { 74 global $ID; 75 global $INFO; 76 77 // FIXME prevent bumping an already published revision 78 $sqlite = $this->dbHelper->getDB(); 79 $revision = new Revision($sqlite, $ID, $INFO['currentrev']); 80 81 if ($status === Constants::STATUS_PUBLISHED) { 82 $revision->setVersion($newversion); 83 } 84 $revision->setUser($_SERVER['REMOTE_USER']); 85 $revision->setStatus($status); 86 $revision->setTimestamp(time()); 87 $revision->save(); 88 } 89 90 /** 91 * Set "published" status in all assigned schemas 92 * 93 * @return void 94 */ 95 protected function updateSchemaData() 96 { 97 global $ID; 98 global $INFO; 99 100 $schemaAssignments = \dokuwiki\plugin\struct\meta\Assignments::getInstance(); 101 $tables = $schemaAssignments->getPageAssignments($ID); 102 103 if (empty($tables)) return; 104 105 $sqlite = $this->dbHelper->getDB(); 106 107 foreach ($tables as $table) { 108 // TODO unpublish earlier revisions 109 $sqlite->query( "UPDATE data_$table SET published = 1 WHERE pid = ? AND rev = ?", [$ID, $INFO['currentrev']]); 110 $sqlite->query( "UPDATE multi_$table SET published = 1 WHERE pid = ? AND rev = ?", [$ID, $INFO['currentrev']]); 111 } 112 } 113} 114