fgcf.module 16 KB

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