• Avail guidance to develop Drupal modules & Drupal themes
  • Share and get review of your code
  • Get Access to free CodeBooks and ThemeBooks
  • Drupal 5, 6 and 7 covered

Userpoints Meter

0 Points /1000 Points

Content types in tile with images

in Project codes

This CodeLet will create a tiled page with all the content type listed with individual image uploaded using the admin form. This CodeLets demonstrates user of different Drupal APIs like file_save_upload, hook_menu and hook_perm

<?php
//$Id$

/**
* @file
*
* Display a list of content types
*
* @author Drupal Developer <http://twitter.com/DrupalD>
*/


/**
* Implementation of hook_menu
*
* @author Drupal Developer <http://twitter.com/DrupalD>
*/
function upanel_menu() {
   
$items = array();
 
$items['upanel'] = array(
   
'title' => 'Home',
   
'page callback' => 'upanel_content_list',
   
'access arguments' => array('access upanel'),
  );

 
$items['admin/settings/upanel'] = array(
   
'title' => 'User panel',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('upanel_admin_form'),
   
'access arguments' => array('administer upanel'),
  );
    return
$items;
}


/**
* Implementation of hook_perm
*
* @author Drupal Developer <http://twitter.com/DrupalD>
*/
function upanel_perm() {
    return array(
"access upanel", "administer upanel");
}

/**
* Implementation of hook_init
*/
function upanel_init() {
   
drupal_add_css(drupal_get_path("module", 'upanel') ."/upanel.css");
}

/**
* upanel_content_list
*
* Display list of content types
*
* @author Drupal Developer <http://twitter.com/DrupalD>
*/
function upanel_content_list() {
   
$am__node = _upanel_get_node_types();
   
$am__images = _upnale_get_images();
   
$am__attributes = array("width" => 48, 'height' => 48);
   
$i = 0;
    foreach (
$am__node as $type => $name) {
        if (
$am__images[$type]) {
         
$ss__image = theme('image', $am__images[$type], $name, $name, $am__attributes, FALSE); 
        }
        else {
           
$ss__image = theme('image', drupal_get_path('module', 'upanel') ."/bookmark6.gif");
        }
    if (
$i % 6 == 0 && ($i != 0 || $i != 1)) {
       
$output .="<div style='clear:both;'>&nbsp;</div>";
    }
   
$output .= "<div class='unpanel-grid'>";
       
$ss__image_wraper = "<div style='margin-top:20px;'>". $ss__image ."</div>";
       
$output .= l($ss__image_wraper . drupal_ucfirst($name), str_replace("_", "-", $type), array('html' => TRUE));
       
$output .= "</div>";
       
$i++;
    }
    return
$output;
}


/**
* upanel_admin_form
*
* @author Drupal Developer <http://twitter.com/DrupalD>
*/
function upanel_admin_form() {
   
$am__node = _upanel_get_node_types();
   
$form = array();
   
$form['#attributes'] = array('enctype' => "multipart/form-data");
 
    foreach (
$am__node as $type => $name) {
       
$form['image_'. $type] = array(
         
'#type' => 'file',
         
'#title' => $name ." ". t("image"),
        );
    }
   
$form['submit'] = array(
   
'#type' => 'submit',
     
'#value' => t("Save configuration"),
    );
 
   
$form['#submit'] = array('upanel_admin_form_submit');
    return
$form;
}


/**
* upanel_admin_form_submit
*
* @param $form
* @param $form_status
* @author Drupal Developer <http://twitter.com/DrupalD>
*/
function upanel_admin_form_submit($form, $form_status) {
   
$am__images = preg_grep ("/^image_/", array_keys($form_status['values']));
   
$ss__destination = file_directory_path() ."/upanel";
    foreach (
$am__images as $ss__image) {
       
$as__type = preg_split("/(^image_)/", $ss__image);
        if (
$h__file = file_save_upload($ss__image, NULL, $ss__destination, FILE_EXISTS_RENAME)) {
           
db_query("UPDATE {upanel_images} SET node_type_image = '%s' WHERE node_type = '%s'", $h__file->filename, $as__type[1]);
            if (!
db_affected_rows()) {
        @
db_query("INSERT INTO {upanel_images} VALUES('%s', '%s')", $as__type[1], $h__file->filename);
            }
           
drupal_set_message(t("Image has been upload"));
            unset(
$h__file, $as__type);
        }
    }
}


/**
* _upanel_get_node_types
*
* @author Drupal Developer <http://twitter.com/DrupalD>
*/
function _upanel_get_node_types() {
 
$am__node_type = array_keys(node_get_types('types'));
 
$am__node_name = array_values(node_get_types('names'));
 
asort($am__node_name);
 
asort($am__node_type);
  return
array_combine($am__node_type, $am__node_name);
}


/**
* _upanel_get_node_types
*
* @author Drupal Developer <http://twitter.com/DrupalD>
*/
function _upnale_get_images() {
   
$r__result = db_query("SELECT * FROM {upanel_images}");
    while(
$om__result = db_fetch_object($r__result)) {
       
$am__image[$om__result->node_type] = file_directory_path() ."/upanel/". $om__result->node_type_image;
    }
    return
$am__image;
}
?>

5
Your rating: None Average: 5 (3 votes)

Comments

joshi's picture
5

 A screen of the output from this CodeLet will help understand what actually this CodeLet Does.

Regards.

[url=http://joshics.in]JoshicsIN[/url]

beautifulmind's picture

I'm imporving this code. Becuase it was not respecting native Drupal access system. Once I have done with it, I'll post the updated code and a screeshot as well.

Thank you for your concern and using the code. I really appreciate it.

Regards.

DrupalD's picture

Please use this link http://drupaldeveloper.in/upanel to see the code at work

Thank you.

[url=http://twitter.com/DrupalD][i]Drupa Developer - A Comprehensive Guild to Drupal Developersl[/i][/url]

Syndicate content

Recent comments