Prevent adding empty vcs condition on repository controller.
[project/versioncontrol.git] / commitlog / commitlog.module
1 <?php
2 /**
3 * @file
4 * Commit Log - Display a history of commits, branches and tags,
5 * optionally filtered by a set of constraint arguments.
6 *
7 * Copyright 2006, 2007 Derek Wright ("dww" , http://drupal.org/user/46549)
8 * Copyright 2007, 2008, 2009 by Jakob Petsovits ("jpetso", http://drupal.org/user/56020)
9 */
10
11 /**
12 * Implements hook_menu().
13 */
14 function commitlog_menu() {
15 $items = array();
16
17 $view_access = 'access commit messages';
18
19 $items['commitlog'] = array(
20 'title' => 'Commit messages',
21 'page callback' => 'commitlog_operations_page',
22 'access arguments' => array($view_access),
23 'type' => MENU_SUGGESTED_ITEM,
24 );
25 $items['commitlog/feed'] = array(
26 'title' => 'Commit messages',
27 'page callback' => 'commitlog_operations_page',
28 'page arguments' => array('global', 'feed'),
29 'access arguments' => array($view_access),
30 'type' => MENU_SUGGESTED_ITEM,
31 );
32 $items['commitlog/repository/%versioncontrol_repository'] = array(
33 'title' => 'Commit messages',
34 'page callback' => 'commitlog_operations_page',
35 'page arguments' => array('repository', 'page', 2),
36 'access arguments' => array($view_access),
37 'type' => MENU_SUGGESTED_ITEM,
38 );
39 $items['commitlog/repository/%versioncontrol_repository/feed'] = array(
40 'title' => 'Commit messages',
41 'page callback' => 'commitlog_operations_page',
42 'page arguments' => array('repository', 'feed', 2),
43 'access arguments' => array($view_access),
44 'type' => MENU_SUGGESTED_ITEM,
45 );
46 $items['commitlog/author/%user'] = array(
47 'title' => 'Commit messages',
48 'page callback' => 'commitlog_operations_page',
49 'page arguments' => array('author', 'page', 2),
50 'access callback' => 'commitlog_user_view_access',
51 'access arguments' => array(2),
52 'type' => MENU_SUGGESTED_ITEM,
53 );
54 $items['commitlog/author/%user/feed'] = array(
55 'title' => 'Commit messages',
56 'page callback' => 'commitlog_operations_page',
57 'page arguments' => array('author', 'feed', 2),
58 'access callback' => 'commitlog_user_view_access',
59 'access arguments' => array(2),
60 'type' => MENU_SUGGESTED_ITEM,
61 );
62 $items['commitlog/commit/%versioncontrol_repository/%'] = array(
63 'title' => 'Commit',
64 'page callback' => 'commitlog_operations_page',
65 'page arguments' => array('commit', 'page', 2, 3),
66 'access arguments' => array($view_access),
67 'type' => MENU_SUGGESTED_ITEM,
68 );
69 $items['user/%user/track/code'] = array(
70 'title' => 'Commits',
71 'page callback' => 'commitlog_operations_page',
72 'page arguments' => array('user', 'page', 1),
73 'access callback' => 'commitlog_user_view_access',
74 'access arguments' => array(1),
75 'type' => MENU_LOCAL_TASK,
76 'weight' => 2,
77 );
78 $items['user/%user/track/code/feed'] = array(
79 'title' => 'Commits',
80 'page callback' => 'commitlog_operations_page',
81 'page arguments' => array('user', 'feed', 1),
82 'access callback' => 'commitlog_user_view_access',
83 'access arguments' => array(1),
84 'type' => MENU_LOCAL_TASK,
85 'weight' => 2,
86 );
87 return $items;
88 }
89
90 /**
91 * Implements hook_permission().
92 */
93 function commitlog_permission() {
94 return array(
95 'access commit messages' => array(
96 'title' => t('Access commit messages'),
97 'description' => t('See commits information about the version control repostories.'),
98 ),
99 );
100 }
101
102 /**
103 * Menu access callback for menu items using user object.
104 *
105 * Pass through user_view_access() to follow standard drupal access.
106 */
107 function commitlog_user_view_access($account) {
108 return user_view_access($account) && user_access('access commit messages');
109 }
110
111 /**
112 * Implements hook_views_api().
113 *
114 * @return array
115 */
116 function commitlog_views_api() {
117 return array(
118 'api' => 3,
119 'path' => drupal_get_path('module', 'commitlog'). '/includes/views',
120 );
121 }
122
123 /**
124 * Implements ctools hook_ctools_plugin_directory().
125 */
126 function commitlog_ctools_plugin_directory($module, $plugin) {
127 if ($module == 'versioncontrol') {
128 return "includes/plugins/$plugin";
129 }
130 }
131
132 /**
133 * Implements hook_ctools_plugin_type().
134 */
135 function commitlog_ctools_plugin_type() {
136 return array(
137 'views_sets' => array(
138 'cache' => TRUE,
139 'use hooks' => TRUE,
140 'classes' => array('handler'),
141 ),
142 );
143 }
144
145 /**
146 * Page callback for the 'commitlog' family of menu paths.
147 *
148 * @param $view_type
149 * What type of commit log listing we're creating. Supported types are 'author', 'commit', 'global', 'repository' and 'user'.
150 * @param $display_type
151 * What type of display output we are creating. Supported types are 'page' and 'feed'.
152 * @param $argument
153 * The argument with which to filter the view. This is used as the first argument in the view in question.
154 * @param $argument2
155 * Another argument to filter the view. This is used as the second argument in the view in question.
156 */
157 function commitlog_operations_page($view_type = NULL, $display_type='page', $argument = NULL, $argument2 = NULL) {
158 drupal_add_css(drupal_get_path('module', 'commitlog') . '/commitlog.css', 'module');
159 $view_name = '';
160 if (($view_type == 'author' || $view_type == 'user') && !is_null($argument)) {
161 $account = $argument;
162 $set = versioncontrol_get_views_set('user_commit_view');
163 $view_name = $set->getViewName();
164 $feed_url = ($view_type == 'author') ? "commitlog/author/{$account->uid}/feed" : "user/{$account->uid}/track/code/feed";
165
166 if ($display_type == 'feed') {
167 return versioncontrol_render_commitlog_view($view_name, 'feed', array($account->uid), $feed_url);
168 }
169 return versioncontrol_render_commitlog_view($view_name, 'default', array($account->uid), '', $feed_url);
170 }
171 if ($view_type == 'repository' && !is_null($argument)) {
172 $repository = $argument;
173 $set = versioncontrol_get_views_set('repository_commit_view');
174 $view_name = $set->getViewNameByEntity($repository);
175 $feed_url = "commitlog/repository/{$repository->repo_id}/feed";
176 if ($display_type == 'feed') {
177 return versioncontrol_render_commitlog_view($view_name, 'feed', array($repository->repo_id), $feed_url);
178 }
179 return versioncontrol_render_commitlog_view($view_name, 'default', array($repository->repo_id), '', $feed_url);
180 }
181 if ($view_type == 'commit' && !is_null($argument) && !is_null($argument2)) {
182 $repository = $argument;
183 $revision = $argument2;
184 $set = versioncontrol_get_views_set('individual_commit_view');
185 $view_name = $set->getViewName();
186 return versioncontrol_render_commitlog_view($view_name, 'default', array($repository->repo_id, $revision));
187 }
188 // Global by default if no view type provided.
189 if (is_null($view_type) || $view_type == 'global') {
190 $set = versioncontrol_get_views_set('global_commit_view');
191 $view_name = $set->getViewName();
192 $feed_url = 'commitlog/feed';
193
194 if ($display_type == 'feed') {
195 return versioncontrol_render_commitlog_view($view_name, 'feed', array(), $feed_url);
196 }
197 return versioncontrol_render_commitlog_view($view_name, 'default', array(), '', $feed_url);
198 }
199
200 // Nothing catched the current arguments, so react acordingly.
201 return drupal_not_found();
202 }
203
204 /**
205 * A convinience function to render a commitlog view.
206 * Basic logic here borrowed from views_embed_view().
207 *
208 * @param $view_name
209 * Name of the view to render.
210 * @param $display_type
211 * What type of display output we are creating. Supported types are 'default' and 'feed'.
212 * @param $arguments
213 * The arguments needed for render the view.
214 * @param $path
215 * The path to overwrite the view with.
216 * @param $feed_url
217 * The url of the feed this display is linked. Only valid for $display_type = 'default'.
218 * @return
219 * On $display_type == 'default', the content to show.
220 * On $display_type == 'feed', NULL, but handle RSS printning by hand.
221 */
222 function versioncontrol_render_commitlog_view($view_name, $display_type, $arguments = array(), $path = '', $feed_url = '') {
223 $view = views_get_view($view_name);
224 if (!$view || !$view->access($display_type)) {
225 return drupal_not_found();
226 }
227
228 if (!empty($path)) {
229 $view->override_path = $path;
230 if ($display_type == 'feed') {
231 // Ugh, this line is needed, as override_path seems to not be enough
232 $view->display['feed']->display_options['path'] = $path;
233 }
234 }
235 $output = $view->preview($display_type, $arguments);
236 if ($display_type == 'feed') {
237 drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
238 print $output;
239 return;
240 }
241 else { // default display type
242 if (!empty($feed_url)) {
243 $feed_url = url($feed_url, array('absolute' => TRUE));
244 drupal_add_feed($feed_url);
245 }
246 }
247 // default
248 $title = $view->get_title();
249 if (!empty($title)) {
250 drupal_set_title($title);
251 }
252
253 return $output;
254 }