Guest User

Untitled

a guest
Jul 16th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. <?php
  2.  
  3. function formation_help($path, $arg) {
  4. switch ($path) {
  5. // Main module help for the block module
  6. case 'admin/help#formation':
  7. return '<p>' . t('Formation module help.' ). '</p>';
  8. }
  9. }
  10.  
  11. function formation_permission() {
  12. return array(
  13. 'Access formation' => array(
  14. 'title'=>t('Accès à la formation'),
  15. 'description'=>t("Add an access formation permission.")
  16. )
  17. );
  18. }
  19.  
  20. function formation_user_login(&$edit, $account) {
  21. drupal_set_message(t('Welcome dear @username', array('@username'=>$account->name)));
  22. }
  23.  
  24. function formation_menu() {
  25. $items = array();
  26.  
  27. $items['training'] = array(
  28. 'title' => t('Training'),
  29. 'page callback' => 'training_page',
  30. 'access callback' => TRUE
  31. );
  32.  
  33. $items['formation.rss'] = array(
  34. 'title' => t('Flux RSS'),
  35. 'page callback' => 'formation_flux',
  36. 'access callback' => TRUE
  37. );
  38.  
  39. $items['training/welcome'] = array(
  40. 'title' => t('Welcome'),
  41. 'type' => MENU_DEFAULT_LOCAL_TASK,
  42. 'weight' => 0,
  43. 'access callback' => TRUE
  44. );
  45.  
  46. $items['training/liste-noeuds'] = array(
  47. 'title' => t('Nodes list'),
  48. 'type' => MENU_LOCAL_TASK,
  49. 'weight' => 1,
  50. 'page callback' => 'formation_display_nodes',
  51. 'access callback' => TRUE
  52. );
  53.  
  54. $items['admin/config/content/import'] = array(
  55. 'title' => t('Import books'),
  56. 'type' => MENU_LOCAL_TASK,
  57. 'weight' => 1,
  58. 'page callback' => 'formation_import_books',
  59. 'access callback' => TRUE
  60. );
  61.  
  62. $items['node/%node/stats'] = array(
  63. 'title' => t('Statistics'),
  64. 'type' => MENU_LOCAL_TASK,
  65. 'weight' => 1,
  66. 'page callback' => 'formation_node_showstats',
  67. 'page arguments' => array(1),
  68. 'access callback' => TRUE
  69. );
  70.  
  71. return $items;
  72. }
  73.  
  74. function formation_import_books($import=NULL) {
  75. if( !$import ) {
  76. return l('Import','admin/config/content/import/start');
  77. }
  78.  
  79. if( $import == 'start' ) {
  80. $file = 'import.csv';
  81.  
  82. $handle = fopen(drupal_get_path('module','formation').DIRECTORY_SEPARATOR.'import.csv','r');
  83. // Delete header row.
  84. fgetcsv( $handle, 8192, ',');
  85.  
  86. while( $row = fgetcsv( $handle, 8192, ',') ) {
  87. $node = new stdClass();
  88. $node->type = 'livre';
  89. $node->field_summary['und'][0]['value'] = $row[2];
  90. $node->field_author['und'][0]['value'] = $row[1];
  91. $node->title = $row[0];
  92.  
  93. node_save($node);
  94. $buffer[] = t('The book !book has been imported',array('!book'=>$row[0]));
  95. }
  96.  
  97. fclose( $handle );
  98.  
  99. return '<br /><br />'.implode('<br />',$buffer).'<br /><br />';
  100. }
  101. }
  102.  
  103. function formation_display_nodes($nodeType=NULL) {
  104.  
  105. if( !$nodeType ) {
  106. $types = node_type_get_types();
  107. $vars = array();
  108. foreach( $types as $type ) {
  109. $vars['items'][]['data'] = l(t($type->name),current_path().'/'.$type->type);
  110. }
  111.  
  112. return theme('item_list',$vars);
  113. }
  114.  
  115.  
  116. $query = db_select('node','n')
  117. ->fields('n',array('nid','title'))
  118. ->orderBy('title', 'ASC');
  119.  
  120. //if ( $nodeType == 'article' || $nodeType == 'page' )
  121. $query->condition('type',$nodeType,'=');
  122.  
  123. $query = $query->extend('PagerDefault')
  124. ->limit(10);
  125.  
  126. $result = $query->execute();
  127.  
  128. $vars = array();
  129. foreach( $result as $row ) {
  130. $vars['items'][]['data'] = l($row->title,'node/'.$row->nid);
  131. }
  132.  
  133. $vars['title'] = t('Node list of type !type' , array('!type'=>t(node_type_get_name( $nodeType ))));
  134. $backLink = '<br />'.l(t('Back'),'training/liste-noeuds');
  135. return theme('item_list',$vars).theme('pager',array('tags'=>array())).$backLink;
  136. }
  137.  
  138. function formation_flux() {
  139. $content = genere_flux(range(50,75));
  140. drupal_add_http_header('Content-Type','application/rss+xml; charset=utf-8');
  141. print $content;
  142. }
  143.  
  144. function formation_block_info() {
  145.  
  146. $blocks = array();
  147. $blocks['formation_welcome'] = array(
  148. 'info' => t('Formation welcome message'),
  149. 'status' => 1,
  150. 'region' => 'sidebar_second'
  151. );
  152.  
  153. $blocks['formation_compteur'] = array(
  154. 'info' => t('Formation compteur'),
  155. 'status' => 0
  156. );
  157.  
  158. $blocks['formation_node_stats'] = array(
  159. 'info' => t('Formation node stats'),
  160. 'status' => 1,
  161. 'region' => 'content'
  162. );
  163.  
  164. return $blocks;
  165. }
  166.  
  167. function formation_block_view($delta = '') {
  168. $block = array();
  169.  
  170. switch( $delta ) {
  171. case 'formation_welcome':
  172. $block['subject'] = t('Welcome');
  173. $block['content'] = '<p>'.t('Welcome on our website.').'<br />'.t('It is !date',array('!date'=>date('H:i'))).'</p>';
  174. break;
  175.  
  176. case 'formation_compteur':
  177. $block['subject'] = t('Compteur');
  178. $block['content'] = formation_count_nodes();
  179. break;
  180.  
  181. case 'formation_node_stats':
  182. $block['subject'] = t('Statistics');
  183. $block['content'] = formation_node_blockstats();
  184. break;
  185. }
  186.  
  187. return $block;
  188. }
  189.  
  190. function formation_count_nodes() {
  191.  
  192. $headers = array('type'=>t('Type'),'compteur'=>t('Total'));
  193.  
  194. // QUERY AVEC COUNT !!!!
  195. $query = db_select('node','n');
  196. $type = $query->addField('n','type','n_type');
  197. $query->groupBy('n_type');
  198. $query->addExpression('COUNT(n.nid)', 'node_count');
  199. $result = $query->execute()->fetchAllAssoc('n_type');
  200. foreach($result as $row) {
  201. $data[] = array('type'=>$row->n_type,'compteur'=>$row->node_count);
  202. }
  203.  
  204. return theme('table',array('rows'=>$data,'header'=>$headers));
  205.  
  206. }
  207.  
  208. function training_page() {
  209. global $user;
  210. $text = t('You are on the page Training').'<br />';
  211. $text .= ( user_is_logged_in() ) ? t('Your username is !username',array('!username'=>$user->name)) : t("You're not logged in") ;
  212.  
  213. return '<p>'.$text.'</p>';
  214. }
  215.  
  216. function formation_node_delete( $node ) {
  217. $query = db_delete('formation_count')->condition('nid',$node->nid)->execute();
  218. }
  219.  
  220. function formation_node_view($node, $view_mode, $langcode) {
  221. $query = db_select('formation_count','n')
  222. ->fields('n',array('compteur'))
  223. ->condition('nid',$node->nid);
  224.  
  225. $result = $query->execute()->fetch();
  226. $compteur = 1;
  227.  
  228. if( !$result ) {
  229. $query = db_insert('formation_count');
  230. }
  231. else {
  232. $query = db_update('formation_count');
  233. $compteur = $result->compteur + 1;
  234. }
  235.  
  236. $query = $query->fields(array(
  237. 'nid' => $node->nid,
  238. 'compteur' => $compteur
  239. ));
  240.  
  241. if( $result )
  242. $query = $query->condition('nid',$node->nid );
  243.  
  244. $query = $query->execute();
  245.  
  246. }
  247.  
  248. function formation_node_showstats($node) {
  249. $result = db_select('formation_count','n')
  250. ->fields('n',array('compteur'))
  251. ->condition('nid',$node->nid)
  252. ->execute()
  253. ->fetch();
  254.  
  255. $content = t('The node !title has been displayed !compteur times',array('!title'=>$node->title,'!compteur'=>$result->compteur));
  256.  
  257. return $content;
  258. }
  259.  
  260.  
  261. function formation_node_blockstats() {
  262. if( !preg_match('/node\/([0-9]+)$/',request_uri(),$match))
  263. return;
  264.  
  265. $result = db_select('formation_count','n')
  266. ->fields('n',array('compteur'))
  267. ->condition('nid',$match[1])
  268. ->execute()
  269. ->fetch();
  270.  
  271. $node = node_load($match[1]);
  272.  
  273. $content = t('The node !title has been displayed !compteur times',array('!title'=>$node->title,'!compteur'=>$result->compteur));
  274.  
  275. return $content;
  276. }
  277.  
  278. function formation_cron() {
  279. formation_import_books('start');
  280. }
Add Comment
Please, Sign In to add comment