4 * Copyright 2011, Franck Villaume - Capgemini
5 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
6 * Copyright 2012, Franck Villaume - TrivialDev
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 class scmhookPlugin extends Plugin {
25 function __construct() {
27 $this->name = "scmhook";
28 $this->text = "Scmhook"; // To show in the tabs, use...
29 $this->_addHook('groupmenu'); // To put into the project tabs
30 $this->_addHook('groupisactivecheckbox'); // The "use ..." checkbox in editgroupinfo
31 $this->_addHook('groupisactivecheckboxpost'); //
32 $this->_addHook('scm_admin_page');
33 $this->_addHook('scm_admin_update');
34 $this->_addHook('artifact_extra_detail');
35 $this->_addHook('task_extra_detail');
38 function CallHook($hookname, &$params) {
41 case "scm_admin_page": {
42 $group_id = $params['group_id'];
43 $scm_plugin = $params['scm_plugin'];
44 $group = &group_get_object($group_id);
45 if ($group->usesPlugin($this->name) && $scm_plugin) {
46 $this->displayScmHook($group_id, $scm_plugin);
50 case "scm_admin_update": {
51 $this->update($params);
54 case "artifact_extra_detail": {
55 $group_id = $params['group_id'];
56 $group = &group_get_object($group_id);
57 if ($group->usesPlugin($this->name)) {
58 $this->artifact_extra_detail($params);
62 case "task_extra_detail": {
63 $group_id = $params['group_id'];
64 $group = &group_get_object($group_id);
65 if ($group->usesPlugin($this->name)) {
66 $this->task_extra_detail($params);
74 function exists($group_id) {
75 $res = db_query_params('SELECT id_group FROM plugin_scmhook WHERE id_group = $1', array($group_id));
85 function add($group_id) {
86 if (!$this->exists($group_id)) {
87 $res = db_query_params('INSERT INTO plugin_scmhook (id_group) VALUES ($1)', array($group_id));
95 function remove($group_id) {
96 if ($this->exists($group_id)) {
97 $res = db_query_params('DELETE FROM plugin_scmhook where id_group = $1', array($group_id));
105 function update($params) {
106 $group_id = $params['group_id'];
108 foreach($params as $key => $value) {
109 if ($key == strstr($key, 'scm')) {
110 $hookname = preg_replace('/scm[a-z][a-z]+_/','',$key);
111 if ($key != $hookname) { //handle the case of scm_enable_anonymous
112 if (strlen($hooksString)) {
113 $hooksString .= '|'.$hookname;
115 $hooksString .= $hookname;
120 $res = db_query_params('UPDATE plugin_scmhook set hooks = $1, need_update = 1 where id_group = $2',
121 array($hooksString, $group_id));
129 function displayScmHook($group_id, $scm) {
130 $hooksAvailable = $this->getAvailableHooks($group_id);
131 $statusDeploy = $this->getStatusDeploy($group_id);
132 $hooksEnabled = $this->getEnabledHooks($group_id);
133 if (count($hooksAvailable)) {
134 echo '<div id="scmhook">';
136 echo '<p class="warning">'._('Hooks management update process waiting ...').'</p>';
138 echo '<h2>'._('Enable Repository Hooks').'</h2>';
141 $this->displayScmSvnHook($hooksAvailable, $statusDeploy, $hooksEnabled);
145 $this->displayScmHgHook($hooksAvailable, $statusDeploy, $hooksEnabled);
151 echo '<p class="information">'._('No hooks available').'</p>';
155 function getStatusDeploy($group_id) {
156 $res = db_query_params('SELECT need_update FROM plugin_scmhook WHERE id_group = $1', array($group_id));
160 $row = db_fetch_array($res);
161 return $row['need_update'];
164 function getAvailableHooks($group_id) {
165 $listScm = $this->getListLibraryScm();
166 $group = &group_get_object($group_id);
167 for ($i = 0; $i < count($listScm); $i++) {
168 if ($group->usesPlugin($listScm[$i])) {
169 return $this->getListLibraryHook($listScm[$i]);
175 function getEnabledHooks($group_id) {
176 $res = db_query_params('SELECT hooks FROM plugin_scmhook WHERE id_group = $1', array($group_id));
180 $row = db_fetch_array($res);
182 return explode('|', $row['hooks']);
188 function getListLibraryScm() {
189 return array_values(array_diff(scandir(dirname(__FILE__).'/../library/'), Array('.', '..', '.svn')));
192 function getListLibraryHook($scm) {
193 $listHooks = array_values(array_diff(scandir(dirname(__FILE__).'/../library/'.$scm), array('.', '..', '.svn', 'skel', 'hooks', 'depends', 'cronjobs')));
194 $validHooks = array();
195 foreach ($listHooks as $hook) {
196 if (!stristr($hook,'~')) {
197 include_once dirname(__FILE__).'/../library/'.$scm.'/'.$hook;
198 $hookClassName = preg_replace('/.class.php/','', $hook);
199 $hookObject = new $hookClassName;
200 $validHooks[] = $hookObject;
206 function artifact_extra_detail($params) {
207 $hooksAvailable = $this->getAvailableHooks($params['group_id']);
208 $hooksEnabled = $this->getEnabledHooks($params['group_id']);
209 foreach ($hooksAvailable as $hookAvailable) {
210 if (in_array($hookAvailable->getClassname(), $hooksEnabled)) {
211 if (method_exists($hookAvailable,'artifact_extra_detail')) {
212 $hookAvailable->artifact_extra_detail($params);
218 function task_extra_detail($params) {
219 $hooksAvailable = $this->getAvailableHooks($params['group_id']);
220 $hooksEnabled = $this->getEnabledHooks($params['group_id']);
221 foreach ($hooksAvailable as $hookAvailable) {
222 if (in_array($hookAvailable->getClassname(), $hooksEnabled)) {
223 if (method_exists($hookAvailable,'task_extra_detail')) {
224 $hookAvailable->task_extra_detail($params);
231 * override default groupisactivecheckboxpost function for init value in db
233 function groupisactivecheckboxpost(&$params) {
234 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
235 $group = group_get_object($params['group']);
236 $flag = strtolower('use_'.$this->name);
237 if (getIntFromRequest($flag) == 1) {
238 $group->setPluginUse($this->name);
239 $this->add($group->getID());
241 $group->setPluginUse($this->name, false);
242 $this->remove($group->getID());
247 function displayScmSvnHook($hooksAvailable, $statusDeploy, $hooksEnabled) {
249 $hooksPreCommit = array();
250 $hooksPreRevPropChange = array();
251 $hooksPostCommit = array();
252 foreach ($hooksAvailable as $hook) {
253 switch ($hook->getHookType()) {
255 $hooksPreCommit[] = $hook;
258 case "pre-revprop-change": {
259 $hooksPreRevPropChange[] = $hook;
262 case "post-commit": {
263 $hooksPostCommit[] = $hook;
267 //byebye hook.... we do not know you...
272 if (count($hooksPreCommit)) {
273 echo '<h3>'._('pre-commit Hooks').'</h3>';
274 $tabletop = array('', _('Hook Name'), _('Description'));
275 $classth = array('unsortable', '', '');
276 echo $HTML->listTableTop($tabletop, false, 'sortable_scmhook_precommit', 'sortable', $classth);
277 foreach ($hooksPreCommit as $hookPreCommit) {
278 if (! empty($hookPreCommit->onlyGlobalAdmin) && ! Permission::isGlobalAdmin()) {
279 echo '<tr style="display: none;" ><td>';
284 echo '<input type="checkbox" ';
285 echo 'name="'.$hookPreCommit->getLabel().'_'.$hookPreCommit->getClassname().'" ';
286 if (in_array($hookPreCommit->getClassname(), $hooksEnabled))
287 echo ' checked="checked"';
290 echo ' disabled="disabled"';
294 echo $hookPreCommit->getName();
296 echo $hookPreCommit->getDescription();
299 echo $HTML->listTableBottom();
301 if (count($hooksPreRevPropChange)) {
302 echo '<h3>'._('pre-revprop-change Hooks').'</h3>';
303 $tabletop = array('', _('Hook Name'), _('Description'));
304 $classth = array('unsortable', '', '');
305 echo $HTML->listTableTop($tabletop, false, 'sortable_scmhook_precommit', 'sortable', $classth);
306 foreach ($hooksPreRevPropChange as $hook) {
307 if (! empty($hook->onlyGlobalAdmin) && ! Permission::isGlobalAdmin()) {
308 echo '<tr style="display: none;" ><td>';
313 echo '<input type="checkbox" ';
314 echo 'name="'.$hook->getLabel().'_'.$hook->getClassname().'" ';
315 if (in_array($hook->getClassname(), $hooksEnabled))
316 echo ' checked="checked"';
319 echo ' disabled="disabled"';
323 echo $hook->getName();
325 echo $hook->getDescription();
328 echo $HTML->listTableBottom();
330 if (count($hooksPostCommit)) {
331 echo '<h3>'._('post-commit Hooks').'</h3>';
332 $tabletop = array('', _('Hook Name'), _('Description'));
333 $classth = array('unsortable', '', '');
334 echo $HTML->listTableTop($tabletop, false, 'sortable_scmhook_postcommit', 'sortable', $classth);
335 foreach ($hooksPostCommit as $hookPostCommit) {
336 if (! empty($hookPostCommit->onlyGlobalAdmin) && ! Permission::isGlobalAdmin()) {
337 echo '<tr style="display: none;" ><td>';
342 echo '<input type="checkbox" ';
343 echo 'name="'.$hookPostCommit->getLabel().'_'.$hookPostCommit->getClassname().'" ';
344 if (in_array($hookPostCommit->getClassname(), $hooksEnabled))
345 echo ' checked="checked"';
348 echo ' disabled="disabled"';
352 echo $hookPostCommit->getName();
354 echo $hookPostCommit->getDescription();
357 echo $HTML->listTableBottom();
361 function displayScmHgHook($hooksAvailable, $statusDeploy, $hooksEnabled) {
363 $hooksServePushPullBundle = array();
364 foreach ($hooksAvailable as $hook) {
365 switch ($hook->getHookType()) {
366 case "serve-push-pull-bundle": {
367 $hooksServePushPullBundle[] = $hook;
371 //byebye hook.... we do not know you...
376 if (count($hooksServePushPullBundle)) {
377 echo '<h3>'._('serve-push-pull-bundle Hooks').'</h3>';
378 $tabletop = array('', _('Hook Name'), _('Description'));
379 $classth = array('unsortable', '', '');
380 echo $HTML->listTableTop($tabletop, false, 'sortable_scmhook_serve-push-pull-bundle', 'sortable', $classth);
381 foreach ($hooksServePushPullBundle as $hookServePushPullBundle) {
382 if (! empty($hookServePushPullBundle->onlyGlobalAdmin) && ! Permission::isGlobalAdmin()) {
383 echo '<tr style="display: none;" ><td>';
388 echo '<input type="checkbox" ';
389 echo 'name="'.$hookServePushPullBundle->getLabel().'_'.$hookServePushPullBundle->getClassname().'" ';
390 if (in_array($hookServePushPullBundle->getClassname(), $hooksEnabled))
391 echo ' checked="checked"';
394 echo ' disabled="disabled"';
398 echo $hookServePushPullBundle->getName();
400 echo $hookServePushPullBundle->getDescription();
403 echo $HTML->listTableBottom();
410 // c-file-style: "bsd"