fgcf.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. $color = '';
  319. if (isset($cat_node->field_fgcf_thematique_color[0]['value'])) {
  320. $color = $cat_node->field_fgcf_thematique_color[0]['value'];
  321. // Add '#' prefix if not already present in the value.
  322. $color = (strpos($color, '#') !== 0 ? '#' . $color : $color);
  323. }
  324. $ret = '<div class="node-thematique"' . (!empty($color) ? " style=\";border-color: $color\"" : '') . '>';
  325. $ret .= theme('item_list', $items);
  326. $ret .= '</div>';
  327. return $ret;
  328. }
  329. /**
  330. * Subscribe form displayed on 'formations' nodes.
  331. */
  332. function fgcf_subscribe_form($form_state, $node) {
  333. global $user;
  334. $form['node'] = array(
  335. '#type' => 'value',
  336. '#value' => $node,
  337. );
  338. if ($user->uid) {
  339. $form['subscribe'] = array(
  340. '#type' => 'submit',
  341. '#value' => t('Inscrivez-moi'),
  342. );
  343. }
  344. return $form;
  345. }
  346. /**
  347. * Submit handler for the subscribe form.
  348. *
  349. * Send an email to site administrators.
  350. */
  351. function fgcf_subscribe_form_submit($form, &$form_state) {
  352. global $user;
  353. // Prevent flooding.
  354. $flood_limit = variable_get('fgcf_subscribe_flood', 10);
  355. if (!$flood_limit || ($flood_limit && flood_is_allowed('fgcf_subscribe', $flood_limit))) {
  356. $to = variable_get('fgcf_mail_destination', variable_get('site_mail', ini_get('sendmail_from')));
  357. $params = array(
  358. 'account' => $user,
  359. 'node' => $form_state['values']['node'],
  360. );
  361. $message = drupal_mail('fgcf', 'subscribe', $to, language_default(), $params);
  362. if ($message['result']) {
  363. drupal_set_message(t("Votre demande a été transmise aux administrateurs du site."));
  364. flood_register_event('fgcf_subscribe');
  365. }
  366. }
  367. else {
  368. drupal_set_message(t('Trop de demandes d\'envoi de messages dans la dernière heure: attendez et recommencez.'), 'warning');
  369. }
  370. }
  371. /**
  372. * Implementation of hook_mail().
  373. */
  374. function fgcf_mail($key, &$message, $params) {
  375. $language = $message['language'];
  376. switch ($key) {
  377. case 'subscribe':
  378. // Generate base replacement strings.
  379. $variables = user_mail_tokens($params['account'], $language);
  380. // Add our own.
  381. $node = $params['node'];
  382. $variables['@node_title'] = $node->title;
  383. $variables['@node_reference'] = $node->field_fgcf_fiche_reference[0]['value'];
  384. $variables['!node_uri'] = url('node/' . $node->nid, array('absolute' => TRUE));
  385. $variables['!user_ip'] = ip_address();
  386. $message['subject'] = t('[France-galop] Inscription à une formation', $variables, $language->language);
  387. $message['body'][] = t("Formation: @node_reference - @node_title (!node_uri)\nUtilisateur: !username\nMail: !mailto\nIP de connection: !user_ip", $variables, $language->language);
  388. break;
  389. }
  390. }
  391. /**
  392. * Shared filter form for course lists.
  393. *
  394. * Filter values are taken from the form logic or, when absent, from the
  395. * session, in order for them to be persistent across non-form pages.
  396. *
  397. * @return array
  398. */
  399. function fgcf_filter_form($form_state, $post = NULL) {
  400. // The form depends on session data, so we need to disable page caching on
  401. // pages displaying the form.
  402. $GLOBALS['conf']['cache'] = FALSE;
  403. $form['filtre'] = array(
  404. '#title' => t('Filtrer les formations'),
  405. '#attributes' => array('class' => 'fgcf-filtre'),
  406. '#type' => 'fieldset',
  407. '#collapsible' => TRUE,
  408. );
  409. $dif = isset($form_state['storage']['dif'])
  410. ? $form_state['storage']['dif']
  411. : isset($_SESSION['fgcf']['dif'])
  412. ? $_SESSION['fgcf']['dif']
  413. : FALSE;
  414. $form['filtre']['dif'] = array(
  415. '#type' => 'checkbox',
  416. '#default_value' => $dif,
  417. '#title' => t('Eligibles DIF'),
  418. );
  419. $modalite = isset($form_state['storage']['modalite'])
  420. ? $form_state['storage']['modalite']
  421. : isset($_SESSION['fgcf']['modalite'])
  422. ? $_SESSION['fgcf']['modalite']
  423. : array();
  424. $form['filtre']['modalite'] = array(
  425. '#title' => t('Modalité<br />de formation'),
  426. '#type' => 'checkboxes',
  427. '#options' => array(
  428. '1' => t('Intra-entreprise'),
  429. '2' => t('Inter-entreprises'),
  430. '4' => t('Interne'),
  431. '999' => t('Autres'),
  432. ),
  433. '#default_value' => $modalite,
  434. );
  435. $form['filtre']['submit'] = array(
  436. '#type' => 'submit',
  437. '#value' => t('Appliquer le filtre'),
  438. );
  439. if (!is_null($post)) {
  440. $form['post'] = array(
  441. '#type' => 'markup',
  442. '#value' => $post,
  443. );
  444. }
  445. $form = array(
  446. '#type' => 'fieldset',
  447. $form,
  448. );
  449. return $form;
  450. }
  451. /**
  452. * Submit handler for fgcf_filter_form().
  453. */
  454. function fgcf_filter_form_submit($form, &$form_state) {
  455. foreach (array('modalite', 'dif') as $key) {
  456. $value = $form_state['values'][$key];
  457. $form_state['storage'][$key] = $value;
  458. $_SESSION['fgcf'][$key] = $value;
  459. }
  460. }
  461. /**
  462. * Translate values in the filter form into arguments for the views.
  463. */
  464. function _fgcf_filter_form_translate_values() {
  465. // Default values.
  466. $values = array(
  467. 'dif' => 'all',
  468. 'modalites' => 'all',
  469. );
  470. if (isset($_SESSION['fgcf']['dif']) && $_SESSION['fgcf']['dif']) {
  471. $values['dif'] = 'o';
  472. }
  473. if (isset($_SESSION['fgcf']['modalite'])) {
  474. $modalites = array_filter($_SESSION['fgcf']['modalite']);
  475. if (!empty($modalites)) {
  476. // 'Autres' (999) means 'all other values than 1 and 2'.
  477. // Translate this into actual values.
  478. if (isset($modalites[999])) {
  479. // Get the actual possible values from the field definition.
  480. $field = content_fields('field_fgcf_fiche_modalite', 'fgcf_fiche');
  481. $allowed_values = content_allowed_values($field);
  482. // Remove options 1 (intra), 2 (inter), 4 (internal).
  483. unset($allowed_values[1], $allowed_values[2], $allowed_values[4]);
  484. // Remove the 'proxy' 'Autres' choice.
  485. unset($modalites[999]);
  486. // Add the actual values.
  487. $modalites += drupal_map_assoc(array_keys($allowed_values));
  488. }
  489. $values['modalites'] = implode('+', array_keys($modalites));
  490. }
  491. }
  492. return $values;
  493. }
  494. /**
  495. * Helper: get a vocabulary ID from its name.
  496. *
  497. * @todo : inefficient - replace with constants once the vocabs are deployed ?
  498. *
  499. * @param string $name
  500. *
  501. * @return int
  502. */
  503. function _fgcf_get_vocabulary_by_name($name) {
  504. static $matches = array();
  505. if (!isset($matches[$name])) {
  506. $matches[$name] = 0;
  507. $vocabularies = taxonomy_get_vocabularies();
  508. foreach ($vocabularies as $vid => $vocabulary) {
  509. if (drupal_strtolower($vocabulary->name) == drupal_strtolower($name)) {
  510. $matches[$name] = $vid;
  511. break;
  512. }
  513. }
  514. }
  515. return $matches[$name];
  516. }
  517. /**
  518. * Theme function for fgcf_formatter_fgcf_support_page_link.
  519. *
  520. * @return string
  521. */
  522. function theme_fgcf_formatter_fgcf_support_page_link($element) {
  523. $term = taxonomy_get_term($element['#item']['value']);
  524. $ret = l($term->name, 'fgcf/support/'. $term->tid); // can only be a tid
  525. return $ret;
  526. }