4 * Copyright 2011, Franck Villaume - Capgemini
6 * This file is part of FusionForge. FusionForge is free software;
7 * you can redistribute it and/or modify it under the terms of the
8 * GNU General Public License as published by the Free Software
9 * Foundation; either version 2 of the Licence, or (at your option)
12 * FusionForge is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 class scmhookPlugin extends Plugin {
23 function scmhookPlugin () {
25 $this->name = "scmhook" ;
26 $this->text = "Scmhook" ; // To show in the tabs, use...
27 $this->_addHook('groupmenu'); // To put into the project tabs
28 $this->_addHook('groupisactivecheckbox'); // The "use ..." checkbox in editgroupinfo
29 $this->_addHook('groupisactivecheckboxpost'); //
30 $this->_addHook('scm_admin_page');
31 $this->_addHook('scm_admin_update');
32 $this->_addHook('artifact_extra_detail');
33 $this->_addHook('task_extra_detail');
36 function CallHook($hookname, &$params) {
39 case "scm_admin_page": {
40 $group_id = $params['group_id'];
41 $group = &group_get_object($group_id);
42 if ($group->usesPlugin($this->name)) {
43 $this->displayScmHook($group_id);
47 case "scm_admin_update": {
48 $this->update($params);
51 case "artifact_extra_detail": {
52 $group_id = $params['group_id'];
53 $group = &group_get_object($group_id);
54 if ($group->usesPlugin($this->name)) {
55 $this->artifact_extra_detail($params);
59 case "task_extra_detail": {
60 $group_id = $params['group_id'];
61 $group = &group_get_object($group_id);
62 if ($group->usesPlugin($this->name)) {
63 $this->task_extra_detail($params);
71 function exists($group_id) {
72 $res = db_query_params('SELECT id_group FROM plugin_scmhook WHERE id_group = $1', array($group_id));
82 function add($group_id) {
83 if (!$this->exists($group_id)) {
84 $res = db_query_params('INSERT INTO plugin_scmhook (id_group) VALUES ($1)', array($group_id));
92 function remove($group_id) {
93 if ($this->exists($group_id)) {
94 $res = db_query_params('DELETE FROM plugin_scmhook where id_group = $1', array($group_id));
102 function update($params) {
103 $group_id = $params['group_id'];
105 foreach($params as $key => $value) {
106 if ($key == strstr($key, 'scm')) {
107 $hookname = preg_replace('/scm[a-z][a-z][a-z]_/','',$key);
108 if ($key != $hookname) { //handle the case of scm_enable_anonymous
109 if (strlen($hooksString)) {
110 $hooksString .= '|'.$hookname;
112 $hooksString .= $hookname;
117 $res = db_query_params('UPDATE plugin_scmhook set hooks = $1, need_update = 1 where id_group = $2',
118 array($hooksString, $group_id));
126 function displayScmHook($group_id) {
128 use_javascript('/js/sortable.js');
129 $hooksAvailable = $this->getAvailableHooks($group_id);
130 $statusDeploy = $this->getStatusDeploy($group_id);
131 $hooksEnabled = $this->getEnabledHooks($group_id);
132 if (count($hooksAvailable)) {
133 echo '<div id="scmhook">';
135 echo '<p class="warning">'._('Hooks management update process waiting ...').'</p>';
137 echo '<h2>'._('Enable Repository Hooks').'</h2>';
138 $hooksPreCommit = array();
139 $hooksPostCommit = array();
140 foreach ($hooksAvailable as $hook) {
141 switch ($hook->getHookType()) {
143 $hooksPreCommit[] = $hook;
146 case "post-commit": {
147 $hooksPostCommit[] = $hook;
152 //byebye hook.... we do not validate you...
157 if (count($hooksPreCommit)) {
158 echo '<h3>'._('pre-commit Hooks').'</h3>';
159 $tabletop = array('', _('Hook Name'), _('Description'));
160 $classth = array('unsortable', '', '');
161 echo $HTML->listTableTop($tabletop, false, 'sortable_scmhook_precommit', 'sortable', $classth);
162 foreach ($hooksPreCommit as $hookPreCommit) {
164 echo '<input type="checkbox" ';
165 echo 'name="'.$hookPreCommit->getLabel().'_'.$hookPreCommit->getClassname().'" ';
166 if (in_array($hookPreCommit->getClassname(), $hooksEnabled))
167 echo ' checked="checked"';
170 echo ' disabled="disabled"';
174 echo $hookPreCommit->getName();
176 echo $hookPreCommit->getDescription();
179 echo $HTML->listTableBottom();
181 if (count($hooksPostCommit)) {
182 echo '<h3>'._('post-commit Hooks').'</h3>';
183 $tabletop = array('', _('Hook Name'), _('Description'));
184 $classth = array('unsortable', '', '');
185 echo $HTML->listTableTop($tabletop, false, 'sortable_scmhook_postcommit', 'sortable', $classth);
186 foreach ($hooksPostCommit as $hookPostCommit) {
188 echo '<input type="checkbox" ';
189 echo 'name="'.$hookPostCommit->getLabel().'_'.$hookPostCommit->getClassname().'" ';
190 if (in_array($hookPostCommit->getClassname(), $hooksEnabled))
191 echo ' checked="checked"';
194 echo ' disabled="disabled"';
198 echo $hookPostCommit->getName();
200 echo $hookPostCommit->getDescription();
203 echo $HTML->listTableBottom();
207 echo '<p class="information">'._('No hooks available').'</p>';
211 function getStatusDeploy($group_id) {
212 $res = db_query_params('SELECT need_update FROM plugin_scmhook WHERE id_group = $1', array($group_id));
216 $row = db_fetch_array($res);
217 return $row['need_update'];
220 function getAvailableHooks($group_id) {
221 $listScm = $this->getListLibraryScm();
222 $group = &group_get_object($group_id);
223 for ($i = 0; $i < count($listScm); $i++) {
224 if ($group->usesPlugin($listScm[$i])) {
225 return $this->getListLibraryHook($listScm[$i]);
231 function getEnabledHooks($group_id) {
232 $res = db_query_params('SELECT hooks FROM plugin_scmhook WHERE id_group = $1', array($group_id));
236 $row = db_fetch_array($res);
238 return explode('|', $row['hooks']);
244 function getListLibraryScm() {
245 return array_values(array_diff(scandir(dirname(__FILE__).'/../library/'), Array('.', '..', '.svn')));
248 function getListLibraryHook($scm) {
249 $listHooks = array_values(array_diff(scandir(dirname(__FILE__).'/../library/'.$scm), array('.', '..', '.svn', 'skel', 'hooks', 'depends', 'cronjobs')));
250 $validHooks = array();
251 foreach ($listHooks as $hook) {
252 if (!stristr($hook,'~')) {
253 include_once dirname(__FILE__).'/../library/'.$scm.'/'.$hook;
254 $hookClassName = preg_replace('/.class.php/','', $hook);
255 $hookObject = new $hookClassName;
256 $validHooks[] = $hookObject;
262 function artifact_extra_detail($params) {
263 $hooksAvailable = $this->getAvailableHooks($params['group_id']);
264 $hooksEnabled = $this->getEnabledHooks($params['group_id']);
265 foreach ($hooksAvailable as $hookAvailable) {
266 if (in_array($hookAvailable->getClassname(), $hooksEnabled)) {
267 if (method_exists($hookAvailable,'artifact_extra_detail')) {
268 $hookAvailable->artifact_extra_detail($params);
274 function task_extra_detail($params) {
275 $hooksAvailable = $this->getAvailableHooks($params['group_id']);
276 $hooksEnabled = $this->getEnabledHooks($params['group_id']);
277 foreach ($hooksAvailable as $hookAvailable) {
278 if (in_array($hookAvailable->getClassname(), $hooksEnabled)) {
279 if (method_exists($hookAvailable,'task_extra_detail')) {
280 $hookAvailable->task_extra_detail($params);
287 * override default groupisactivecheckboxpost function for init value in db
289 function groupisactivecheckboxpost(&$params) {
290 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
291 $group = group_get_object($params['group']);
292 $flag = strtolower('use_'.$this->name);
293 if (getIntFromRequest($flag) == 1) {
294 $group->setPluginUse($this->name);
295 $this->add($group->getID());
297 $group->setPluginUse($this->name, false);
298 $this->remove($group->getID());
306 // c-file-style: "bsd"