Advertisement
Guest User

Untitled

a guest
Apr 12th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $mb_fichesmat = array(
  4.     'id' => 'mb_fichesmat',
  5.     'title' => 'Caractéristiques du matériel',
  6.     'page' => 'post',
  7.     'context' => 'normal',
  8.     'priority' => 'high',
  9.     'fields' => array(
  10.        
  11.         // GENERAL
  12.         array(
  13.                         'titre' => 'CARACTERISTIQUES',
  14.                         'champs' => array(
  15.                                         'name' => 'Nom',
  16.                                         'desc' => 'Nom du produit',
  17.                                         'id' => 'cmb_nom_mat',
  18.                                         'type' => 'text'
  19.                                     ),
  20.                                     array(
  21.                                         'name' => 'Constructeur',
  22.                                         'desc' => 'Nom du constructeur',
  23.                                         'id' => 'cmb_const_mat',
  24.                                         'type' => 'text'
  25.                                     ),
  26.                                     array(
  27.                                         'name' => 'Dimensions',
  28.                                         'desc' => 'Encombrement du produit en mm',
  29.                                         'id' => 'cmb_dim_mat',
  30.                                         'type' => 'text',
  31.                                         'std' => '  mm'
  32.                                     ),
  33.                                     array(
  34.                                         'name' => 'Poids',
  35.                                         'desc' => 'Poids du produit en Kg',
  36.                                         'id' => 'cmb_poids_mat',
  37.                                         'type' => 'text',
  38.                                         'std' => '  Kg'
  39.                                     ),
  40.                                     array(
  41.                                         'name' => 'Matériau',
  42.                                         'desc' => 'Plastique, acier, alu',
  43.                                         'id' => 'cmb_mater_mat',
  44.                                         'type' => 'multicheck',
  45.                                         'options' => array('Plastique', 'Acier', 'Alu', 'MESH', 'Cuivre', 'PVC')
  46.                                     ),
  47.                                     array(
  48.                                         'name' => 'Couleur',
  49.                                         'desc' => 'Noir, rouge, blanc',
  50.                                         'id' => 'cmb_color_mat',
  51.                                         'type' => 'select',
  52.                                         'options' => array('', 'Or', 'Cuivre', 'Argent', 'Metal', 'Alu', 'Bleu', 'Rouge', 'Vert', 'Jaune', 'Orange', 'Noir', 'Blanc', 'Mauve', 'Rose')
  53.                                     ),
  54.                                     array(
  55.                                         'name' => 'Référence produit',
  56.                                         'desc' => 'MEC-DD25-AAC',
  57.                                         'id' => 'cmb_ref_mat',
  58.                                         'type' => 'text'
  59.                                     ),
  60.                                     array(
  61.                                         'name' => 'Prix',
  62.                                         'desc' => 'Prix',
  63.                                         'id' => 'cmb_prix_mat',
  64.                                         'type' => 'text',
  65.                                         'std' => '  €'
  66.                                     ), 
  67.                                    
  68.                                     array(
  69.                                         'name' => 'Lien site officiel',
  70.                                         'desc' => 'Entrer l\'url du site officiel',
  71.                                         'id' => 'cmb_lien_site_mat',
  72.                                         'type' => 'text',
  73.                                         'std' => 'http://'
  74.                                     )
  75.                         ),
  76.        
  77.         // BOITIER
  78.         array(
  79.                         'titre' => 'Section 2',
  80.                         'champs' => array(
  81.                                         'name' => 'Type',
  82.                                         'desc' => 'desc 2',
  83.                                         'id' => 'cmb_typeboit_mat',
  84.                                         'type' => 'checkbox',
  85.                                         'options' => array( 'ATX', 'mATX', 'EATX', 'ITX', 'Mini ITX' )
  86.                                     ),
  87.                                     array(
  88.                                         'name' => 'Slots',
  89.                                         'desc' => 'desc 2.1',
  90.                                         'id' => 'cmb_slotsboit_mat',
  91.                                         'type' => 'text'
  92.                                     ),
  93.                                     array(
  94.                                         'name' => 'Baies 1',
  95.                                         'desc' => 'desc 2.3',
  96.                                         'id' => 'cmb_intbaiesboit_mat',
  97.                                         'type' => 'text'
  98.                                     ),
  99.                                     array(
  100.                                         'name' => 'Baies 2',
  101.                                         'desc' => 'desc 2.4',
  102.                                         'id' => 'cmb_extbaieboits_mat',
  103.                                         'type' => 'text'
  104.                                     )
  105.                                    
  106.                             )
  107.             )
  108.  
  109. );
  110.  
  111. add_action('admin_menu', 'add_mb_fichesmat');
  112. function add_mb_fichesmat() {
  113.     global $mb_fichesmat;
  114.     add_meta_box($mb_fichesmat['id'], $mb_fichesmat['title'], 'show_mb_fichesmat', $mb_fichesmat['page'], $mb_fichesmat['context'], $mb_fichesmat['priority']);
  115. }
  116.  
  117.  
  118. function show_mb_fichesmat() {
  119.     global $mb_fichesmat, $post;
  120.    
  121.       echo '<input type="hidden" name="add_mb_fichesmat_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  122.     echo '<div class="form-table"><div id="gd_metabox_wrap">';
  123.    
  124. foreach ($mb_fichesmat['fields'] as $fields) {
  125.        
  126.        
  127.        
  128.        
  129.    
  130.         echo '<h3>'.$fields['titre'].'</h3>';
  131.        
  132.         echo '<div class="gd_mb_box">';
  133.        
  134.         foreach ($fields as $field) {
  135.            
  136.            
  137.            
  138.        
  139.            
  140.                
  141.                
  142.                     $meta = get_post_meta($post->ID, $field['id'], true);
  143.                     $multimeta = get_post_meta($post->ID, $field['id'], false);
  144.                
  145.            
  146.                                      
  147.                 $label = '<label for="'.$field['id'].'"><h4>'.$field['name'].'</h4></label>';      
  148.                 $html_begin = '<div class="gd_metabox_label">'.$label.'<div class="gd_metabox_content"><div class="gd_metabox_desc">'.$field['desc'].'</div>';     
  149.                 $html_end = '</div></div>';
  150.                
  151.                          
  152.                                 switch ($field['type']) {
  153.                                    
  154.                                    
  155.                                     case 'checkbox':
  156.                                         echo $html_begin;
  157.                                         foreach ($field['options'] as $option => $value) {
  158.                                             echo '<input type="checkbox" name="'.$field['id'].$value.'" id="'.$field['id'].$value.'" style="display:inline; width: 11px; height: 11px; margin-right: 10px;"', $meta ? ' checked="checked"' : '', ' />'.$value.'<br>';
  159.                                         }
  160.                                         echo $html_end;
  161.                                         break;
  162.                                        
  163.                                         case 'multicheck':
  164.                                         echo $html_begin;
  165.                                         $i = 1;
  166.                                         foreach ($field['options'] as $option => $value) {
  167.                                             echo '<input style="display:inline; width: 11px; height: 11px; margin-right: 10px;" type="checkbox" name="', $field['id'], '[]" id="', $field['id'], $i, '" value="', $value, '"', in_array( $value, $multimeta ) ? ' checked="checked"' : '', ' />'.$value.'<br>';
  168.                                         $i++;
  169.                                            
  170.                                         }
  171.                                         echo $html_end;
  172.                                                                            
  173.                                         break;
  174.                                    
  175.                                    
  176.                                     case 'textarea':
  177.                                          
  178.                                         echo $html_begin.'<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:70%;">', $meta ? $meta : $field['std'], '</textarea>'.$html_end;
  179.                                         break;
  180.                                     case 'select':
  181.                                          echo $html_begin.'<select name="', $field['id'], '" id="', $field['id'], '" style="width:40%">';
  182.                                         foreach ($field['options'] as $option) {
  183.                                             echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  184.                                         }
  185.                                         echo '</select>'.$html_end;
  186.                                         break;
  187.                                     case 'text':
  188.                                          echo $html_begin.'<div class="gd_input"><input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" /></div>'.$html_end;
  189.                                         break;
  190.                                     }
  191.                                
  192.                                  
  193.                             }
  194.            
  195.             echo '</div>';
  196.              
  197.         }
  198.    
  199.     echo '</div></div>';
  200. }
  201.  
  202.  
  203. //Add Save Action
  204. add_action('save_post', 'save_mb_fichesmat');
  205.  
  206. function save_mb_fichesmat($post_id) {
  207.     global $mb_fichesmat;
  208.     if (!wp_verify_nonce($_POST['add_mb_fichesmat_nonce'], basename(__FILE__))) {
  209.         return $post_id;
  210.     }
  211.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  212.         return $post_id;
  213.     }
  214.     if ('page' == $_POST['post_type']) {
  215.         if (!current_user_can('edit_page', $post_id)) {
  216.             return $post_id;
  217.         }
  218.     } elseif (!current_user_can('edit_post', $post_id)) {
  219.         return $post_id;
  220.     }
  221.     foreach ($mb_fichesmat['fields'] as $fields) {
  222.        
  223.         foreach ( $fields as $field ) {
  224.            
  225.             $old = get_post_meta($post_id, $field['id'], true);
  226.             $new = $_POST[$field['id']];
  227.             if ($new && $new != $old) {
  228.                 update_post_meta($post_id, $field['id'], $new);
  229.             } elseif ('' == $new && $old) {
  230.                 delete_post_meta($post_id, $field['id'], $old);
  231.             }
  232.            
  233.         }
  234.     }
  235. }
  236.  
  237. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement