Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function formation_help($path, $arg) {
- switch ($path) {
- // Main module help for the block module
- case 'admin/help#formation':
- return '<p>' . t('Formation module help.' ). '</p>';
- }
- }
- function formation_permission() {
- return array(
- 'Access formation' => array(
- 'title'=>t('Accès à la formation'),
- 'description'=>t("Add an access formation permission.")
- )
- );
- }
- function formation_user_login(&$edit, $account) {
- drupal_set_message(t('Welcome dear @username', array('@username'=>$account->name)));
- }
- function formation_menu() {
- $items = array();
- $items['training'] = array(
- 'title' => t('Training'),
- 'page callback' => 'training_page',
- 'access callback' => TRUE
- );
- $items['formation.rss'] = array(
- 'title' => t('Flux RSS'),
- 'page callback' => 'formation_flux',
- 'access callback' => TRUE
- );
- $items['training/welcome'] = array(
- 'title' => t('Welcome'),
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'weight' => 0,
- 'access callback' => TRUE
- );
- $items['training/liste-noeuds'] = array(
- 'title' => t('Nodes list'),
- 'type' => MENU_LOCAL_TASK,
- 'weight' => 1,
- 'page callback' => 'formation_display_nodes',
- 'access callback' => TRUE
- );
- $items['admin/config/content/import'] = array(
- 'title' => t('Import books'),
- 'type' => MENU_LOCAL_TASK,
- 'weight' => 1,
- 'page callback' => 'formation_import_books',
- 'access callback' => TRUE
- );
- $items['node/%node/stats'] = array(
- 'title' => t('Statistics'),
- 'type' => MENU_LOCAL_TASK,
- 'weight' => 1,
- 'page callback' => 'formation_node_showstats',
- 'page arguments' => array(1),
- 'access callback' => TRUE
- );
- return $items;
- }
- function formation_import_books($import=NULL) {
- if( !$import ) {
- return l('Import','admin/config/content/import/start');
- }
- if( $import == 'start' ) {
- $file = 'import.csv';
- $handle = fopen(drupal_get_path('module','formation').DIRECTORY_SEPARATOR.'import.csv','r');
- // Delete header row.
- fgetcsv( $handle, 8192, ',');
- while( $row = fgetcsv( $handle, 8192, ',') ) {
- $node = new stdClass();
- $node->type = 'livre';
- $node->field_summary['und'][0]['value'] = $row[2];
- $node->field_author['und'][0]['value'] = $row[1];
- $node->title = $row[0];
- node_save($node);
- $buffer[] = t('The book !book has been imported',array('!book'=>$row[0]));
- }
- fclose( $handle );
- return '<br /><br />'.implode('<br />',$buffer).'<br /><br />';
- }
- }
- function formation_display_nodes($nodeType=NULL) {
- if( !$nodeType ) {
- $types = node_type_get_types();
- $vars = array();
- foreach( $types as $type ) {
- $vars['items'][]['data'] = l(t($type->name),current_path().'/'.$type->type);
- }
- return theme('item_list',$vars);
- }
- $query = db_select('node','n')
- ->fields('n',array('nid','title'))
- ->orderBy('title', 'ASC');
- //if ( $nodeType == 'article' || $nodeType == 'page' )
- $query->condition('type',$nodeType,'=');
- $query = $query->extend('PagerDefault')
- ->limit(10);
- $result = $query->execute();
- $vars = array();
- foreach( $result as $row ) {
- $vars['items'][]['data'] = l($row->title,'node/'.$row->nid);
- }
- $vars['title'] = t('Node list of type !type' , array('!type'=>t(node_type_get_name( $nodeType ))));
- $backLink = '<br />'.l(t('Back'),'training/liste-noeuds');
- return theme('item_list',$vars).theme('pager',array('tags'=>array())).$backLink;
- }
- function formation_flux() {
- $content = genere_flux(range(50,75));
- drupal_add_http_header('Content-Type','application/rss+xml; charset=utf-8');
- print $content;
- }
- function formation_block_info() {
- $blocks = array();
- $blocks['formation_welcome'] = array(
- 'info' => t('Formation welcome message'),
- 'status' => 1,
- 'region' => 'sidebar_second'
- );
- $blocks['formation_compteur'] = array(
- 'info' => t('Formation compteur'),
- 'status' => 0
- );
- $blocks['formation_node_stats'] = array(
- 'info' => t('Formation node stats'),
- 'status' => 1,
- 'region' => 'content'
- );
- return $blocks;
- }
- function formation_block_view($delta = '') {
- $block = array();
- switch( $delta ) {
- case 'formation_welcome':
- $block['subject'] = t('Welcome');
- $block['content'] = '<p>'.t('Welcome on our website.').'<br />'.t('It is !date',array('!date'=>date('H:i'))).'</p>';
- break;
- case 'formation_compteur':
- $block['subject'] = t('Compteur');
- $block['content'] = formation_count_nodes();
- break;
- case 'formation_node_stats':
- $block['subject'] = t('Statistics');
- $block['content'] = formation_node_blockstats();
- break;
- }
- return $block;
- }
- function formation_count_nodes() {
- $headers = array('type'=>t('Type'),'compteur'=>t('Total'));
- // QUERY AVEC COUNT !!!!
- $query = db_select('node','n');
- $type = $query->addField('n','type','n_type');
- $query->groupBy('n_type');
- $query->addExpression('COUNT(n.nid)', 'node_count');
- $result = $query->execute()->fetchAllAssoc('n_type');
- foreach($result as $row) {
- $data[] = array('type'=>$row->n_type,'compteur'=>$row->node_count);
- }
- return theme('table',array('rows'=>$data,'header'=>$headers));
- }
- function training_page() {
- global $user;
- $text = t('You are on the page Training').'<br />';
- $text .= ( user_is_logged_in() ) ? t('Your username is !username',array('!username'=>$user->name)) : t("You're not logged in") ;
- return '<p>'.$text.'</p>';
- }
- function formation_node_delete( $node ) {
- $query = db_delete('formation_count')->condition('nid',$node->nid)->execute();
- }
- function formation_node_view($node, $view_mode, $langcode) {
- $query = db_select('formation_count','n')
- ->fields('n',array('compteur'))
- ->condition('nid',$node->nid);
- $result = $query->execute()->fetch();
- $compteur = 1;
- if( !$result ) {
- $query = db_insert('formation_count');
- }
- else {
- $query = db_update('formation_count');
- $compteur = $result->compteur + 1;
- }
- $query = $query->fields(array(
- 'nid' => $node->nid,
- 'compteur' => $compteur
- ));
- if( $result )
- $query = $query->condition('nid',$node->nid );
- $query = $query->execute();
- }
- function formation_node_showstats($node) {
- $result = db_select('formation_count','n')
- ->fields('n',array('compteur'))
- ->condition('nid',$node->nid)
- ->execute()
- ->fetch();
- $content = t('The node !title has been displayed !compteur times',array('!title'=>$node->title,'!compteur'=>$result->compteur));
- return $content;
- }
- function formation_node_blockstats() {
- if( !preg_match('/node\/([0-9]+)$/',request_uri(),$match))
- return;
- $result = db_select('formation_count','n')
- ->fields('n',array('compteur'))
- ->condition('nid',$match[1])
- ->execute()
- ->fetch();
- $node = node_load($match[1]);
- $content = t('The node !title has been displayed !compteur times',array('!title'=>$node->title,'!compteur'=>$result->compteur));
- return $content;
- }
- function formation_cron() {
- formation_import_books('start');
- }
Add Comment
Please, Sign In to add comment