fgcf.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Non-features part of FGCF
  6. */
  7. include_once('fgcf.features.inc');
  8. /**
  9. * Implementation of hook_menu().
  10. */
  11. function fgcf_menu() {
  12. $read_access = array('access course catalog');
  13. $items = array();
  14. $items['fgcf'] = array(
  15. 'title' => 'Catalogue Formations',
  16. 'page callback' => 'fgcf_page_home',
  17. 'access arguments' => $read_access,
  18. 'menu_name' => 'primary-links',
  19. );
  20. $items['fgcf/format'] = array(
  21. 'type' => MENU_CALLBACK,
  22. 'title' => 'Formations par format',
  23. 'page callback' => 'fgcf_page_format',
  24. 'access arguments' => $read_access,
  25. );
  26. $items['fgcf/organisme'] = array(
  27. 'type' => MENU_CALLBACK,
  28. 'title' => 'Formations par organisme',
  29. 'page callback' => 'fgcf_page_organisme',
  30. 'access arguments' => $read_access,
  31. );
  32. $items['fgcf/thematique'] = array(
  33. 'type' => MENU_CALLBACK,
  34. 'title' => 'Formations par thématique',
  35. 'page callback' => 'fgcf_page_thematique',
  36. 'access arguments' => $read_access,
  37. );
  38. // Debug page - no access on production.
  39. // $items['fgcf/odt'] = array(
  40. // 'title' => 'ODT Test',
  41. // 'page callback' => 'fgcf_page_odt',
  42. // 'access callback' => $read_access,
  43. // );
  44. return $items;
  45. }
  46. /**
  47. * Implementation of hook_init().
  48. */
  49. function fgcf_init() {
  50. if (arg(0) == 'fgcf') {
  51. drupal_add_css(drupal_get_path('module', 'fgcf') .'/theme/css/fgcf.css');
  52. }
  53. }
  54. /**
  55. * Implementation of hook_perm().
  56. */
  57. function fgcf_perm() {
  58. return array('access course catalog');
  59. }
  60. /**
  61. * Implementation of hook_theme().
  62. */
  63. function fgcf_theme($existing, $type, $theme, $path) {
  64. $ret = array(
  65. 'fgcf_home' => array(
  66. 'arguments' => array('filter_form' => array()),
  67. 'template' => 'fgcf-home',
  68. 'path' => $path .'/theme',
  69. ),
  70. 'fgcf_thematiques' => array(
  71. 'arguments' => array('terms' => array()),
  72. ),
  73. );
  74. return $ret;
  75. }
  76. /**
  77. * Implementation of hook_term_path().
  78. */
  79. function fgcf_term_path($term) {
  80. $vocabulary = taxonomy_vocabulary_load($term->vid);
  81. switch ($vocabulary->vid) {
  82. case _fgcf_get_vocabulary_by_name('FGCF Thématique'):
  83. return 'fgcf/thematique/'. $term->tid;
  84. break;
  85. case _fgcf_get_vocabulary_by_name('FGCF Format de cours'):
  86. return 'fgcf/format/'. $term->tid;
  87. break;
  88. default:
  89. // Terms in other vocabs should be set to display 'as text' (no link)
  90. break;
  91. }
  92. }
  93. /**
  94. * Implementation of hook_views_api().
  95. */
  96. function fgcf_views_api() {
  97. $ret = array(
  98. 'api' => '2',
  99. 'path' => drupal_get_path('module', 'fgcf') . '/views',
  100. );
  101. return $ret;
  102. }
  103. /**
  104. * Implementation of hook_nodeapi().
  105. */
  106. function fgcf_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  107. switch ($op) {
  108. case 'view':
  109. // Only operate on 'page' view.
  110. if ($a3 /* !teaser */ || !$a4 /* page */) {
  111. break;
  112. }
  113. drupal_add_css(drupal_get_path('module', 'fgcf') .'/theme/css/fgcf.css');
  114. switch ($node->type) {
  115. case 'fgcf_prestataire':
  116. $bc = drupal_get_breadcrumb();
  117. $bc[] = l(t('Catalogue formations'), 'fgcf');
  118. $bc[] = l(t('Organismes'), 'fgcf/organisme');
  119. drupal_set_breadcrumb($bc);
  120. break;
  121. case 'fgcf_fiche':
  122. $nat_config = variable_get('nat_config', NULL);
  123. $vid = isset($nat_config['types']['fgcf_thematique'])
  124. ? reset($nat_config['types']['fgcf_thematique'])
  125. : 0;
  126. unset($nat_config);
  127. $taxo2 = array();
  128. foreach ($node->taxonomy as $tid => $term) {
  129. if ($term->vid != $vid) {
  130. $taxo2[$tid] = $term;
  131. }
  132. else {
  133. $node->fgcf_scat = $term; // Single by configuration
  134. }
  135. }
  136. $node->taxonomy = $taxo2;
  137. unset($taxo2, $tid, $term);
  138. // Build cat/scat[/scat]* path
  139. $cats = array();
  140. $parents = taxonomy_get_parents($node->fgcf_scat->tid);
  141. foreach ($parents as $tid => $term) {
  142. $cats[] = $term;
  143. }
  144. $cats[] = $node->fgcf_scat;
  145. $node->content['fgcf_catpath'] = array(
  146. '#value' => theme('fgcf_thematiques', $cats),
  147. );
  148. // Add 'Subscribe' button.
  149. $node->content['fgcf_subscribe'] = array(
  150. '#value' => drupal_get_form('fgcf_subscribe_form', $node),
  151. '#weight' => -100,
  152. );
  153. $bc = drupal_get_breadcrumb();
  154. $bc[] = l(t('Catalogue formations'), 'fgcf');
  155. $bc[] = l(t('Thématiques'), 'fgcf/thematique');
  156. $bc[] = l($cats[0]->description, 'fgcf/thematique/'. $cats[0]->tid);
  157. $bc[] = l($cats[1]->description, 'fgcf/thematique/'. $cats[1]->tid);
  158. drupal_set_breadcrumb($bc);
  159. unset($cats, $parents, $tid, $term);
  160. break;
  161. }
  162. }
  163. }
  164. /**
  165. * Page callback : 'Catalogue Formations'.
  166. */
  167. function fgcf_page_home() {
  168. drupal_add_css(drupal_get_path('module', 'fgcf') .'/theme/css/fgcf.css');
  169. drupal_set_title('Catalogue des formations France Galop');
  170. $filter_form = drupal_get_form('fgcf_filter_form');
  171. $ret = theme('fgcf_home', $filter_form);
  172. return $ret;
  173. }
  174. /**
  175. * Page callback : 'Formations par format'.
  176. */
  177. function fgcf_page_format($format = 0) {
  178. $bc = drupal_get_breadcrumb();
  179. $bc[] = l(t('Catalogue formations'), 'fgcf');
  180. $filter_form = drupal_get_form('fgcf_filter_form');
  181. $ret = $filter_form;
  182. $vid = _fgcf_get_vocabulary_by_name('FGCF Format de cours');
  183. if (empty($format)) {
  184. $ret .= views_embed_view('fgcf_formats_de_formations', 'default', $vid);
  185. }
  186. else {
  187. $term = taxonomy_get_term($format);
  188. if (!is_object($term) || !isset($term->vid) || $term->vid != $vid) {
  189. $format = 0;
  190. }
  191. else {
  192. $bc[] = l(t('Formats de formation'), 'fgcf/format');
  193. drupal_set_title(t('Format : @term', array('@term' => $term->name)));
  194. $values = _fgcf_filter_form_translate_values();
  195. }
  196. $ret .= views_embed_view('fgcf_formations_par_format', 'default', $format, $values['dif'], $values['modalites']);
  197. }
  198. drupal_set_breadcrumb($bc);
  199. return $ret;
  200. }
  201. /**
  202. * Page callback : 'Formations par organisme'.
  203. */
  204. function fgcf_page_organisme() {
  205. $bc = drupal_get_breadcrumb();
  206. $bc[] = l(t('Catalogue formations'), 'fgcf');
  207. drupal_set_breadcrumb($bc);
  208. $filter_form = drupal_get_form('fgcf_filter_form');
  209. $ret = $filter_form;
  210. $ret .= views_embed_view('fgcf_organismes_de_formation');
  211. return $ret;
  212. }
  213. /**
  214. * Page callback : 'Formations par thematique'.
  215. */
  216. function fgcf_page_thematique($thematique = 0) {
  217. $bc = drupal_get_breadcrumb();
  218. $bc[] = l(t('Catalogue formations'), 'fgcf');
  219. $filter_form = drupal_get_form('fgcf_filter_form');
  220. $ret = $filter_form;
  221. // The views itself doesn't mention vocab ids, which might be different in dev
  222. // and in production.
  223. // We limit to terms in the 'Thématique' vocab outside the view.
  224. $vid = _fgcf_get_vocabulary_by_name('FGCF Thématique');
  225. if (empty($thematique)) {
  226. // Afficher liste des thématiques de 1er niveau.
  227. $ret .= views_embed_view('fgcf_thematiques_premier_niveau', 'default', $vid);
  228. }
  229. else {
  230. $term = taxonomy_get_term($thematique);
  231. $parents = array();
  232. if (!is_object($term) || !isset($term->vid) || $term->vid != $vid) {
  233. $thematique = 0;
  234. $description = '';
  235. }
  236. else {
  237. $bc[] = l(t('Thématiques'), 'fgcf/thematique');
  238. $parents = taxonomy_get_parents($thematique);
  239. $description = $term->description;
  240. }
  241. if (empty($parents)) {
  242. // Top category: display list of sub-categories.
  243. drupal_set_title(t('Thématique : @term', array('@term' => $description)));
  244. $ret .= views_embed_view('fgcf_thematique_deuxieme_niveau', 'default', $thematique);
  245. }
  246. else {
  247. // Sub-category: display list of nodes.
  248. $parent = current($parents);
  249. $bc[] = l($parent->description, 'fgcf/thematique/'. $parent->tid);
  250. drupal_set_title(t('Thématique : @term', array('@term' => $description)));
  251. $values = _fgcf_filter_form_translate_values();
  252. $ret .= views_embed_view('fgcf_formations_par_thematique', 'default', $thematique, $values['dif'], $values['modalites']);
  253. }
  254. }
  255. drupal_set_breadcrumb($bc);
  256. return $ret;
  257. }
  258. /**
  259. * Page callback : 'ODT test'.
  260. */
  261. function fgcf_page_odt() {
  262. module_load_include('inc', 'fgcf', 'fgcf.odt');
  263. $template = dirname(__FILE__) ."/catalogue_template.odt";
  264. $odf = fgcf_odt_export_formations($template);
  265. $odf->exportAsAttachedFile('catalogue.odt');
  266. }
  267. /**
  268. * Theme function for course themes.
  269. */
  270. function theme_fgcf_thematiques($terms) {
  271. $items = array();
  272. foreach ($terms as $tid => $term) {
  273. $items[] = l($term->description, taxonomy_term_path($term));
  274. }
  275. // Build cat logo
  276. $cat_term = reset($terms);
  277. $nodes = nat_get_nids(array($cat_term->tid), TRUE);
  278. $cat_node = reset($nodes);
  279. $node_view = node_build_content($cat_node, FALSE, FALSE);
  280. $logo = drupal_render($node_view->content['field_fgcf_thematique_logo']);
  281. array_unshift($items, $logo);
  282. $ret = '<div class="node-thematique">'. theme('item_list', $items) .'</div>';
  283. return $ret;
  284. }
  285. /**
  286. * Subscribe form displayed on 'formations' nodes.
  287. */
  288. function fgcf_subscribe_form($form_state, $node) {
  289. $form['node'] = array(
  290. '#type' => 'value',
  291. '#value' => $node,
  292. );
  293. $form['subscribe'] = array(
  294. '#type' => 'submit',
  295. '#value' => t('Inscrivez-moi'),
  296. );
  297. return $form;
  298. }
  299. /**
  300. * Submit handler for the subscribe form.
  301. *
  302. * Send an email to site administrators.
  303. */
  304. function fgcf_subscribe_form_submit($form, &$form_state) {
  305. global $user;
  306. // Prevent flooding.
  307. if (flood_is_allowed('fgcg_subscribe', 10)) {
  308. $to = variable_get('site_mail', ini_get('sendmail_from'));
  309. $params = array(
  310. 'account' => $user,
  311. 'node' => $form_state['values']['node'],
  312. );
  313. $message = drupal_mail('fgcf', 'subscribe', $to, language_default(), $params);
  314. if ($message['result']) {
  315. drupal_set_message(t("Votre demande a été transmise aux administrateurs du site."));
  316. flood_register_event('fgcg_subscribe');
  317. }
  318. }
  319. }
  320. /**
  321. * Implemanbtation of hook_mail().
  322. */
  323. function fgcf_mail($key, &$message, $params) {
  324. $language = $message['language'];
  325. switch ($key) {
  326. case 'subscribe':
  327. // Generate base replacement strings.
  328. $variables = user_mail_tokens($params['account'], $language);
  329. // Add our own.
  330. $node = $params['node'];
  331. $variables['@node_title'] = $node->title;
  332. $variables['@node_reference'] = $node->field_fgcf_fiche_reference[0]['value'];
  333. $variables['!node_uri'] = url('node/' . $node->nid, array('absolute' => TRUE));
  334. $variables['!user_ip'] = ip_address();
  335. $message['subject'] = t('[France-galop] Inscription à une formation', $variables, $language->language);
  336. $message['body'][] = t("Formation: @node_reference - @node_title (!node_uri)\nUtilisateur: !username\nMail: !mailto\nIP de connection: !user_ip", $variables, $language->language);
  337. break;
  338. }
  339. }
  340. /**
  341. * Shared filter form for course lists.
  342. *
  343. * Filter values are taken from the form logic or, when absent, from the
  344. * session, in order for them to be persistent across non-form pages.
  345. *
  346. * @return array
  347. */
  348. function fgcf_filter_form($form_state) {
  349. $form['filtre'] = array(
  350. '#title' => t('Filtrer les formations'),
  351. '#attributes' => array('class' => 'fgcf-filtre'),
  352. '#type' => 'fieldset',
  353. '#collapsible' => TRUE,
  354. );
  355. $dif = isset($form_state['storage']['dif'])
  356. ? $form_state['storage']['dif']
  357. : isset($_SESSION['fgcf']['dif'])
  358. ? $_SESSION['fgcf']['dif']
  359. : FALSE;
  360. $form['filtre']['dif'] = array(
  361. '#type' => 'checkbox',
  362. '#default_value' => $dif,
  363. '#title' => t('Eligibles DIF'),
  364. );
  365. $modalite = isset($form_state['storage']['modalite'])
  366. ? $form_state['storage']['modalite']
  367. : isset($_SESSION['fgcf']['modalite'])
  368. ? $_SESSION['fgcf']['modalite']
  369. : array();
  370. $form['filtre']['modalite'] = array(
  371. '#title' => t('Modalité'),
  372. '#type' => 'checkboxes',
  373. '#options' => array(
  374. '1' => t('Inter-entreprises'),
  375. '2' => t('Intra-entreprise'),
  376. '3' => t('Autres'),
  377. ),
  378. '#default_value' => $modalite,
  379. );
  380. $form['filtre']['submit'] = array(
  381. '#type' => 'submit',
  382. '#value' => t('Appliquer le filtre'),
  383. );
  384. return $form;
  385. }
  386. /**
  387. * Submit handler for fgcf_filter_form().
  388. */
  389. function fgcf_filter_form_submit($form, &$form_state) {
  390. foreach (array('modalite', 'dif') as $key) {
  391. $value = $form_state['values'][$key];
  392. //$form_state['storage'][$key] = $value;
  393. $_SESSION['fgcf'][$key] = $value;
  394. }
  395. }
  396. /**
  397. * Translate values in the filter form into arguments for the views.
  398. */
  399. function _fgcf_filter_form_translate_values() {
  400. // Default values.
  401. $values = array(
  402. 'dif' => 'all',
  403. 'modalites' => 'all',
  404. );
  405. if (isset($_SESSION['fgcf']['dif']) && $_SESSION['fgcf']['dif']) {
  406. $values['dif'] = 'o';
  407. }
  408. if (isset($_SESSION['fgcf']['modalite'])) {
  409. $modalites = array_filter($_SESSION['fgcf']['modalite']);
  410. if (!empty($modalites)) {
  411. $values['modalites'] = implode('+', array_keys($modalites));
  412. }
  413. }
  414. return $values;
  415. }
  416. /**
  417. * Helper: get a vocabulary ID from its name.
  418. *
  419. * @todo : inefficient - replace with constants once the vocabs are deployed ?
  420. *
  421. * @param string $name
  422. */
  423. function _fgcf_get_vocabulary_by_name($name) {
  424. static $matches = array();
  425. if (!isset($matches[$name])) {
  426. $matches[$name] = 0;
  427. $vocabularies = taxonomy_get_vocabularies();
  428. foreach ($vocabularies as $vid => $vocabulary) {
  429. if (drupal_strtolower($vocabulary->name) == drupal_strtolower($name)) {
  430. $matches[$name] = $vid;
  431. break;
  432. }
  433. }
  434. }
  435. return $matches[$name];
  436. }