/[drupal]/contributions/modules/versioncontrol/versioncontrol.admin.inc
ViewVC logotype

Contents of /contributions/modules/versioncontrol/versioncontrol.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.57 - (show annotations) (download) (as text)
Fri Oct 16 14:15:26 2009 UTC (6 weeks, 1 day ago) by sdboyer
Branch: MAIN
CVS Tags: HEAD
Changes since 1.56: +70 -56 lines
File MIME type: text/x-php
Merging in work from git by marvil07 for his GSOC project to OO-ify vcsapi. From this commit forward, HEAD is OO.
1 <?php
2 // $Id$
3 /**
4 * @file
5 * Version Control API - An interface to version control systems
6 * whose functionality is provided by pluggable back-end modules.
7 *
8 * This file contains the administrative user interface
9 * for accounts and repositories.
10 *
11 * Copyright 2006, 2007 Derek Wright ("dww" , http://drupal.org/user/46549)
12 * Copyright 2007, 2008, 2009 by Jakob Petsovits ("jpetso", http://drupal.org/user/56020)
13 */
14
15 //TODO: define if we want to do the load each time, per use, or all-in-one like views.inc
16 require_once drupal_get_path('module', 'versioncontrol') .'/includes/VersioncontrolAccount.php';
17 require_once drupal_get_path('module', 'versioncontrol') .'/includes/VersioncontrolRepository.php';
18 require_once drupal_get_path('module', 'versioncontrol') .'/includes/VersioncontrolOperation.php';
19
20 /**
21 * Form callback for "admin/project/versioncontrol-settings/general":
22 * Provide a form for settings of Version Control API itself.
23 */
24 function versioncontrol_admin_settings(&$form_state) {
25 $form = array();
26 $presets = _versioncontrol_get_string_presets();
27
28 $form['#id'] = 'versioncontrol-settings-form';
29
30 $form['versioncontrol_email_address'] = array(
31 '#type' => 'textfield',
32 '#title' => t('E-mail address'),
33 '#default_value' => variable_get('versioncontrol_email_address', 'versioncontrol@example.com'),
34 '#description' => t('The e-mail address of the VCS administrator.'),
35 '#weight' => -10,
36 );
37
38 $form['versioncontrol_register_form'] = array(
39 '#type' => 'fieldset',
40 '#title' => t('Account registration form strings'),
41 '#description' => t('The following messages are shown on the !repository-selection-page that leads to the account registration form.', array('!repository-selection-page' => l(t('repository selection page'), 'versioncontrol/register/demo'))),
42 '#collapsible' => TRUE,
43 '#collapsed' => TRUE,
44 '#weight' => 0,
45 );
46 $form['versioncontrol_register_form']['versioncontrol_registration_message_unauthorized'] = array(
47 '#type' => 'textarea',
48 '#title' => t('Message to unauthorized users'),
49 '#description' => t('Message to show to anonymous users and to users without the \'use version control systems\' permission.'),
50 '#default_value' => variable_get(
51 'versioncontrol_registration_message_unauthorized',
52 $presets['versioncontrol_registration_message_unauthorized']
53 ),
54 );
55 $form['versioncontrol_register_form']['versioncontrol_registration_message_authorized'] = array(
56 '#type' => 'textarea',
57 '#title' => t('Message to registering users'),
58 '#description' => t('Message to show to users who are in the process of selecting a repository in order to create a VCS account. If there\'s only one repository on this site, users will never get to see this message.'),
59 '#default_value' => variable_get(
60 'versioncontrol_registration_message_authorized',
61 $presets['versioncontrol_registration_message_authorized']
62 ),
63 );
64
65 $form['submit'] = array(
66 '#type' => 'submit',
67 '#value' => t('Save configuration'),
68 '#weight' => 100,
69 );
70
71 return $form;
72 }
73
74 /**
75 * Submit handler for the authorization settings form.
76 * system_settings_form() is left out because it's nasty for modules that
77 * hook into this form and don't want to use automatic variable saving.
78 */
79 function versioncontrol_admin_settings_submit($form, &$form_state) {
80 $value_names = array(
81 'versioncontrol_email_address',
82 'versioncontrol_registration_message_unauthorized',
83 'versioncontrol_registration_message_authorized',
84 );
85 foreach ($value_names as $name) {
86 variable_set($name, $form_state['values'][$name]);
87 }
88 }
89
90
91 /**
92 * Form callback for "admin/project/versioncontrol-accounts":
93 * A list of accounts with filtering possibilities.
94 */
95 function versioncontrol_admin_account_list_form(&$form_state) {
96 // Retrieve a list of all repositories with registered VCS accounts.
97 $result = db_query('SELECT DISTINCT repo_id FROM {versioncontrol_accounts}');
98 $repo_ids = array();
99
100 while ($repo_id = db_result($result)) {
101 $repo_ids[] = $repo_id;
102 }
103 $repositories = VersioncontrolRepositoryCache::getInstance()->getRepositories(array('repo_ids' => $repo_ids));
104
105 $filter_form = versioncontrol_admin_account_list_filter_form($form_state, $repositories);
106 $filter_form['#id'] = 'versioncontrol-account-filter-form';
107 drupal_alter('form', $filter_form, $form_state, 'versioncontrol_admin_account_list_filter_form');
108
109 $form = array();
110 $form['filters'] = array_merge($filter_form, array(
111 '#type' => 'fieldset',
112 '#title' => t('Filter'),
113 '#collapsible' => TRUE,
114 '#collapsed' => FALSE,
115 '#prefix' => '<div class="container-inline">',
116 '#suffix' => '</div>',
117 ));
118
119 // Filter the list of accounts.
120 $accounts = VersioncontrolAccountCache::getInstance()->getAccounts(array(), TRUE);
121
122 foreach (module_implements('versioncontrol_filter_accounts') as $module) {
123 $function = $module .'_versioncontrol_filter_accounts';
124 $function($accounts);
125 }
126 $accounts = versioncontrol_admin_get_paged_accounts($accounts);
127 $users = array();
128
129 // Retrieve more info for the accounts that survived filtering.
130 foreach ($accounts as $uid => $usernames_by_repository) {
131 $user = user_load($uid);
132 if (!$user) {
133 unset($accounts[$uid]);
134 continue;
135 }
136 $users[$uid] = $user;
137 }
138 if (empty($users)) {
139 $form['empty'] = array(
140 '#type' => 'markup',
141 '#value' => '<p>'. t('No accounts could be found with the current filter settings.') .'</p>',
142 );
143 return $form;
144 }
145
146 // Retrieve total, first and last commits of each user.
147 $statistics = VersioncontrolOperationCache::getInstance()->getStatistics(array(
148 'types' => array(VERSIONCONTROL_OPERATION_COMMIT),
149 'uids' => array_keys($users),
150 ), array(
151 'group_by' => array('uid'),
152 ));
153
154 $total_operations = array();
155 $first_operation_dates = array();
156 $last_operation_dates = array();
157
158 foreach ($statistics as $user_stats) {
159 $total_operations[$user_stats->uid] = $user_stats->total_operations;
160 $first_operation_dates[$user_stats->uid] = t('!time ago', array(
161 '!time' => format_interval(time() - $user_stats->first_operation_date, 1))
162 );
163 $last_operation_dates[$user_stats->uid] = t('!time ago', array(
164 '!time' => format_interval(time() - $user_stats->last_operation_date, 1))
165 );
166 if (module_exists('commitlog')) {
167 $total_operations[$user_stats->uid] =
168 l($total_operations[$user_stats->uid], 'user/'. $user_stats->uid .'/track/code');
169 }
170 }
171
172 // Construct the user account table.
173 $header = array(t('User'), t('Accounts'), t('Commits'), t('First commit'), t('Last commit'));
174 $rows_by_uid = array();
175 foreach ($accounts as $uid => $usernames_by_repository) {
176 $user = $users[$uid];
177
178 // Present a list of all VCS usernames and the repository where they're in.
179 $repo_usernames = array();
180 foreach ($usernames_by_repository as $repo_id => $account) {
181 if (!isset($repositories[$repo_id])) { // happens if the backend isn't loaded
182 continue;
183 }
184 $formatted_username = theme('versioncontrol_account_username',
185 $uid, $account->vcs_username, $repositories[$repo_id],
186 array('prefer_drupal_username' => FALSE)
187 );
188 $repo_name = module_exists('commitlog')
189 ? theme('commitlog_repository', $repositories[$repo_id])
190 : check_plain($repositories[$repo_id]['name']);
191
192 $repo_usernames[] = t('!username in !repository (!edit)', array(
193 '!username' => $formatted_username,
194 '!repository' => $repo_name,
195 '!edit' => l(t('edit'),
196 'user/'. $uid .'/edit/versioncontrol/'. $repo_id .'/'. drupal_urlencode($account->vcs_username)
197 ),
198 ));
199 }
200 $vcs_usernames = empty($repo_usernames)
201 ? t('VCS backend is currently disabled')
202 : theme('item_list', $repo_usernames);
203
204 $rows_by_uid[$uid] = array(
205 theme('username', $user), $vcs_usernames,
206 isset($total_operations[$uid]) ? $total_operations[$uid] : 0,
207 isset($first_operation_dates[$uid]) ? $first_operation_dates[$uid] : t('n/a'),
208 isset($last_operation_dates[$uid]) ? $last_operation_dates[$uid] : t('n/a'),
209 );
210 }
211
212 // Provide a possibility for backends and other modules to modify the list.
213 foreach (module_implements('versioncontrol_alter_account_list') as $module) {
214 $function = $module .'_versioncontrol_alter_account_list';
215 $function($accounts, $repositories, $header, $rows_by_uid);
216 }
217 // We don't want the uids in the final $rows array.
218 $rows = array_values($rows_by_uid);
219
220 // The finished user account list with account filter.
221 $form['accounts'] = array(
222 '#type' => 'markup',
223 '#value' => theme('table', $header, $rows),
224 );
225
226 if ($pager = theme('pager', NULL, variable_get('versioncontrol_admin_account_pager', 20), 0)) {
227 $form['pager'] = array(
228 '#type' => 'markup',
229 '#value' => $pager,
230 );
231 }
232 return $form;
233 }
234
235 /**
236 * Return a subset of the given accounts (10 Drupal users by default, and all
237 * of their VCS usernames). Paging is also used by emulating pager_query().
238 */
239 function versioncontrol_admin_get_paged_accounts($accounts, $element = 0) {
240 global $pager_page_array, $pager_total, $pager_total_items;
241 $page = isset($_GET['page']) ? $_GET['page'] : '';
242 $pager_page_array = explode(',', $page);
243 $page = empty($pager_page_array[$element]) ? 0 : $pager_page_array[$element];
244
245 $limit = variable_get('versioncontrol_admin_account_pager', 20);
246 $i = 0;
247 $paged_accounts = array();
248
249 foreach ($accounts as $uid => $usernames_by_repository) {
250 if ($i >= ($page * $limit) && $i < (($page+1) * $limit)) {
251 $paged_accounts[$uid] = $usernames_by_repository;
252 }
253 ++$i;
254 }
255
256 // Emulate pager_query() in order to get a proper theme('pager').
257 $pager_total_items[$element] = count($accounts);
258 $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
259 $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
260
261 return $paged_accounts;
262 }
263
264 /**
265 * A form for filtering accounts which is embedded in the account list page.
266 */
267 function versioncontrol_admin_account_list_filter_form(&$form_state, $repositories) {
268 $form = array();
269 $repository_names = array(0 => t('All'));
270
271 foreach ($repositories as $repo_id => $repository) {
272 $repository_names[$repo_id] = check_plain($repository['name']);
273 }
274 if (!isset($_SESSION['versioncontrol_filter_repo_id'])) {
275 $_SESSION['versioncontrol_filter_repo_id'] = 0;
276 }
277
278 $form['#id'] = 'versioncontrol-account-filter-form';
279
280 $form['repo_id'] = array(
281 '#type' => 'select',
282 '#title' => t('Repository'),
283 '#options' => $repository_names,
284 '#default_value' => $_SESSION['versioncontrol_filter_repo_id'],
285 '#weight' => 10,
286 );
287 $form['submit'] = array(
288 '#type' => 'submit',
289 '#value' => t('Filter'),
290 '#submit' => array('versioncontrol_admin_account_list_filter_form_submit'),
291 '#weight' => 100,
292 );
293 return $form;
294 }
295
296 /**
297 * Submit handler for the account filter form.
298 */
299 function versioncontrol_admin_account_list_filter_form_submit($form, &$form_state) {
300 $_SESSION['versioncontrol_filter_repo_id'] = $form_state['values']['repo_id'];
301 }
302
303 /**
304 * Implementation of hook_versioncontrol_filter_accounts():
305 * Unset filtered accounts before they are even attempted to be displayed
306 * on the account list.
307 *
308 * @param $accounts
309 * The accounts that would normally be displayed, in the same format as the
310 * return value of VersioncontrolAccountCache::getInstance()->getAccounts(). Entries in this list
311 * may be unset by this filter function.
312 */
313 function versioncontrol_versioncontrol_filter_accounts(&$accounts) {
314 if (empty($accounts)) {
315 return;
316 }
317 if (!isset($_SESSION['versioncontrol_filter_repo_id'])) {
318 $_SESSION['versioncontrol_filter_repo_id'] = 0;
319 }
320 $filter_repo_id = $_SESSION['versioncontrol_filter_repo_id'];
321
322 if ($filter_repo_id == 0) {
323 return; // Don't change the list if "All" repositories are selected.
324 }
325
326 foreach ($accounts as $uid => $usernames_by_repository) {
327 foreach ($usernames_by_repository as $repo_id => $username) {
328 if ($repo_id != $filter_repo_id) {
329 unset($accounts[$uid][$repo_id]);
330 if (empty($accounts[$uid])) {
331 unset($accounts[$uid]);
332 }
333 }
334 }
335 }
336 }
337
338
339 /**
340 * Form callback for "admin/project/versioncontrol-accounts/import":
341 * A repository select box and a text area for VCS specific account data
342 * that will be imported into the selected repository.
343 */
344 function versioncontrol_admin_account_import_form(&$form_state) {
345 $form = array();
346 $selected_backends = array();
347
348 foreach (versioncontrol_get_backends() as $vcs => $backend) {
349 $repo = new $backend->classes['repo'](0, array(), FALSE);
350 if ($repo instanceof VersioncontrolRepositoryImportExport) {
351 $selected_backends[] = $vcs;
352 }
353 }
354 $repositories = VersioncontrolRepositoryCache::getInstance()->getRepositories(array('vcs' => $selected_backends));
355
356 if (empty($repositories)) {
357 $form['no_repositories'] = array(
358 '#type' => 'markup',
359 '#value' => t('There are no repositories for which the accounts can be imported.'),
360 '#prefix' => '<p>',
361 '#suffix' => '</p>',
362 );
363 return $form;
364 }
365
366 $repository_names = array();
367 foreach ($repositories as $repo_id => $repository) {
368 if (!isset($first_repo_id)) {
369 $first_repo_id = $repo_id;
370 }
371 $repository_names[$repo_id] = check_plain($repository['name']);
372 }
373
374 $form['repo_id'] = array(
375 '#type' => 'select',
376 '#title' => t('Import accounts into'),
377 '#options' => $repository_names,
378 '#default_value' => $first_repo_id,
379 );
380 $form['data'] = array(
381 '#type' => 'textarea',
382 '#title' => t('Account data'),
383 '#description' => t('The contents of the config file that contains the accounts that shall be imported. Existing accounts will be updated, new accounts will be created, and all others will be left untouched.'),
384 '#cols' => 70,
385 '#rows' => 24,
386 '#required' => TRUE,
387 );
388 $form['submit'] = array(
389 '#type' => 'submit',
390 '#value' => t('Import accounts'),
391 '#weight' => 100,
392 );
393 return $form;
394 }
395
396 /**
397 * Validation form callback for VCS account data.
398 *
399 * Only actually validates that the repo backend is correctly configured.
400 */
401 function versioncontrol_admin_account_import_form_validate($form, &$form_state) {
402 if (is_null(VersioncontrolRepositoryCache::getInstance()->getRepository($form_state['values']['repo_id']))) {
403 form_error($form['data'], t('The selected repository could not be found.'));
404 }
405 }
406
407 /**
408 * Submit handler for the "select repository to import" form from above:
409 * Call the repo::importAccounts() with the given data.
410 */
411 function versioncontrol_admin_account_import_form_submit($form, &$form_state) {
412 $repository = VersioncontrolRepositoryCache::getInstance()->getRepository($form_state['values']['repo_id']);
413 $repository->importAccounts($form_state['values']['data']);
414 }
415
416
417 /**
418 * Callback for 'admin/project/versioncontrol-accounts/export/%versioncontrol_repository':
419 * Export a repository's authenticated accounts to the version control system's
420 * password file format.
421 */
422 function versioncontrol_admin_account_export_page($repository) {
423 if (!$repository instanceof VersioncontrolRepositoryImportExport) {
424 drupal_not_found();
425 return;
426 }
427
428 // The repository has already been selected, export the accounts.
429 $data = $repository->exportAccounts();
430
431 if (arg(5) == 'plaintext') {
432 drupal_set_header('Content-Type: text/plain; charset=utf-8');
433 print $data;
434 }
435 else {
436 return drupal_get_form(
437 'versioncontrol_admin_account_export_display_form', $repository, $data
438 );
439 }
440 }
441
442 /**
443 * Form callback for "admin/project/versioncontrol-accounts/export":
444 * Select the repository from which to extract data, and show the exported
445 * data on submitting the form.
446 */
447 function versioncontrol_admin_account_export_form(&$form_state) {
448 $form = array();
449 $selected_backends = array();
450
451 foreach (versioncontrol_get_backends() as $vcs => $backend) {
452 $repo = new $backend->classes['repo'](0, array(), FALSE);
453 if ($repo instanceof VersioncontrolRepositoryImportExport) {
454 $selected_backends[] = $vcs;
455 }
456 }
457 $repositories = VersioncontrolRepositoryCache::getInstance()->getRepositories(array('vcs' => $selected_backends));
458
459 if (empty($repositories)) {
460 $form['no_repositories'] = array(
461 '#type' => 'markup',
462 '#value' => t('There are no repositories for which the accounts can be exported.'),
463 '#prefix' => '<p>',
464 '#suffix' => '</p>',
465 );
466 return $form;
467 }
468
469 $repository_names = array();
470 foreach ($repositories as $repo_id => $repository) {
471 if (!isset($first_repo_id)) {
472 $first_repo_id = $repo_id;
473 }
474 $repository_names[$repo_id] = check_plain($repository['name']);
475 }
476
477 $form['repository'] = array(
478 '#type' => 'fieldset',
479 '#title' => t('Select repository'),
480 '#weight' => 10,
481 );
482 $form['repository']['repo_id'] = array(
483 '#type' => 'select',
484 '#title' => t('Export accounts from'),
485 '#options' => $repository_names,
486 '#default_value' => $first_repo_id,
487 );
488 $form['repository']['buttons'] = array();
489 $form['repository']['buttons']['export_to_form'] = array(
490 '#type' => 'submit',
491 '#value' => t('Export to page'),
492 );
493 $form['repository']['buttons']['export_to_plaintext'] = array(
494 '#type' => 'submit',
495 '#value' => t('Export to plaintext'),
496 );
497 return $form;
498 }
499
500 /**
501 * Submit handler for the "select repository to export" form from above:
502 * Redirect to the export form for the specific repository.
503 */
504 function versioncontrol_admin_account_export_form_submit($form, &$form_state) {
505 $form_state['redirect'] = 'admin/project/versioncontrol-accounts/export/'. $form_state['values']['repo_id'];
506
507 if ($form_state['values']['op'] == t('Export to plaintext')) {
508 $form_state['redirect'] .= '/plaintext';
509 }
510 }
511
512 /**
513 * Form callback for 'admin/project/versioncontrol-accounts/export/%versioncontrol_repository':
514 * Show the exported account data in a form as text area.
515 *
516 * @param $repository
517 * The repository for which accounts have been exported.
518 * @param $data
519 * The exported account data - the text that is supposed to be copied
520 * to the version control system's accounts file.
521 */
522 function versioncontrol_admin_account_export_display_form(&$form_state, $repository, $data) {
523 $form = array();
524 $form['#repo_id'] = $repository['repo_id'];
525
526 $form['data'] = array(
527 '#type' => 'textarea',
528 '#title' => t('@vcs account data', array('@vcs' => $repository->backend->name)),
529 '#description' => t('The exported account data. You can copy this to the config file that contains the @vcs accounts.', array('@vcs' => $backend['name'])),
530 '#default_value' => $data,
531 '#cols' => 70,
532 '#rows' => 30,
533 );
534 return $form;
535 }
536
537
538 /**
539 * Form callback for "admin/project/versioncontrol-repositories":
540 * A simple list of repositories, sorted by version control system.
541 */
542 function versioncontrol_admin_repository_list(&$form_state) {
543 $form = array();
544 $backends = versioncontrol_get_backends();
545 $repositories = VersioncontrolRepositoryCache::getInstance()->getRepositories();
546
547 if (empty($repositories)) {
548 $form['empty'] = array(
549 '#value' => '<p>'. t('No repositories are currently known. Please add one or more repositories in order to be able to use version control features on the site.') .'</p>'
550 );
551 return $form;
552 }
553
554 // The header may be modified separately for each VCS,
555 // so this is only a template and still missing the Actions column.
556 $header_template = array(t('Name'), t('Root'));
557
558 // Sort repositories by backend.
559 $repositories_by_backend = array();
560 foreach ($repositories as $repo_id => $repository) {
561 if (!isset($repositories_by_backend[$repository['vcs']])) {
562 $repositories_by_backend[$repository['vcs']] = array();
563 }
564 $repositories_by_backend[$repository['vcs']][$repo_id] = $repository;
565 }
566
567 // Construct the form elements for each VCS separately.
568 foreach ($repositories_by_backend as $vcs => $vcs_repositories) {
569 $header = $header_template;
570 $title = t('@vcs repositories', array('@vcs' => $backends[$vcs]->name));
571 $form[$vcs] = array(
572 '#value' => '<h4>'. $title .'</h4>',
573 );
574
575 // Add the standard items of each repository - name and root - to the rows.
576 $rows_by_repo_id = array();
577 foreach ($vcs_repositories as $repo_id => $repository) {
578 $rows_by_repo_id[$repo_id] = array(
579 'name' => check_plain($repository['name']),
580 'root' => check_plain($repository['root']),
581 );
582 }
583 // Provide a possibility for backends and other modules to modify the list.
584 // TODO replace with drupal_alter()
585 foreach (module_implements('versioncontrol_alter_repository_list') as $module) {
586 $function = $module .'_versioncontrol_alter_repository_list';
587 $function($vcs, $vcs_repositories, $header, $rows_by_repo_id);
588 }
589 // Add the Actions column as final column, after all the other ones
590 $header[] = array('data' => t('Actions'), 'colspan' => 2);
591 foreach ($rows_by_repo_id as $repo_id => $row) {
592 $links = array(
593 array(
594 'title' => t('Edit'),
595 'href' => 'admin/project/versioncontrol-repositories/edit/'. $repo_id,
596 ),
597 array(
598 'title' => t('Delete'),
599 'href' => 'admin/project/versioncontrol-repositories/delete/'. $repo_id,
600 ),
601 );
602 $rows_by_repo_id[$repo_id]['links'] = theme('links', $links);
603 }
604 // We don't want the repository ids in the final $rows array.
605 $rows = array_values($rows_by_repo_id);
606
607 // The finished table for the currently processed VCS.
608 $form[$vcs]['list'] = array(
609 '#value' => theme('table', $header, $rows) .'<br/>'
610 );
611 }
612 return $form;
613 }
614
615 /**
616 * Form callback for
617 * 'admin/project/versioncontrol-repositories/edit/%versioncontrol_repository'
618 * and "admin/project/versioncontrol-repositories/add-$vcs":
619 * Provide a form to edit or add a repository.
620 *
621 * @param $repository
622 * The repository that is to be edited, or VERSIONCONTROL_FORM_CREATE
623 * if the repository doesn't exist yet and should be added.
624 * @param $vcs
625 * If $repo_id is NULL, this should be the unique identification string
626 * of the backend for which the repository should be added.
627 * Otherwise, this value is ignored.
628 */
629 function versioncontrol_admin_repository_edit(&$form_state, $repository, $vcs = NULL) {
630 $backends = versioncontrol_get_backends();
631 $repository_exists = ($repository !== VERSIONCONTROL_FORM_CREATE);
632
633 if (!$repository_exists && !isset($backends[$vcs])) {
634 drupal_goto('admin/project/versioncontrol-repositories');
635 // drupal_goto() calls exit() so script execution ends right here
636 }
637
638 $form = array();
639 $form['#id'] = 'versioncontrol-repository-form';
640
641 if ($repository_exists) {
642 $form['#repository'] = $repository;
643 }
644 $form['#vcs'] = $repository_exists ? $repository['vcs'] : $vcs;
645 $form['#original_name'] = $repository_exists ? $repository['name'] : 0;
646
647 $form['repository_information'] = array(
648 '#type' => 'fieldset',
649 '#title' => t('Repository information'),
650 '#collapsible' => TRUE,
651 '#collapsed' => FALSE,
652 '#weight' => 0,
653 );
654 $form['repository_information']['repo_name'] = array(
655 '#type' => 'textfield',
656 '#title' => t('Repository name'),
657 '#description' => t('A label for the repository that will be used in all user visible messages.'),
658 '#default_value' => $repository_exists ? $repository['name'] : '',
659 '#required' => TRUE,
660 '#weight' => 0,
661 '#size' => 40,
662 '#maxlength' => 255,
663 );
664 $form['repository_information']['root'] = array(
665 '#type' => 'textfield',
666 '#title' => t('Repository root'),
667 '#description' => t('The location of the repository\'s root directory.'),
668 '#default_value' => $repository_exists ? $repository['root'] : '',
669 '#weight' => 5,
670 '#size' => 60,
671 '#maxlength' => 255,
672 );
673
674 if ($repository_exists && isset($repository->data['versioncontrol']['registration_message'])) {
675 $current_registration_message = $repository->data['versioncontrol']['registration_message'];
676 }
677 else {
678 $presets = _versioncontrol_get_string_presets();
679 $current_registration_message = $presets['versioncontrol_registration_message_repository'];
680 }
681
682 $form['account_registration'] = array(
683 '#type' => 'fieldset',
684 '#title' => t('Account registration'),
685 '#collapsible' => TRUE,
686 '#collapsed' => TRUE,
687 '#weight' => 1,
688 );
689 $form['account_registration']['authorization_method'] = array(
690 '#type' => 'select',
691 '#title' => t('Authorization method'),
692 '#description' => t('Determines the way that users can register an account for this repository.'),
693 '#options' => versioncontrol_get_authorization_methods(),
694 '#default_value' => $repository_exists
695 ? $repository['authorization_method']
696 : _versioncontrol_get_fallback_authorization_method(),
697 '#weight' => 0,
698 );
699 $form['account_registration']['repository_messages'] = array(
700 '#type' => 'fieldset',
701 '#title' => t('Account registration form strings'),
702 '#description' => t('The following message is shown on the !account-registration-page for this repository.', array('!account-registration-page' => l(t('account registration page'), 'versioncontrol/register/demo/'. $repository['repo_id']))),
703 '#collapsible' => TRUE,
704 '#collapsed' => FALSE,
705 '#weight' => 2,
706 );
707 $form['account_registration']['repository_messages']['registration_message'] = array(
708 '#type' => 'textarea',
709 '#title' => t('Message to registering users'),
710 '#description' => t('Message to show to users that are in the process of creating an account in this repository.'),
711 '#default_value' => $current_registration_message,
712 );
713
714 $backend_capabilities = $backends[$form['#vcs']]['capabilities'];
715
716 if (in_array(VERSIONCONTROL_CAPABILITY_COMMIT_RESTRICTIONS, $backend_capabilities)) {
717 $form['commit_restrictions'] = array(
718 '#type' => 'fieldset',
719 '#title' => t('Commit restrictions'),
720 '#collapsible' => TRUE,
721 '#collapsed' => TRUE,
722 '#weight' => 6,
723 );
724 $form['commit_restrictions']['allow_unauthorized_access'] = array(
725 '#type' => 'checkbox',
726 '#title' => t('Allow repository access to unknown or unauthorized committers'),
727 '#description' => 'If this is enabled, users can commit to this repository even if no association of the VCS account to the Drupal user is known (assuming that no other commit restrictions apply). If it is disabled, only known and authorized users may commit. This setting only works if the appropriate pre-commit/pre-branch/pre-tag hook script is set up for this repository.',
728 '#default_value' => $repository_exists
729 ? $repository->data['versioncontrol']['allow_unauthorized_access']
730 : FALSE,
731 '#weight' => 0,
732 );
733 }
734
735 if ($repository_exists) {
736 $repo_urls = $repository->getUrlHandler()->urls;
737 }
738
739 $form['repository_urls'] = array(
740 '#type' => 'fieldset',
741 '#title' => t('Repository browser URLs'),
742 '#description' => t('These URLs will be used to add links to item and commit displays such as the commit log.'),
743 '#collapsible' => TRUE,
744 '#collapsed' => TRUE,
745 '#weight' => 10,
746 );
747 $form['repository_urls']['commit_view'] = array(
748 '#type' => 'textfield',
749 '#title' => t('Commit view URL'),
750 '#default_value' => isset($repo_urls) ? $repo_urls['commit_view'] : '',
751 '#size' => 40,
752 '#maxlength' => 255,
753 '#description' => t('Enter URL to the commit view web site. Use the %revision variable in the right place to represent the global revision identifier of the commit.'),
754 );
755 $form['repository_urls']['file_log_view'] = array(
756 '#type' => 'textfield',
757 '#title' => t('File log URL'),
758 '#default_value' => isset($repo_urls) ? $repo_urls['file_log_view'] : '',
759 '#size' => 40,
760 '#maxlength' => 255,
761 '#description' => t('Enter URL to the log view of a file in the repository. Use the %path, %revision and %branch variables in the right place to represent the path, revision and branch of the originating version of the file for the log.'),
762 );
763 $form['repository_urls']['file_view'] = array(
764 '#type' => 'textfield',
765 '#title' => t('File content URL'),
766 '#default_value' => isset($repo_urls) ? $repo_urls['file_view'] : '',
767 '#size' => 40,
768 '#maxlength' => 255,
769 '#description' => t('Enter URL to the content view of a file in the repository. Use the %path, %revision and %branch variables in the right place to represent the path, revision and branch of the file.'),
770 );
771 $form['repository_urls']['directory_view'] = array(
772 '#type' => 'textfield',
773 '#title' => t('Directory listing URL'),
774 '#default_value' => isset($repo_urls) ? $repo_urls['directory_view'] : '',
775 '#size' => 40,
776 '#maxlength' => 255,
777 '#description' => t('Enter URL to the file listing of a directory in the repository. Use the %path, %revision and %branch variables in the right place to represent the path, revision and branch of the file.'),
778 );
779 $form['repository_urls']['diff'] = array(
780 '#type' => 'textfield',
781 '#title' => t('Diff URL'),
782 '#default_value' => isset($repo_urls) ? $repo_urls['diff'] : '',
783 '#size' => 40,
784 '#maxlength' => 255,
785 '#description' => t('Enter URL to the diff web site. Use the %path, %new-revision, %old-revision and %branch variables in the right place to represent the file path, old revision and new revision. If the tool supports diffs between wholly different files, you can also use %old-path for the path of the old version of the file.'),
786 );
787 $form['repository_urls']['tracker'] = array('#type' => 'textfield',
788 '#title' => t('Issue tracker URL'),
789 '#default_value' => isset($repo_urls) ? $repo_urls['tracker'] : '',
790 '#size' => 40,
791 '#maxlength' => 255,
792 '#description' => t('Enter URL to link to issues in log messages. Use the %d variable in the right place to represent the issue/case/bug id.'),
793 );
794
795 $form['submit'] = array(
796 '#type' => 'submit',
797 '#value' => t('Save repository'),
798 '#weight' => 100,
799 );
800
801 return $form;
802 }
803
804 /**
805 * Validate the add/edit repository form before it is submitted.
806 */
807 function versioncontrol_admin_repository_edit_validate($form, &$form_state) {
808 if (!empty($form['#repository']) && $form_state['values']['repo_name'] == $form['#original_name']) {
809 return;
810 }
811 $same_repositories = VersioncontrolRepositoryCache::getInstance()->getRepositories(array(
812 'names' => array($form_state['values']['repo_name']),
813 ));
814 if (!empty($same_repositories)) {
815 form_error($form['repository_information']['repo_name'],
816 t('The repository name %reponame is already in use, please assign a different one.',
817 array('%reponame' => $form_state['values']['repo_name']))
818 );
819 }
820 }
821
822 /**
823 * Add or update the repository when the add/edit form is submitted.
824 */
825 function versioncontrol_admin_repository_edit_submit($form, &$form_state) {
826 $repository_urls = array();
827 $repository_url_keys = array_keys(VersioncontrolRepositoryUrlHandler::getEmpty());
828 foreach ($repository_url_keys as $key) {
829 $repository_urls[$key] = $form_state['values'][$key];
830 }
831
832 // Take the existing repository and modify it with the given form values,
833 // or construct a new one altogether.
834 if (isset($form['#repository'])) {
835 $repository = $form['#repository'];
836 $repository->name = $form_state['values']['repo_name'];
837 $repository->root = $form_state['values']['root'];
838 $repository->authorization_method = $form_state['values']['authorization_method'];
839 }
840 else {
841 $backends = versioncontrol_get_backends();
842 $repository = array(
843 'name' => $form_state['values']['repo_name'],
844 'vcs' => $form['#vcs'],
845 'root' => $form_state['values']['root'],
846 'authorization_method' => $form_state['values']['authorization_method'],
847 'backend' => $backends[$form['#vcs']],
848 );
849 $repository = new $backends[$form['#vcs']]->classes['repo'](null, $repository, FALSE);
850 }
851
852 // We also provide a 'data' array where other modules can put their own
853 // per-repository data that doesn't need to be stored in a separate db column.
854 $repository->data['versioncontrol'] = array(
855 'registration_message' => $form_state['values']['registration_message'],
856 'url_handler' => new VersioncontrolRepositoryUrlHandler($repository, $repository_urls),
857 );
858
859 if (isset($form_state['values']['allow_unauthorized_access'])) {
860 $repository->data['versioncontrol']['allow_unauthorized_access'] =
861 (bool) $form_state['values']['allow_unauthorized_access'];
862 }
863
864 // Let other modules alter the repository array.
865 foreach (module_implements('versioncontrol_repository_submit') as $module) {
866 $function = $module .'_versioncontrol_repository_submit';
867 $function($repository, $form, $form_state);
868 }
869
870 if (!is_null($repository->repo_id)) {
871 $repository->update();
872 drupal_set_message(t('The %repository repository has been updated.', array(
873 '%repository' => $repository->name,
874 )));
875 }
876 else {
877 $repository = $repository->insert();
878 drupal_set_message(t('The %repository repository has been added.', array(
879 '%repository' => $repository->name
880 )));
881 }
882
883 $form_state['redirect'] = 'admin/project/versioncontrol-repositories';
884 }
885
886
887 /**
888 * Form callback for 'admin/project/versioncontrol-repositories/delete/%versioncontrol_repository':
889 * Provide a form to confirm deletion of a repository.
890 */
891 function versioncontrol_admin_repository_delete_confirm(&$form_state, $repository) {
892 $form = array();
893 $form['#repo_id'] = $repository['repo_id'];
894
895 $form = confirm_form($form,
896 t('Are you sure you want to delete %name?', array('%name' => $repository['name'])),
897 !empty($_GET['destination']) ? $_GET['destination'] : 'admin/project/versioncontrol-repositories',
898 t('Mind that by deleting the repository, all associated data such as commits and account associations will be deleted as well.'),
899 t('Delete'), t('Cancel')
900 );
901 return $form;
902 }
903
904 /**
905 * Delete the repository when the confirmation form is submitted.
906 */
907 function versioncontrol_admin_repository_delete_confirm_submit($form, &$form_state) {
908 $repository = VersioncontrolRepositoryCache::getInstance()->getRepository($form['#repo_id']);
909 $repository->delete();
910 drupal_set_message(t('The %repository repository has been deleted.', array(
911 '%repository' => $repository['name'],
912 )));
913 $form_state['redirect'] = 'admin/project/versioncontrol-repositories';
914 }

  ViewVC Help
Powered by ViewVC 1.1.2