| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Implementation of hook_field_info().
|
| 5 |
*/
|
| 6 |
function asset_field_info() {
|
| 7 |
return array(
|
| 8 |
'asset' => array('label' => 'Asset'),
|
| 9 |
);
|
| 10 |
}
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_field_settings().
|
| 14 |
*/
|
| 15 |
function asset_field_settings($op, $field) {
|
| 16 |
switch ($op) {
|
| 17 |
case 'form':
|
| 18 |
|
| 19 |
$type_options = array();
|
| 20 |
$types = asset_get_types();
|
| 21 |
foreach($types as $key => $type){
|
| 22 |
$type_options[$key] = $type->name;
|
| 23 |
}
|
| 24 |
$form['allowed_types'] = array(
|
| 25 |
'#type' => 'checkboxes',
|
| 26 |
'#title' => t('Allowed asset types'),
|
| 27 |
'#description' => t('Limit the selectable assets to only assets of the selected type(s).'),
|
| 28 |
'#options' => $type_options,
|
| 29 |
'#default_value' => $field['allowed_types'],
|
| 30 |
);
|
| 31 |
$form['allowed_extensions'] = array(
|
| 32 |
'#type' => 'textfield',
|
| 33 |
'#title' => t('Allowed file extensions'),
|
| 34 |
'#description' => t('Filter the available file type assets by this comma separated list of file extensions. This only applies to File assets. Ex: jpg,gif,png'),
|
| 35 |
'#default_value' => $field['allowed_extensions'],
|
| 36 |
);
|
| 37 |
|
| 38 |
$options = array();
|
| 39 |
$formatters = asset_get_formatters();
|
| 40 |
foreach($formatters as $key => $formatter){
|
| 41 |
$options[$key] = $formatter->name;
|
| 42 |
}
|
| 43 |
$form['valid_formatters'] = array(
|
| 44 |
'#type' => 'select',
|
| 45 |
'#title' => t('Valid formatters'),
|
| 46 |
'#description' => t('Limit the selectable assets to only assets that support these formatters. Leave empty to allow all assets.'),
|
| 47 |
'#options' => $options,
|
| 48 |
'#multiple' => TRUE,
|
| 49 |
'#size' => min(count($options), 10),
|
| 50 |
'#default_value' => $field['valid_formatters'],
|
| 51 |
);
|
| 52 |
return $form;
|
| 53 |
|
| 54 |
case 'save':
|
| 55 |
return array('allowed_types', 'allowed_extensions', 'valid_formatters');
|
| 56 |
|
| 57 |
case 'database columns':
|
| 58 |
return array(
|
| 59 |
'aid' => array('type' => 'int', 'length' => '10', 'not null' => TRUE, 'default' => 0),
|
| 60 |
);
|
| 61 |
|
| 62 |
case 'filters':
|
| 63 |
return array();
|
| 64 |
break;
|
| 65 |
}
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Implementation of hook_field().
|
| 70 |
*/
|
| 71 |
function asset_field($op, &$node, $field, &$items, $teaser, $page) {
|
| 72 |
switch ($op) {
|
| 73 |
case 'validate':
|
| 74 |
if($field['allowed_extensions']){
|
| 75 |
$allowed_extensions = array_map('trim', explode(',', $field['allowed_extensions']));
|
| 76 |
}
|
| 77 |
|
| 78 |
foreach($items as $delta => $item){
|
| 79 |
$error_field = $field['field_name'] .']['. $delta .'][aid';
|
| 80 |
if(!$item['aid']){
|
| 81 |
continue;
|
| 82 |
}
|
| 83 |
$asset = asset_load($item['aid']);
|
| 84 |
|
| 85 |
// make sure selected asset is allowed type
|
| 86 |
if(is_array($field['allowed_types']) && !$field['allowed_types'][$asset->type]){
|
| 87 |
form_set_error($error_field, t('The selected asset type is not allowed.'));
|
| 88 |
}
|
| 89 |
|
| 90 |
// make sure selected asset is allowed extension
|
| 91 |
if($asset->type == 'file' && $allowed_extensions){
|
| 92 |
$info = pathinfo($asset->file['filename']);
|
| 93 |
if(!in_array($info['extension'], $allowed_extensions)){
|
| 94 |
form_set_error($error_field, t('The selected asset is not one of the allowed extensions.'));
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
// make sure asset supports one of the valid formatters
|
| 99 |
if($field['valid_formatters']){
|
| 100 |
$valid = FALSE;
|
| 101 |
foreach($field['valid_formatters'] as $format){
|
| 102 |
if($asset->formatters[$format]){
|
| 103 |
$valid = TRUE;
|
| 104 |
break;
|
| 105 |
}
|
| 106 |
}
|
| 107 |
if(!$valid){
|
| 108 |
form_set_error($error_field, t('Asset does not support the specified formatters.'));
|
| 109 |
}
|
| 110 |
}
|
| 111 |
}
|
| 112 |
|
| 113 |
break;
|
| 114 |
|
| 115 |
case 'view':
|
| 116 |
break;
|
| 117 |
}
|
| 118 |
}
|
| 119 |
|
| 120 |
/**
|
| 121 |
* Implementation of hook_field_formatter_info().
|
| 122 |
*/
|
| 123 |
function asset_field_formatter_info() {
|
| 124 |
$formatters = array(
|
| 125 |
'default' => array(
|
| 126 |
'label' => t('Default'),
|
| 127 |
'field types' => array('asset'),
|
| 128 |
),
|
| 129 |
'icon' => array(
|
| 130 |
'label' => t('Asset Icon'),
|
| 131 |
'field types' => array('asset'),
|
| 132 |
),
|
| 133 |
);
|
| 134 |
|
| 135 |
$asset_formatters = asset_get_formatters();
|
| 136 |
foreach($asset_formatters as $key => $formatter){
|
| 137 |
$formatters[$key] = array(
|
| 138 |
'label' => $formatter->name,
|
| 139 |
'field types' => array('asset'),
|
| 140 |
);
|
| 141 |
}
|
| 142 |
|
| 143 |
return $formatters;
|
| 144 |
}
|
| 145 |
|
| 146 |
/**
|
| 147 |
* Implementation of hook_field_formatter().
|
| 148 |
*
|
| 149 |
*/
|
| 150 |
function asset_field_formatter($field, $item, $formatter, $node) {
|
| 151 |
$asset = asset_load($item['aid']);
|
| 152 |
if($asset->aid){
|
| 153 |
switch($formatter){
|
| 154 |
case 'default':
|
| 155 |
return asset_view($asset);
|
| 156 |
case 'icon':
|
| 157 |
return asset_icon($asset);
|
| 158 |
default:
|
| 159 |
return asset_render($asset, array('format' => $formatter));
|
| 160 |
}
|
| 161 |
}
|
| 162 |
}
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Implementation of hook_widget_info().
|
| 166 |
*/
|
| 167 |
function asset_widget_info() {
|
| 168 |
return array(
|
| 169 |
'asset' => array(
|
| 170 |
'label' => t('Asset Wizard'),
|
| 171 |
'field types' => array('asset'),
|
| 172 |
),
|
| 173 |
'directory' => array(
|
| 174 |
'label' => t('Asset Directory List'),
|
| 175 |
'field types' => array('asset'),
|
| 176 |
),
|
| 177 |
);
|
| 178 |
}
|
| 179 |
|
| 180 |
/**
|
| 181 |
* Implementation of hook_widget_settings().
|
| 182 |
*/
|
| 183 |
function asset_widget_settings($op, $widget) {
|
| 184 |
switch ($op) {
|
| 185 |
case 'form':
|
| 186 |
$form = array();
|
| 187 |
if($widget['type'] == 'directory'){
|
| 188 |
$form['parent'] = array(
|
| 189 |
'#type' => 'select',
|
| 190 |
'#title' => t('Parent Directory'),
|
| 191 |
'#options' => asset_directory_options(),
|
| 192 |
'#default_value' => $widget['parent'],
|
| 193 |
);
|
| 194 |
}
|
| 195 |
return $form;
|
| 196 |
|
| 197 |
case 'validate':
|
| 198 |
break;
|
| 199 |
|
| 200 |
case 'save':
|
| 201 |
if($widget['type'] == 'directory'){
|
| 202 |
return array('parent');
|
| 203 |
}
|
| 204 |
return array();
|
| 205 |
}
|
| 206 |
}
|
| 207 |
|
| 208 |
|
| 209 |
/**
|
| 210 |
* Implementation of hook_widget().
|
| 211 |
*/
|
| 212 |
function asset_widget($op, &$node, $field, &$items) {
|
| 213 |
switch ($op) {
|
| 214 |
case 'form':
|
| 215 |
$form = array();
|
| 216 |
$return_type = 'id';
|
| 217 |
$key = 'aid';
|
| 218 |
|
| 219 |
drupal_add_js(drupal_get_path('module', 'asset_wizard') .'/js/asset_wizard_cck.js');
|
| 220 |
|
| 221 |
$form[$field['field_name']] = array(
|
| 222 |
'#tree' => TRUE,
|
| 223 |
'#type' => 'fieldset',
|
| 224 |
'#title' => $field['widget']['label'],
|
| 225 |
'#description' => $field['widget']['description'],
|
| 226 |
'#weight' => $field['widget']['weight'],
|
| 227 |
'#attributes' => array('class' => $field['multiple'] ? 'asset-field-multiple' : 'asset-field'),
|
| 228 |
);
|
| 229 |
|
| 230 |
// Directory List Widget
|
| 231 |
if($field['widget']['type'] == 'directory') {
|
| 232 |
$directory_options = asset_directory_options();
|
| 233 |
// Multiple Directory List Widget
|
| 234 |
if ($field['multiple']) {
|
| 235 |
$delta = 0;
|
| 236 |
foreach ($items as $data) {
|
| 237 |
if ($data['aid']) {
|
| 238 |
$form[$field['field_name']][$delta]['aid'] = array(
|
| 239 |
'#type' => 'select',
|
| 240 |
'#default_value' => $data['aid'],
|
| 241 |
'#options' => $directory_options,
|
| 242 |
);
|
| 243 |
$delta++;
|
| 244 |
}
|
| 245 |
}
|
| 246 |
foreach (range($delta, $delta + 10) as $delta) {
|
| 247 |
$form[$field['field_name']][$delta]['aid'] = array(
|
| 248 |
'#type' => 'select',
|
| 249 |
'#default_value' => NULL,
|
| 250 |
'#options' => $directory_options,
|
| 251 |
);
|
| 252 |
}
|
| 253 |
}
|
| 254 |
// Single Directory List Widget
|
| 255 |
else {
|
| 256 |
$delta = 0;
|
| 257 |
$data = $items[$delta];
|
| 258 |
$form[$field['field_name']][$delta]['aid'] = array(
|
| 259 |
'#type' => 'select',
|
| 260 |
'#default_value' => $data['aid'],
|
| 261 |
'#options' => $directory_options,
|
| 262 |
);
|
| 263 |
}
|
| 264 |
}
|
| 265 |
// Asset Wizard Widget
|
| 266 |
else{
|
| 267 |
// Multiple Asset Wizard Widget
|
| 268 |
if ($field['multiple']) {
|
| 269 |
$delta = 0;
|
| 270 |
foreach ($items as $data) {
|
| 271 |
if ($data['aid']) {
|
| 272 |
$form[$field['field_name']][$delta]['aid'] = array(
|
| 273 |
'#type' => 'assetfield',
|
| 274 |
'#maxlength' => 255,
|
| 275 |
'#default_value' => $data['aid'],
|
| 276 |
'#asset_return_type' => $return_type,
|
| 277 |
'#asset_config' => $field['field_name'],
|
| 278 |
);
|
| 279 |
$delta++;
|
| 280 |
}
|
| 281 |
}
|
| 282 |
foreach (range($delta, $delta + 10) as $delta) {
|
| 283 |
$form[$field['field_name']][$delta]['aid'] = array(
|
| 284 |
'#type' => 'assetfield',
|
| 285 |
'#maxlength' => 255,
|
| 286 |
'#default_value' => NULL,
|
| 287 |
'#asset_return_type' => $return_type,
|
| 288 |
'#asset_config' => $field['field_name'],
|
| 289 |
);
|
| 290 |
}
|
| 291 |
}
|
| 292 |
// Single Asset Wizard Widget
|
| 293 |
else {
|
| 294 |
$delta = 0;
|
| 295 |
$data = $items[$delta];
|
| 296 |
$form[$field['field_name']][$delta]['aid'] = array(
|
| 297 |
'#type' => 'assetfield',
|
| 298 |
'#default_value' => $data['aid'],
|
| 299 |
'#attributes' => array('class' => 'asset-cck-'. $return_type),
|
| 300 |
'#asset_return_type' => $return_type,
|
| 301 |
'#asset_config' => $field['field_name'],
|
| 302 |
);
|
| 303 |
}
|
| 304 |
}
|
| 305 |
return $form;
|
| 306 |
|
| 307 |
case 'validate':
|
| 308 |
if ($field['required']) {
|
| 309 |
if (!count($items)) {
|
| 310 |
form_set_error($field['field_name'], $field['widget']['label'] .' is required.');
|
| 311 |
}
|
| 312 |
}
|
| 313 |
return;
|
| 314 |
|
| 315 |
case 'process form values':
|
| 316 |
// Don't save empty fields except the first value
|
| 317 |
foreach ($items as $delta => $item) {
|
| 318 |
if ($item['aid'] == '' && $delta > 0) {
|
| 319 |
unset($items[$delta]);
|
| 320 |
}
|
| 321 |
}
|
| 322 |
break;
|
| 323 |
}
|
| 324 |
}
|