Last active
August 29, 2015 14:27
-
-
Save sam452/c459dbc85b3938791f66 to your computer and use it in GitHub Desktop.
Managed_files in drupal forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Admin module for managing builds | |
*/ | |
function calendar_admin_menu() { | |
$items = array(); | |
$items['calendar/admin/sponsor/new'] = array( | |
'title' => "Add new sponsor", | |
'page callback' => 'calendar_admin_add_sponsor_page', | |
'access callback' => TRUE, | |
); | |
return $items; | |
} | |
function sponsor_load($sponsorID) { | |
return db_query('SELECT sponsorID, sponsorName, sponsorBio, logo, sageID FROM {calSponsor} WHERE sponsorid = :sponsorid', array(':sponsorid' => $sponsorID))->fetchObject(); | |
} | |
function calendar_admin_add_sponsor_page() { | |
$item = new StdClass; | |
$item->sponsorID = FALSE; | |
$item->sponsorName = ''; | |
$item->sponsorBio = ''; | |
$item->logo = ''; | |
$item->sageID = ''; | |
return drupal_get_form('calendar_admin_sponsor_form', $item); | |
} | |
function calendar_admin_sponsor_form($form, &$form_state, $item) { | |
$form['#item'] = $item; | |
$form['sponsorName'] = array( | |
'#title' => t('Enter sponsor name'), | |
'#type' => 'textfield', | |
'#default_value' => $item->sponsorName, | |
); | |
$form['sponsorBio'] = array( | |
'#title' => t('Enter sponsor bio'), | |
'#type' => 'textfield', | |
'#default_value' => $item->sponsorBio, | |
); | |
$form['logo_fid'] = array( | |
'#type' => 'managed_file', | |
'#title' => t('Logo'), | |
'#progress_message' => t('Please wait...'), | |
'#progress_indicator' => 'bar', | |
'#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'), | |
'#default_value' => variable_get('logo_fid', ''), | |
'#upload_location' => 'public://logos/', | |
); | |
$form['logo'] = array( | |
'#title' => t('Logo file name'), | |
'#type' => 'hidden, | |
'#default_value' => $item->logo, | |
); | |
$form['sageID'] = array( | |
'#title' => t('Enter sageID'), | |
'#type' => 'textfield', | |
'#default_value' => $item->sageID, | |
); | |
$form['submit'] = array( | |
'#type' => 'submit', | |
'#value' => t('Save'), | |
); | |
return $form; | |
} | |
// function calendar_admin_sponsor_form_validate($form, &$form_state, $item) { | |
// $file = file_save_upload('file', array( | |
// // Validates file is really an image. | |
// 'file_validate_is_image' => array(), | |
// // Validate extensions. | |
// 'file_validate_extensions' => array('png gif jpg jpeg'), | |
// )); | |
// // If the file passed validation: | |
// if ($file) { | |
// // Move the file into the Drupal file system. | |
// if ($file = file_move($file, 'public://')) { | |
// // Save the file for use in the submit handler. | |
// $form_state['storage']['file'] = $file; | |
// } | |
// else { | |
// form_set_error('file', t("Failed to write the uploaded file to the site's file folder.")); | |
// } | |
// } | |
// else { | |
// form_set_error('file', t('No file was uploaded.')); | |
// } | |
// } | |
function calendar_admin_sponsor_form_submit($form, &$form_state) { | |
$item = $form['#item']; | |
$managed_file = (!empty($form['logo_fid']['#default_value'])) ? $form['logo_fid']['#default_value'] : 0; | |
if (isset($form_state['values']['logo_fid']) && $form_state['values']['logo_fid']) { | |
if (isset($managed_file_example) && $managed_file_example != $form_state['values']['logo_fid']) { | |
mf_remove_managed_file($managed_file_example, 'one'); | |
mf_add_managed_file($form_state['values']['logo_fid'], 'example'); | |
} else { | |
mf_add_managed_file($form_state['values']['logo_fid'], 'example'); | |
} | |
} elseif ($managed_file_one) { | |
mf_remove_managed_file($managed_file_example, 'example'); | |
} | |
if(isset($item->sponsorID) && $item->sponsorID) { | |
db_update('calSponsor') | |
->fields(array( | |
'sponsorName' => $form_state['values']['sponsorName'], | |
'sponsorBio' => $form_state['values']['sponsorBio'], | |
'logo' => $form_state['values']['logo'], | |
'sageID' => $form_state['values']['sageID'], | |
)) | |
->condition('sponsorid', $item->sponsorID) | |
->execute(); | |
$form_state['redirect'] = 'calendar/admin/sponsor/'. $item->sponsorID; | |
} else { | |
$sponsor = $sponsorID = db_insert('calSponsor') | |
->fields(array( | |
'sponsorName' => $form_state['values']['sponsorName'], | |
'sponsorBio' => $form_state['values']['sponsorBio'], | |
'logo' => $form_state['values']['logo_fid'], | |
'sageID' => $form_state['values']['sageID'], | |
)) | |
->execute(); | |
$file = file_load($form_state['values']['file']); | |
$file->status = FILE_STATUS_PERMANENT; | |
// file_usage_add($file, 'calendar_admin', 'user', $sponsor->sponsorID); | |
file_save($file); | |
$form_state['redirect'] = 'calendar/admin/sponsor/'. $sponsorID; | |
} | |
} | |
function mf_remove_managed_file($managed_file, $which) { | |
// Retrieve the old file's id. | |
$file = $managed_file ? file_load($managed_file) : FALSE; | |
if ($file) { | |
// When a module is managing a file, it must manage the usage count. | |
// Here we decrement the usage count with file_usage_delete(). | |
file_usage_delete($file, 'mf', $which, $file->fid); | |
// The file_delete() function takes a file object and checks to see if | |
// the file is being used by any other modules. If it is the delete | |
// operation is cancelled, otherwise the file is deleted. | |
file_delete($file); | |
drupal_set_message(t('The image @image_name was removed.', array('@image_name' => $file->filename))); | |
} | |
} | |
function mf_add_managed_file($managed_file, $which) { | |
/* if our file is already in use, then we don't need to re-do this and increase the count */ | |
$count = db_query('SELECT `count` FROM {file_usage} WHERE fid=:fid', array('fid' => $managed_file))->fetchField(); | |
if (empty($count)) { | |
/* load the file via fid */ | |
$file = file_load($managed_file); | |
/* change status to permanent */ | |
$file->status = FILE_STATUS_PERMANENT; | |
/* save the file */ | |
file_save($file); | |
/* record the file as in use */ | |
file_usage_add($file, 'mf', $which, $managed_file); | |
unset($file); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment