fgcf.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 Supports pédagogiques'):
  114. return 'fgcf/support/'. $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. // Désactivé pa #647
  203. // l(t('Formations par support'), 'fgcf/support'),
  204. );
  205. $links = theme('item_list', $links, NULL, 'ul', array('class' => 'fgcf-home-links'));
  206. $ret = theme('fgcf_home', $links, fgcf_page_thematique(0, $links));
  207. return $ret;
  208. }
  209. /**
  210. * Page callback : 'Formations par support'.
  211. */
  212. function fgcf_page_support($format = 0) {
  213. $bc = drupal_get_breadcrumb();
  214. $bc[] = l(t('Catalogue formations'), 'fgcf');
  215. $filter_form = drupal_get_form('fgcf_filter_form');
  216. $ret = $filter_form;
  217. $vid = _fgcf_get_vocabulary_by_name('FGCF Supports pédagogiques');
  218. if (empty($format)) {
  219. $ret .= views_embed_view('fgcf_supports_de_formations', 'default', $vid);
  220. }
  221. else {
  222. $term = taxonomy_get_term($format);
  223. if (!is_object($term) || !isset($term->vid) || $term->vid != $vid) {
  224. $format = 0;
  225. }
  226. else {
  227. $bc[] = l(t('Supports pédagogiques'), 'fgcf/support');
  228. drupal_set_title(t('Support: @term', array('@term' => $term->name)));
  229. $values = _fgcf_filter_form_translate_values();
  230. }
  231. $ret .= views_embed_view('fgcf_formations_par_support', 'default', $format, $values['dif'], $values['modalites']);
  232. }
  233. drupal_set_breadcrumb($bc);
  234. return $ret;
  235. }
  236. /**
  237. * Page callback : 'Formations par organisme'.
  238. */
  239. function fgcf_page_organisme() {
  240. $bc = drupal_get_breadcrumb();
  241. $bc[] = l(t('Catalogue formations'), 'fgcf');
  242. drupal_set_breadcrumb($bc);
  243. $filter_form = drupal_get_form('fgcf_filter_form');
  244. $ret = $filter_form;
  245. $ret .= views_embed_view('fgcf_organismes_de_formation');
  246. return $ret;
  247. }
  248. /**
  249. * Page callback : 'Formations par thematique'.
  250. */
  251. function fgcf_page_thematique($thematique = 0, $filter_post = NULL) {
  252. $bc = drupal_get_breadcrumb();
  253. $bc[] = l(t('Catalogue formations'), 'fgcf');
  254. $filter_form = drupal_get_form('fgcf_filter_form', $filter_post);
  255. $ret = $filter_form;
  256. // The views itself doesn't mention vocab ids, which might be different in dev
  257. // and in production.
  258. // We limit to terms in the 'Thème' vocab outside the view.
  259. $vid = _fgcf_get_vocabulary_by_name('FGCF Thème');
  260. if (empty($thematique)) {
  261. // Afficher liste des thèmes de 1er niveau.
  262. $ret .= views_embed_view('fgcf_thematiques_premier_niveau', 'default', $vid);
  263. }
  264. else {
  265. $term = taxonomy_get_term($thematique);
  266. $parents = array();
  267. if (!is_object($term) || !isset($term->vid) || $term->vid != $vid) {
  268. $thematique = 0;
  269. $description = '';
  270. }
  271. else {
  272. $bc[] = l(t('Thèmes'), 'fgcf/theme');
  273. $parents = taxonomy_get_parents($thematique);
  274. $description = $term->description;
  275. }
  276. if (empty($parents)) {
  277. // Top category: display list of sub-categories.
  278. drupal_set_title(t('Thème : @term', array('@term' => $description)));
  279. $ret .= views_embed_view('fgcf_thematique_deuxieme_niveau', 'default', $thematique);
  280. }
  281. else {
  282. // Sub-category: display list of nodes.
  283. $parent = current($parents);
  284. $bc[] = l($parent->description, 'fgcf/theme/'. $parent->tid);
  285. drupal_set_title(t('Thème : @term', array('@term' => $description)));
  286. $values = _fgcf_filter_form_translate_values();
  287. $ret .= views_embed_view('fgcf_formations_par_thematique', 'default', $thematique, $values['dif'], $values['modalites']);
  288. }
  289. }
  290. drupal_set_breadcrumb($bc);
  291. return $ret;
  292. }
  293. /**
  294. * Page callback : 'ODT test'.
  295. */
  296. function fgcf_page_odt() {
  297. module_load_include('inc', 'fgcf', 'fgcf.odt');
  298. $template = dirname(__FILE__) ."/catalogue_template.odt";
  299. $template = "catalogue_template.odt";
  300. $odf = fgcf_odt_export_formations($template);
  301. $odf->exportAsAttachedFile('catalogue.odt');
  302. }
  303. /**
  304. * Theme function for course themes.
  305. */
  306. function theme_fgcf_thematiques($terms) {
  307. $items = array();
  308. foreach ($terms as $tid => $term) {
  309. $items[] = l($term->description, taxonomy_term_path($term));
  310. }
  311. // Build cat logo
  312. $cat_term = reset($terms);
  313. $nodes = nat_get_nids(array($cat_term->tid), TRUE);
  314. $cat_node = reset($nodes);
  315. $node_view = node_build_content($cat_node, FALSE, FALSE);
  316. $logo = drupal_render($node_view->content['field_fgcf_thematique_logo']);
  317. array_unshift($items, $logo);
  318. $ret = '<div class="node-thematique">'. theme('item_list', $items) .'</div>';
  319. return $ret;
  320. }
  321. /**
  322. * Subscribe form displayed on 'formations' nodes.
  323. */
  324. function fgcf_subscribe_form($form_state, $node) {
  325. global $user;
  326. $form['node'] = array(
  327. '#type' => 'value',
  328. '#value' => $node,
  329. );
  330. if ($user->uid) {
  331. $form['subscribe'] = array(
  332. '#type' => 'submit',
  333. '#value' => t('Inscrivez-moi'),
  334. );
  335. }
  336. return $form;
  337. }
  338. /**
  339. * Submit handler for the subscribe form.
  340. *
  341. * Send an email to site administrators.
  342. */
  343. function fgcf_subscribe_form_submit($form, &$form_state) {
  344. global $user;
  345. // Prevent flooding.
  346. $flood_limit = variable_get('fgcf_subscribe_flood', 10);
  347. if (!$flood_limit || ($flood_limit && flood_is_allowed('fgcf_subscribe', $flood_limit))) {
  348. $to = variable_get('fgcf_mail_destination', variable_get('site_mail', ini_get('sendmail_from')));
  349. $params = array(
  350. 'account' => $user,
  351. 'node' => $form_state['values']['node'],
  352. );
  353. $message = drupal_mail('fgcf', 'subscribe', $to, language_default(), $params);
  354. if ($message['result']) {
  355. drupal_set_message(t("Votre demande a été transmise aux administrateurs du site."));
  356. flood_register_event('fgcf_subscribe');
  357. }
  358. }
  359. else {
  360. drupal_set_message(t('Trop de demandes d\'envoi de messages dans la dernière heure: attendez et recommencez.'), 'warning');
  361. }
  362. }
  363. /**
  364. * Implementation of hook_mail().
  365. */
  366. function fgcf_mail($key, &$message, $params) {
  367. $language = $message['language'];
  368. switch ($key) {
  369. case 'subscribe':
  370. // Generate base replacement strings.
  371. $variables = user_mail_tokens($params['account'], $language);
  372. // Add our own.
  373. $node = $params['node'];
  374. $variables['@node_title'] = $node->title;
  375. $variables['@node_reference'] = $node->field_fgcf_fiche_reference[0]['value'];
  376. $variables['!node_uri'] = url('node/' . $node->nid, array('absolute' => TRUE));
  377. $variables['!user_ip'] = ip_address();
  378. $message['subject'] = t('[France-galop] Inscription à une formation', $variables, $language->language);
  379. $message['body'][] = t("Formation: @node_reference - @node_title (!node_uri)\nUtilisateur: !username\nMail: !mailto\nIP de connection: !user_ip", $variables, $language->language);
  380. break;
  381. }
  382. }
  383. /**
  384. * Shared filter form for course lists.
  385. *
  386. * Filter values are taken from the form logic or, when absent, from the
  387. * session, in order for them to be persistent across non-form pages.
  388. *
  389. * @return array
  390. */
  391. function fgcf_filter_form($form_state, $post = NULL) {
  392. $form['filtre'] = array(
  393. '#title' => t('Filtrer les formations'),
  394. '#attributes' => array('class' => 'fgcf-filtre'),
  395. '#type' => 'fieldset',
  396. '#collapsible' => TRUE,
  397. );
  398. $dif = isset($form_state['storage']['dif'])
  399. ? $form_state['storage']['dif']
  400. : isset($_SESSION['fgcf']['dif'])
  401. ? $_SESSION['fgcf']['dif']
  402. : FALSE;
  403. $form['filtre']['dif'] = array(
  404. '#type' => 'checkbox',
  405. '#default_value' => $dif,
  406. '#title' => t('Eligibles DIF'),
  407. );
  408. $modalite = isset($form_state['storage']['modalite'])
  409. ? $form_state['storage']['modalite']
  410. : isset($_SESSION['fgcf']['modalite'])
  411. ? $_SESSION['fgcf']['modalite']
  412. : array();
  413. $form['filtre']['modalite'] = array(
  414. '#title' => t('Modalité<br />de formation'),
  415. '#type' => 'checkboxes',
  416. '#options' => array(
  417. '1' => t('Intra-entreprise'),
  418. '2' => t('Inter-entreprises'),
  419. '4' => t('Interne'),
  420. '999' => t('Autres'),
  421. ),
  422. '#default_value' => $modalite,
  423. );
  424. $form['filtre']['submit'] = array(
  425. '#type' => 'submit',
  426. '#value' => t('Appliquer le filtre'),
  427. );
  428. if (!is_null($post)) {
  429. $form['post'] = array(
  430. '#type' => 'markup',
  431. '#value' => $post,
  432. );
  433. }
  434. $form = array(
  435. '#type' => 'fieldset',
  436. $form,
  437. );
  438. return $form;
  439. }
  440. /**
  441. * Submit handler for fgcf_filter_form().
  442. */
  443. function fgcf_filter_form_submit($form, &$form_state) {
  444. foreach (array('modalite', 'dif') as $key) {
  445. $value = $form_state['values'][$key];
  446. $form_state['storage'][$key] = $value;
  447. $_SESSION['fgcf'][$key] = $value;
  448. }
  449. }
  450. /**
  451. * Translate values in the filter form into arguments for the views.
  452. */
  453. function _fgcf_filter_form_translate_values() {
  454. // Default values.
  455. $values = array(
  456. 'dif' => 'all',
  457. 'modalites' => 'all',
  458. );
  459. if (isset($_SESSION['fgcf']['dif']) && $_SESSION['fgcf']['dif']) {
  460. $values['dif'] = 'o';
  461. }
  462. if (isset($_SESSION['fgcf']['modalite'])) {
  463. $modalites = array_filter($_SESSION['fgcf']['modalite']);
  464. if (!empty($modalites)) {
  465. // 'Autres' (999) means 'all other values than 1 and 2'.
  466. // Translate this into actual values.
  467. if (isset($modalites[999])) {
  468. // Get the actual possible values from the field definition.
  469. $field = content_fields('field_fgcf_fiche_modalite', 'fgcf_fiche');
  470. $allowed_values = content_allowed_values($field);
  471. // Remove options 1 (intra), 2 (inter), 4 (internal).
  472. unset($allowed_values[1], $allowed_values[2], $allowed_values[4]);
  473. // Remove the 'proxy' 'Autres' choice.
  474. unset($modalites[999]);
  475. // Add the actual values.
  476. $modalites += drupal_map_assoc(array_keys($allowed_values));
  477. }
  478. $values['modalites'] = implode('+', array_keys($modalites));
  479. }
  480. }
  481. return $values;
  482. }
  483. /**
  484. * Helper: get a vocabulary ID from its name.
  485. *
  486. * @todo : inefficient - replace with constants once the vocabs are deployed ?
  487. *
  488. * @param string $name
  489. *
  490. * @return int
  491. */
  492. function _fgcf_get_vocabulary_by_name($name) {
  493. static $matches = array();
  494. if (!isset($matches[$name])) {
  495. $matches[$name] = 0;
  496. $vocabularies = taxonomy_get_vocabularies();
  497. foreach ($vocabularies as $vid => $vocabulary) {
  498. if (drupal_strtolower($vocabulary->name) == drupal_strtolower($name)) {
  499. $matches[$name] = $vid;
  500. break;
  501. }
  502. }
  503. }
  504. return $matches[$name];
  505. }
  506. /**
  507. * Theme function for fgcf_formatter_fgcf_support_page_link.
  508. *
  509. * @return string
  510. */
  511. function theme_fgcf_formatter_fgcf_support_page_link($element) {
  512. $term = taxonomy_get_term($element['#item']['value']);
  513. $ret = l($term->name, 'fgcf/support/'. $term->tid); // can only be a tid
  514. return $ret;
  515. }