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);
63 function exists($group_id) {
64 $res = db_query_params('SELECT id_group FROM plugin_scmhook WHERE id_group = $1', array($group_id));
74 function add($group_id) {
75 if (!$this->exists($group_id)) {
76 $res = db_query_params('INSERT INTO plugin_scmhook (id_group) VALUES ($1)', array($group_id));
84 function remove($group_id) {
85 if ($this->exists($group_id)) {
86 $res = db_query_params('DELETE FROM plugin_scmhook where id_group = $1', array($group_id));
94 function update($params) {
95 $group_id = $params['group_id'];
97 foreach($params as $key => $value) {
98 if ($key == strstr($key, 'scm')) {
99 $hookname = preg_replace('/scm[a-z][a-z][a-z]_/','',$key);
100 if ($key != $hookname) { //handle the case of scm_enable_anonymous
101 if (strlen($hooksString)) {
102 $hooksString .= '|'.$hookname;
104 $hooksString .= $hookname;
109 $res = db_query_params('UPDATE plugin_scmhook set hooks = $1, need_update = 1 where id_group = $2',
110 array($hooksString, $group_id));
118 function displayScmHook($group_id) {
120 use_javascript('/js/sortable.js');
121 $hooksAvailable = $this->getAvailableHooks($group_id);
122 $statusDeploy = $this->getStatusDeploy($group_id);
123 $hooksEnabled = $this->getEnabledHooks($group_id);
124 if (count($hooksAvailable)) {
125 echo '<div id="scmhook">';
127 echo '<p class="warning">'._('Hooks management update process waiting ...').'</p>';
129 echo '<h3>'._('Enable Repository Hooks').'</h3>';
130 $hooksPreCommit = array();
131 $hooksPostCommit = array();
132 foreach ($hooksAvailable as $hook) {
133 switch ($hook->getHookType()) {
135 $hooksPreCommit[] = $hook;
138 case "post-commit": {
139 $hooksPostCommit[] = $hook;
144 //byebye hook.... we do not validate you...
149 if (count($hooksPreCommit)) {
150 echo '<h4>'._('pre-commit Hooks').'</h4>';
151 $tabletop = array('', _('Hook Name'), _('Description'));
152 $classth = array('unsortable', '', '');
153 echo $HTML->listTableTop($tabletop, false, 'sortable_scmhook_precommit', 'sortable', $classth);
154 foreach ($hooksPreCommit as $hookPreCommit) {
156 echo '<input type="checkbox" ';
157 echo 'name="'.$hookPreCommit->getLabel().'_'.$hookPreCommit->getClassname().'" ';
158 if (in_array($hookPreCommit->getClassname(), $hooksEnabled))
159 echo ' checked="checked"';
162 echo ' disabled="disabled"';
166 echo $hookPreCommit->getName();
168 echo $hookPreCommit->getDescription();
171 echo $HTML->listTableBottom();
173 if (count($hooksPostCommit)) {
174 echo '<h4>'._('post-commit Hooks').'</h4>';
175 $tabletop = array('', _('Hook Name'), _('Description'));
176 $classth = array('unsortable', '', '');
177 echo $HTML->listTableTop($tabletop, false, 'sortable_scmhook_postcommit', 'sortable', $classth);
178 foreach ($hooksPostCommit as $hookPostCommit) {
180 echo '<input type="checkbox" ';
181 echo 'name="'.$hookPostCommit->getLabel().'_'.$hookPostCommit->getClassname().'" ';
182 if (in_array($hookPostCommit->getClassname(), $hooksEnabled))
183 echo ' checked="checked"';
186 echo ' disabled="disabled"';
190 echo $hookPostCommit->getName();
192 echo $hookPostCommit->getDescription();
195 echo $HTML->listTableBottom();
199 echo '<p class="information">'._('No hooks available').'</p>';
203 function getStatusDeploy($group_id) {
204 $res = db_query_params('SELECT need_update FROM plugin_scmhook WHERE id_group = $1', array($group_id));
208 $row = db_fetch_array($res);
209 return $row['need_update'];
212 function getAvailableHooks($group_id) {
213 $listScm = $this->getListLibraryScm();
214 $group = &group_get_object($group_id);
215 for ($i = 0; $i < count($listScm); $i++) {
216 if ($group->usesPlugin($listScm[$i])) {
217 return $this->getListLibraryHook($listScm[$i]);
223 function getEnabledHooks($group_id) {
224 $res = db_query_params('SELECT hooks FROM plugin_scmhook WHERE id_group = $1', array($group_id));
228 $row = db_fetch_array($res);
230 return explode('|', $row['hooks']);
236 function getListLibraryScm() {
237 return array_values(array_diff(scandir(dirname(__FILE__).'/../library/'), Array('.', '..', '.svn')));
240 function getListLibraryHook($scm) {
241 $listHooks = array_values(array_diff(scandir(dirname(__FILE__).'/../library/'.$scm), array('.', '..', '.svn', 'skel', 'hooks', 'depends', 'cronjobs')));
242 $validHooks = array();
243 foreach ($listHooks as $hook) {
244 if (!stristr($hook,'~')) {
245 include dirname(__FILE__).'/../library/'.$scm.'/'.$hook;
246 $hookClassName = preg_replace('/.class.php/','', $hook);
247 $hookObject = new $hookClassName;
248 $validHooks[] = $hookObject;
254 function artifact_extra_detail($params) {
255 $hooksAvailable = $this->getAvailableHooks($params['group_id']);
256 $hooksEnabled = $this->getEnabledHooks($params['group_id']);
257 foreach ($hooksAvailable as $hookAvailable) {
258 if (in_array($hookAvailable->getClassname(), $hooksEnabled)) {
259 if (method_exists($hookAvailable,'artifact_extra_detail')) {
260 $hookAvailable->artifact_extra_detail($params);
266 function task_extra_detail($params) {
267 $hooksAvailable = $this->getAvailableHooks($params['group_id']);
268 $hooksEnabled = $this->getEnabledHooks($params['group_id']);
269 foreach ($hooksAvailable as $hookAvailable) {
270 if (in_array($hookAvailable->getClassname(), $hooksEnabled)) {
271 if (method_exists($hookAvailable,'task_extra_detail')) {
272 $hookAvailable->task_extra_detail($params);
279 * override default groupisactivecheckboxpost function for init value in db
281 function groupisactivecheckboxpost(&$params) {
282 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
283 $group = group_get_object($params['group']);
284 $flag = strtolower('use_'.$this->name);
285 if (getIntFromRequest($flag) == 1) {
286 $group->setPluginUse($this->name);
287 $this->add($group->getID());
289 $group->setPluginUse($this->name, false);
290 $this->remove($group->getID());
298 // c-file-style: "bsd"