Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.89 KB | None | 0 0
  1. /***************************
  2. * create project metaboxes
  3. ***************************/
  4. function p52_register_project_meta_boxes( $meta_boxes ) {
  5.     $prefix = 'p52_';
  6.    
  7.     // Get all customer custom post type posts
  8.     $customers = get_posts( array(
  9.         'posts_per_page' => -1,
  10.         'post_type' => 'p52-contact'
  11.     ) );
  12.    
  13.     // project content tabs
  14.     $meta_box = array(
  15.         'title' => 'Project Info',
  16.         'pages' => 'p52-project',
  17.        
  18.         'tabs' => array(
  19.            
  20.         'contacts' => array(
  21.             'label' => 'Customer Info',
  22.             'icon' => 'dashicons-businessman',
  23.             ),
  24.        
  25.         'tasks' => array(
  26.             'label' => 'Tasks',
  27.             'icon' => 'dashicons-clipboard',
  28.             ),
  29.        
  30.         'documents' => array(
  31.             'label' => 'Documents',
  32.             'icon' => 'dashicons-portfolio',
  33.             ),
  34.        
  35.         'images' => array(
  36.             'label' => 'Images',
  37.             'icon' => 'dashicons-format-image',
  38.             ),
  39.            
  40.         'key' => array(
  41.             'label' => 'Key Contacts',
  42.             'icon' => 'dashicons-networking',
  43.         ),
  44.        
  45.         'expenses' => array(
  46.             'label' => 'Expenses',
  47.             'icon' => 'dashicons-editor-ol',
  48.             ),
  49.            
  50.            
  51.         ), // end tabs
  52.        
  53.         'tab_style' => 'default',
  54.        
  55.         'fields' => array(
  56.            
  57.             // Client Info
  58.             array(
  59.                 'name'  => 'Client Info',
  60.                 'id'    => $prefix . 'client',
  61.                 'type'  => 'group',
  62.                 'tab' => 'contacts',
  63.                
  64.                
  65.                 'fields' => array(
  66.            
  67.                     array(
  68.                         'name'  => 'Name',
  69.                         'id'    => $prefix . 'name',
  70.                         'type'  => 'post',
  71.                         'post_type' => 'p52-contact',
  72.                         'placeholder' => 'Select a Customer',
  73.                     ),
  74.  
  75.                     array(
  76.                         'name'  => 'Phone',
  77.                         'id'    => $prefix . 'phone',
  78.                         'type'  => 'hidden',
  79.                     ),
  80.                    
  81.                     array(
  82.                         'name'  => 'Email',
  83.                         'id'    => $prefix . 'email',
  84.                         'type'  => 'hidden',
  85.                     ),
  86.                    
  87.                     array(
  88.                         'name'  => 'Address',
  89.                         'id'    => $prefix . 'address',
  90.                         'type'  => 'hidden',
  91.                     ),
  92.                    
  93.                     ), // end customer fields
  94.                 ), //end customer
  95.            
  96.            
  97.                 // project tasks meta box
  98.                             array(
  99.                                 'name'  => 'Tasks',
  100.                                 'id'    => $prefix . 'tasks',
  101.                                 'type'  => 'group',
  102.                                 'clone' => true,
  103.                                 'tab' => 'tasks',
  104.  
  105.                                 'fields' => array(
  106.  
  107.                                     array(
  108.                                         'name'  => 'Task Name',
  109.                                         'id'    => $prefix . 'task_name',
  110.                                         'type'  => 'text',
  111.                                     ),
  112.  
  113.                                     array(
  114.                                         'name'  => 'Task Description',
  115.                                         'id'    => $prefix . 'task_description',
  116.                                         'type'  => 'wysiwyg',
  117.                                     ),
  118.  
  119.                                     array(
  120.                                         'name'  => 'Task Style',
  121.                                         'id'    => $prefix . 'task_style',
  122.                                         'type'  => 'select',
  123.                                         'options' => array(
  124.                                             'task_email' => 'Email',
  125.                                             'task_phone' => 'Phone',
  126.                                             'task_meeting' => 'Meeting',
  127.                                             'task_skype' => 'Skype',
  128.                                             'task_mockup' => 'Mockup',
  129.                                             'task_wireframe' => 'Wireframe',
  130.                                             'task_design' => 'Design',
  131.                                             'task_photography' => 'Photography',
  132.                                             'task_production' => 'Production',
  133.                                         ),
  134.                                         'placeholder' => 'Select a style',
  135.                                     ),
  136.  
  137.                                     array(
  138.                                         'name'  => 'Due Date',
  139.                                         'id'    => $prefix . 'task_due',
  140.                                         'type'  => 'datetime',
  141.                                     ),
  142.  
  143.                                     array(
  144.                                         'name'  => 'Status',
  145.                                         'id'    => $prefix . 'task_status',
  146.                                         'type'  => 'select',
  147.                                         'options' => array(
  148.                                             'task_not-started' => 'Not Started',
  149.                                             'task_in_progress' => 'In Progess',
  150.                                             'task_waiting_customer' => 'Waiting Customer',
  151.                                             'task_completed' => 'Completed',
  152.                                         ),
  153.                                         'placeholder' => 'Select a status',
  154.                                     ),
  155.  
  156.                                     array(
  157.                                         'name'  => 'Date Complete',
  158.                                         'id'    => $prefix . 'task_date_completed',
  159.                                         'type'  => 'datetime',
  160.                                     ),
  161.                             ),// end task fields
  162.  
  163.                         ), // end tasks
  164.            
  165.                     // Document Uploads
  166.                     array(
  167.                         'name' => 'Documents',
  168.                         'desc'  => 'Please select files to upload',
  169.                         'id'    => $prefix . 'files',
  170.                         'type'  => 'file_advanced',
  171.                         'tab' => 'documents',                        
  172.                     ), // end documents
  173.            
  174.                     // Private document Uploads
  175.                     array(
  176.                         'name' => 'Private Documents',
  177.                         'desc'  => 'Please select files to upload',
  178.                         'id'    => $prefix . 'pvt_files',
  179.                         'type'  => 'file_advanced',
  180.                         'tab' => 'documents',                        
  181.                     ), // end documents
  182.            
  183.                     // image uploads
  184.                     array(
  185.                         'name'  => 'Images',
  186.                         'desc'  => 'Upload the finished images',
  187.                         'id'    => $prefix . 'images',
  188.                         'type'  => 'image_advanced',
  189.                         'tab' => 'images',
  190.                     ), // end images
  191.  
  192.            
  193.                     // Key contacts
  194.                     array(
  195.                         'name'  => 'Key Contact',
  196.                         'id'    => $prefix . 'key_contact',
  197.                         'type'  => 'group',
  198.                         'clone' => true,
  199.                         'tab' => 'key',
  200.  
  201.                         'fields' => array(
  202.            
  203.                             array(
  204.                                 'name'  => 'Name',
  205.                                 'id'    => $prefix . 'key_name',
  206.                                 'type'  => 'post',
  207.                                 'post_type' => 'p52-contact',
  208.                                 'placeholder' => 'Select a Customer',
  209.                             ),
  210.  
  211.                             array(
  212.                                 'name'  => 'Company',
  213.                                 'id'    => $prefix . 'key_company',
  214.                                 'type'  => 'text',
  215.                             ),
  216.  
  217.                             array(
  218.                                 'name'  => 'Phone',
  219.                                 'id'    => $prefix . 'key_phone',
  220.                                 'type'  => 'text',
  221.                             ),
  222.  
  223.                             array(
  224.                                 'name'  => 'Email',
  225.                                 'id'    => $prefix . 'key_email',
  226.                                 'type'  => 'text',
  227.                             ),
  228.  
  229.                             array(
  230.                                 'name'  => 'Project Role',
  231.                                 'id'    => $prefix . 'key_role',
  232.                                 'type'  => 'select',
  233.                                 'options' => array(
  234.                                     'model' => 'Model',
  235.                                     'caterer' => 'Caterer',
  236.                                     'car' => 'Car Hire',
  237.                                     'hire' => 'Gear Hirer',
  238.                                     'venue' => 'Venue',
  239.                                     'printer' => 'Printer',
  240.                                     'airline' => 'Airline',
  241.                                     'other' => 'Other',
  242.                                     ),
  243.                                 'placeholder' => 'Select a role',
  244.                                 ),
  245.  
  246.                             ), // end key contact fields
  247.                        
  248.                 ), // end key contacts
  249.            
  250.            
  251.             // project expenses meta box
  252.                 array(
  253.                 //'name'  => 'Expenses',
  254.                 'id'    => $prefix . 'expenses',
  255.                 'type'  => 'group',
  256.                 'clone' => true,
  257.                 'tab' => 'expenses',
  258.        
  259.                     'fields' => array(
  260.  
  261.                         array(
  262.                             'name'  => 'Description',
  263.                             'id'    => $prefix . 'exp_description',
  264.                             'type'  => 'textarea',
  265.                             'columns' => 4
  266.                         ),
  267.                        
  268.                         array(
  269.                             'name'  => 'Qty/Hrs',
  270.                             'id'    => $prefix . 'exp_qty',
  271.                             'type'  => 'text',
  272.                             'columns' => 2
  273.                         ),
  274.  
  275.                         array(
  276.                             'name'  => 'Price',
  277.                             'id'    => $prefix . 'exp_price',
  278.                             'type'  => 'text',
  279.                             'columns' => 2
  280.                         ),
  281.  
  282.                         array(
  283.                             'name'  => 'Tax',
  284.                             'id'    => $prefix . 'exp_tax',
  285.                             'type'  => 'text',
  286.                             'columns' => 2
  287.                         ),
  288.  
  289.                         array(
  290.                             'name'  => 'Total',
  291.                             'id'    => $prefix . 'exp_total',
  292.                             'type'  => 'text',
  293.                             'columns' => 2
  294.                         ),
  295.                        
  296.                         ), // end expenses fields
  297.  
  298.                         ), // end expenses
  299.            
  300.             ), // end main fields
  301.        
  302.     ); // end project content
  303.    
  304.     // Append phone or another field of customer to meta box above.
  305.         foreach ( $customers as $customer )
  306.         {
  307.             // Show this email field if match name.
  308.             $meta_box['fields'][] = array(
  309.                 'name'      => 'Email',
  310.                 'id'        => $prefix . 'email_' . $customer->ID,
  311.                 'type'      => 'text',
  312.                 'std'       => get_post_meta( $customer->ID, 'p52_contact_email', true ),
  313.                 'visible'   => array( $prefix . 'name', $customer->ID ),
  314.             );
  315.            
  316.             // Show this phone field if match name.
  317.             $meta_box['fields'][] = array(
  318.                 'name'      => 'Phone',
  319.                 'id'        => $prefix . 'phone_' . $customer->ID,
  320.                 'type'      => 'text',
  321.                 'std'       => get_post_meta( $customer->ID, 'p52_contact_phone', true ),
  322.                 'visible'   => array( $prefix . 'name', $customer->ID ),
  323.             );
  324.            
  325.             // Show this company field if match name.
  326.             $meta_box['fields'][] = array(
  327.                 'name'      => 'Company',
  328.                 'id'        => $prefix . 'company_' . $customer->ID,
  329.                 'type'      => 'text',
  330.                 'std'       => get_post_meta( $customer->ID, 'p52_contact_company', true ),
  331.                 'visible'   => array( $prefix . 'name', $customer->ID ),
  332.             );
  333.            
  334.             // Show this address field if match name.
  335.             $meta_box['fields'][] = array(
  336.                 'name'      => 'Address',
  337.                 'id'        => $prefix . 'address_' . $customer->ID,
  338.                 'type'      => 'textarea',
  339.                 'std'       => get_post_meta( $customer->ID, 'p52_contact_address', true ),
  340.                 'visible'   => array( $prefix . 'name', $customer->ID ),
  341.             );
  342.            
  343.         }
  344.         $meta_boxes[] = $meta_box;
  345.    
  346.     $meta_boxes[] = array(
  347.         'id'       => 'project_description',
  348.         'title'    => 'Project Description',
  349.         'pages'    => array('p52-project'),
  350.         'context'  => 'normal',
  351.         'priority' => 'low',
  352.        
  353.  
  354.         'fields' => array(            
  355.        
  356.             array(
  357.                 'name'  => 'Description',
  358.                 'id'    => $prefix . 'proj_descriptions',
  359.                 'type'  => 'wysiwyg',
  360.             ),
  361.            
  362.                        
  363.         ), // end description fields
  364.     ); // end description
  365.    
  366.     // project status meta box
  367.     $meta_boxes[] = array(
  368.         'id'       => 'project_status',
  369.         'title'    => 'Project Status',
  370.         'pages'    => array('p52-project'),
  371.         'context'  => 'side',
  372.         'priority' => 'high',
  373.        
  374.  
  375.         'fields' => array(            
  376.        
  377.             array(
  378.                 'name'  => 'Commence Date',
  379.                 'desc'  => 'Date commenced',
  380.                 'id'    => $prefix . 'commence',
  381.                 'type'  => 'date',
  382.             ),
  383.            
  384.             array(
  385.                 'name'  => 'Project Status',
  386.                 'id'    => $prefix . 'status',
  387.                 'type'  => 'select',
  388.                 'options' => array(
  389.                     'contact' => 'Initial Contact',
  390.                     'quote' => 'Quoted',
  391.                     'won' => 'Won',
  392.                     'started' => 'Started',
  393.                     'lost' => 'Lost',
  394.                     'completed' => 'Completed',
  395.                 ),
  396.                 'placeholder' => 'Select a status',
  397.             ),
  398.            
  399.             array(
  400.                 'name'  => 'Closed Date',
  401.                 'desc'  => 'Date completed',
  402.                 'id'    => $prefix . 'closed',
  403.                 'type'  => 'date',
  404.             ),
  405.            
  406.                        
  407.         ), // end status fields
  408.     ); // status
  409.  
  410.     return $meta_boxes;
  411. }
  412.  
  413. add_filter( 'rwmb_meta_boxes', 'p52_register_project_meta_boxes' );
  414.  
  415. // Save data to phone field
  416. add_action('rwmb_before_save_post', function($post_id)
  417. {
  418.     // Get person ID to save from "Select a Customer" field
  419.     $person_to_save = intval( $_POST['p52_project_name'] );
  420.     // Save related field to phone field
  421.     $_POST['p52_project_phone'] = $_POST['p52_project_phone_' . $person_to_save];
  422.     // Unset all hidden fields
  423.     foreach ( $_POST as $key => $value )
  424.     {
  425.         if ( strpos( $key, 'p52_project_phone_' ) || strpos( $key, 'p52_project_email_' )  ||  strpos( $key, 'p52_project_address_' ) )
  426.             unset( $_POST[$key] );
  427.     }
  428.    
  429.     // Save related field to email field
  430.     $_POST['p52_project_email'] = $_POST['p52_project_email_' . $person_to_save];
  431.     // Unset all hidden fields
  432.     foreach ( $_POST as $key => $value )
  433.     {
  434.         if ( strpos( $key, 'p52_project_email_' )  ||  strpos( $key, 'p52_project_address_' ))
  435.             unset( $_POST[$key] );
  436.     }
  437.    
  438.     // Save related field to address field
  439.     $_POST['p52_project_address'] = $_POST['p52_project_address_' . $person_to_save];
  440.     // Unset all hidden fields
  441.     foreach ( $_POST as $key => $value )
  442.     {
  443.         if ( strpos( $key, 'p52_project_address_' ) )
  444.             unset( $_POST[$key] );
  445.     }
  446.    
  447. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement