gallery_assist.module in Gallery Assist 7
Same filename and directory in other branches
Extend drupal with gallery functionalities. Manage galleries.
File
gallery_assist.moduleView source
<?php
/**
* Version en la que float y align trabajan bien.
* En esta version hay que asegurar la configurasion de galerias antes de ver resultados.
*/
/**
* @file
* Extend drupal with gallery functionalities.
* Manage galleries.
*/
define('GALLERY_ASSIST_CLASSES', drupal_get_path('module', 'gallery_assist') . '/libs/classes/');
/**
* Implements hook_init();
*
* @todo: Optimation and centralization of the GA js code.
*/
function gallery_assist_init() {
global $user, $language;
gallery_assist_add_css('gallery_assist');
// gallery_assist_add_js('gallery_assist');
gallery_assist_add_js('gallery_assist_functions');
// gallery_assist_add_js('tests');
// Create the user gallery_assist directory if the user has the right permission.
$gallery_assist_paths[] = variable_get('gallery_assist_directory', FALSE);
$gallery_assist_paths[] = $user->uid;
if (file_prepare_directory($gallery_assist_paths[0], FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
$gallery_assist_user_upload_path = implode('/', $gallery_assist_paths);
if (!is_dir($gallery_assist_user_upload_path)) {
$create = 0;
$types = node_type_get_names();
foreach (array_keys($types) as $type) {
if (user_access('create gallery_assist content') || user_access('create gallery_assist_' . $type . ' content')) {
++$create;
}
}
if ($create > 0) {
if (file_prepare_directory($gallery_assist_user_upload_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
drupal_set_message(t("A gallery enviroment for user @user has been sucessfull created.", array(
'@user' => $user->name,
)));
watchdog('action', t("A gallery enviroment for user @user has been sucessfull created.", array(
'@user' => $user->name,
)));
}
else {
drupal_set_message(t("It was not possible to create the @user's gallery enviroment.", array(
'@user' => $user->name,
)));
watchdog('action', t("It was not possible to create the @user's gallery enviroment.", array(
'@user' => $user->name,
)));
}
}
}
}
}
function gallery_assist_check_access() {
return TRUE;
}
/**
* Implements hook_permission().
*/
function gallery_assist_permission() {
// Access permissions for the global configuration.
$access = array(
'administer gallery assist' => array(
'title' => t('Administer GA'),
'description' => t("Manage GA global settings and all galleries (su)."),
),
);
// Access permissions for content types with assigned gallery functionality.
$access_holder = array();
$types = node_type_get_names();
foreach ($types as $type => $name) {
if (variable_get("gallery_assist_{$type}") == 1) {
$access_holder[] = array(
"create {$type} galleries" => array(
'title' => t("<strong>Create GA content on {$name}</strong>"),
),
"view {$type} galleries" => array(
'title' => t("View GA content on {$name}"),
),
"edit own {$type} galleries" => array(
'title' => t("Edit own GA content on {$name}"),
'description' => t(""),
),
"edit any {$type} galleries" => array(
'title' => t("Edit any GA content on {$name}"),
),
"delete own {$type} galleries" => array(
'title' => t("Delete own GA content on {$name}"),
),
"delete any {$type} galleries" => array(
'title' => t("Delete any GA content on {$name}"),
),
);
}
}
for ($i = 0; $i < count($access_holder); ++$i) {
$access = array_merge($access, $access_holder[$i]);
}
return $access;
}
/**
* Implements hook_theme().
*/
function gallery_assist_theme() {
return array(
'gallery_assist_items_form' => array(
'render element' => 'form',
),
'gallery_assist_container' => array(
'variables' => array(
'node' => NULL,
),
'template' => 'gallery-assist-container',
'path' => drupal_get_path('module', 'gallery_assist') . '/templates',
),
);
}
//function gallery_assist_theme_registry_alter(&$theme_registry) {
// $theme_registry['gallery_assist_container'] = array(
// 'template' => drupal_get_path('module', 'gallery_assist').'/templates/gallery-assist-container',
// 'type' => 'module',
// );
// dsm('theme registered');
//}
function template_preprocess_gallery_assist_container(&$variables) {
// dsm('from template_preprocess');
}
function theme_gallery_assist_items_form($variables) {
$output = '';
$form = $variables['form'];
$output .= drupal_render($form['pager_top']);
$rows = array();
foreach ($form['gallery_items'] as $pid => $item) {
if (is_numeric($pid)) {
$row = array();
$row[] = array(
'data' => drupal_render($form['gallery_items'][$pid]['delete']),
'valign' => 'top',
);
$row[] = array(
'data' => drupal_render($form['gallery_items'][$pid]['image']),
'valign' => 'top',
'align' => 'center',
'class' => $form['gallery_items'][$pid]['cover']['#value'] ? 'ga-cover' : '',
);
$row[] = drupal_render($form['gallery_items'][$pid]['ptitle']) . drupal_render($form['gallery_items'][$pid]['palt']) . drupal_render($form['gallery_items'][$pid]['pdescription']);
$row[] = array(
'data' => drupal_render($form['gallery_items'][$pid]['cover']),
'valign' => 'top',
);
$rows[] = $row;
}
}
$header = array(
t('X'),
'',
t('Title'),
t('Y'),
);
$output .= theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array(
'id' => 'gallery-assist-items',
),
));
$output .= drupal_render_children($form);
return $output;
}
/**
* Get content types gallery settings and allow other modules to change or increase them.
*
* @param $type
* A string containing the requested content type or default if empty.
*
* @return
* An array with the GA settings.
*/
function gallery_assist_ct_settings($type = 'default') {
module_load_include('inc', 'gallery_assist', 'gallery_assist_variables');
$settings = variable_get("gallery_assist_{$type}_data", array());
drupal_alter('gallery_assist_ct_settings', $settings);
return $settings;
}
/**
* Implements hook_menu().
*/
function gallery_assist_menu() {
$items = array();
// Administration.
$items['admin/config/media/gallery_assist'] = array(
'title' => 'Gallery Assist',
'description' => 'Configure Gallery Assist.',
'page callback' => 'gallery_assist_info',
'access arguments' => array(
TRUE,
),
'file' => 'gallery_assist.admin.inc',
);
$items['admin/config/media/gallery_assist/info'] = array(
'title' => 'Info',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
$items['admin/config/media/gallery_assist/assignments'] = array(
'title' => 'Assignments',
'description' => 'Configure settings per node type with GA functionality (assignment)',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'gallery_assist_assignments_form',
),
'access callback' => TRUE,
'file' => 'gallery_assist.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
$i = 0;
foreach (node_type_get_names() as $type => $name) {
$menu_type = $i == 0 ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK;
if (variable_get("gallery_assist_{$type}") == 1) {
$items["admin/config/media/gallery_assist/assignments/{$type}"] = array(
'title' => $name,
'description' => "Administer the GA settings on {$name}.",
'page callback' => 'drupal_get_form',
'page arguments' => array(
'gallery_assist_assignments_form',
$type,
$name,
),
'access callback' => TRUE,
'type' => $menu_type,
'file' => 'gallery_assist.admin.inc',
'weight' => $i,
);
++$i;
}
}
$items['admin/config/media/gallery_assist/global'] = array(
'title' => 'Global',
'description' => 'Global Gallery Assist settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'gallery_assist_global_form',
),
'access callback' => TRUE,
'file' => 'gallery_assist.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
// Node edit.
$items['node/%node/gallery'] = array(
'title' => 'Gallery',
'description' => '',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'gallery_assist_gallery_node_form',
1,
2,
),
'access callback' => gallery_assist_check_access(1),
#'file' => 'gallery_assist.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
$items['node/%node/gallery/items'] = array(
'title' => 'Items',
'type' => MENU_DEFAULT_LOCAL_TASK,
'access callback' => gallery_assist_check_access(1),
'weight' => 1,
);
$items['node/%node/gallery/settings'] = array(
'title' => 'Settings',
'description' => '',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'gallery_assist_gallery_node_form',
1,
2,
3,
),
'access callback' => gallery_assist_check_access(1),
#'file' => 'gallery_assist.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
$items['node/%node/gallery/upload'] = array(
'title' => 'Upload',
'description' => '',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'gallery_assist_gallery_node_form',
1,
2,
3,
),
'access callback' => gallery_assist_check_access(1),
#'file' => 'gallery_assist.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 3,
);
$items["node/%node/%/edit"] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'gallery_assist_item_edit_form',
1,
2,
3,
),
'access callback' => TRUE,
);
$items["node/%node/cover/%"] = array(
'page callback' => 'gallery_assist_set_cover',
'page arguments' => array(
1,
1,
3,
2,
),
'access callback' => TRUE,
'file' => 'gallery_assist_form.inc',
);
$items["node/%node/%/delete"] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'gallery_assist_item_delete_confirm',
1,
2,
3,
),
'access callback' => TRUE,
);
// Gallery Item
// $items["node/%/view/%"] = array(
// 'page callback' => array('gallery_assist_item', 1, 2, 3),
// 'access callback' => TRUE,
// );
// Gallery Item
// $items["node/%/%"] = array(
// 'page callback' => array('gallery_assist_item', 1, 2, 3),
// 'access callback' => TRUE,
// );
################################################################################
$items["node/%node/item/%gallery_assist_item"] = array(
'title' => 'View',
'page callback' => 'gallery_assist_item',
'page arguments' => array(
1,
2,
3,
),
'access callback' => TRUE,
);
$items["node/%node/item/%gallery_assist_item/view"] = array(
'title' => 'View',
'page callback' => 'gallery_assist_item',
'page arguments' => array(
1,
2,
3,
),
'access callback' => TRUE,
#'access arguments' => array(TRUE),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
$items["node/%node/item/%gallery_assist_item/edit"] = array(
'title' => 'Edit',
#'page callback' => 'gallery_assist_item',
#'page arguments' => array(1,2,3),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'gallery_assist_item_edit_form',
1,
2,
3,
4,
),
'access callback' => FALSE,
#'access arguments' => array(TRUE),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
// return $items;
################################################################################
// Administration.
$items['admin/config/media/gallery_assist/austoben'] = array(
'title' => 'Austoben',
'description' => 'Configure Gallery Assist.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'gallery_assist_austoben',
),
'access arguments' => array(
TRUE,
),
'file' => 'gallery_assist.austoben.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 200,
);
return $items;
}
function gallery_assist_item_load($pid) {
$node = node_load(arg(1));
$gallery_assist_item = !empty($node->ga_items[$pid]) ? $node->ga_items[$pid] : NULL;
if ($pid && empty($gallery_assist_item)) {
$query = db_select('gallery_assist_item', 'p');
$query
->join('gallery_assist_translated', 't', 'p.pid = t.pid');
$query
->fields('p');
$query
->fields('t', array(
'lang',
'ptitle',
'palt',
'pdescription',
'format',
));
$query
->condition('p.pid', $pid);
$query
->condition('p.gref', $node->ga_gref);
$result = $query
->execute();
foreach ($result as $rec) {
$gallery_assist_item = $rec;
}
}
return $gallery_assist_item;
}
function gallery_assist_item($node, $scope, $item) {
drupal_add_css(drupal_get_path('module', 'gallery_assist') . '/css/gallery-assist.css', array(
'group' => 'gallery_assist',
'weight' => 1000,
));
$node->pid = $item->pid;
include_once GALLERY_ASSIST_CLASSES . 'GalleryAssistPager.inc';
$ga_pager = new GalleryAssistPager($node);
#$ga_pager->getPager();
// Set page title.
drupal_set_title($item->ptitle);
// Display post information only on certain node types.
if (variable_get('node_submitted_' . $node->type, TRUE)) {
#$variables['display_submitted'] = TRUE;
$submitted = t('Submitted by !username on !datetime', array(
'!username' => theme('username', array(
'account' => $node,
)),
'!datetime' => format_date($item->created),
));
$user_picture = theme_get_setting('toggle_node_user_picture') ? theme('user_picture', array(
'account' => $node,
)) : '';
}
else {
$variables['display_submitted'] = FALSE;
$variables['submitted'] = '';
$variables['user_picture'] = '';
}
$variables = array(
'path' => $item->opath,
'alt' => $item->palt,
'title' => $item->ptitle,
'getsize' => FALSE,
'style_name' => $node->ga_conf[$node->type]['preview_image_style'],
);
$preview_image = theme('image_style', $variables);
// Set breadcrumb.
$breadcrumb = drupal_get_breadcrumb();
#$breadcrumb[] = l($node->title,"node/{$node->nid}");
$breadcrumb[] = $item->ptitle;
drupal_set_breadcrumb($breadcrumb);
// Set public information.
// Prepare content array.
$myList = array();
$myTest[] = array(
'data' => 'UNO',
'class' => array(
'test',
),
);
$myTest[] = array(
'data' => 'DOS',
);
$myTest[] = array(
'data' => 'TRES',
);
$content = array(
'submmited' => array(
'#markup' => '<div class="meta submitted">' . (!empty($user_picture) ? $user_picture : '') . (!empty($submitted) ? $submitted : '') . '</div>',
),
'pager' => array(
'#prefix' => '<div class="ga-pager preview ' . $node->type . '">',
'#suffix' => '</div>',
'#markup' => $ga_pager
->getPager(),
),
'content' => array(
'#prefix' => '<div class="content">',
'#suffix' => '</div>',
'uno' => array(
'#markup' => '<div class="ga-preview-image">' . $preview_image . '</div>',
),
'dos' => array(
'#markup' => '<div class="ga-description">' . $item->pdescription . '</div>',
),
),
);
// Render content.
$return = drupal_render($content);
return $return;
}
function gallery_assist_set_cover($form_state, $node, $pid, $cover = FALSE) {
db_update('gallery_assist_item')
->fields(array(
'cover' => 0,
))
->condition('gid', $node->ga_gid)
->execute();
db_update('gallery_assist_item')
->fields(array(
'cover' => 1,
))
->condition('pid', $pid)
->execute();
// Redirect if from node view.
$arg = arg();
if ($arg[2] == 'cover') {
drupal_goto("node/{$node->nid}");
}
}
/**
* Menu callback -- ask for confirmation of node deletion
*/
function gallery_assist_item_delete_confirm($form = array(), $form_state = array(), $node = NULL, $pid = NULL, $action = NULL) {
$node = $form_state['build_info']['args'][0];
if (empty($pid)) {
$pid = $form_state['build_info']['args'][1];
}
$form['#node'] = $node;
// // Always provide entity id in the same form key as in the entity edit form.
// $form['nid'] = array('#type' => 'value', '#value' => $node->nid);
$form['pid'] = array(
'#type' => 'value',
'#value' => $node->ga_items[$pid]->pid,
);
$form['fid'] = array(
'#type' => 'value',
'#value' => $node->ga_items[$pid]->fid,
);
$form['gid'] = array(
'#type' => 'value',
'#value' => $node->ga_items[$pid]->gid,
);
$form['gref'] = array(
'#type' => 'value',
'#value' => $node->ga_items[$pid]->gref,
);
$form['nid'] = array(
'#type' => 'value',
'#value' => $node->ga_items[$pid]->nid,
);
$form['count'] = array(
'#type' => 'value',
'#value' => $node->ga_conf[$node->type]['gallcount'],
);
return confirm_form($form, t('Are you sure you want to delete %title?', array(
'%title' => $node->ga_items[$pid]->ptitle,
)), 'node/' . $node->nid . '/' . $pid . '/edit', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}
/**
* Execute node deletion
*/
function gallery_assist_item_delete_confirm_submit($form, &$form_state) {
$node = $form_state['build_info']['args'][0];
$pid = $form_state['build_info']['args'][1];
$prev = $next = array();
$query = db_select('gallery_assist_item', 'i');
$query
->condition('i.gid', $node->gid, '=')
->condition('i.pid', $pid, '<')
->fields('i', array(
'pid',
))
->orderBy('i.weight', 'DESC');
$result = $query
->execute();
foreach ($result as $rec) {
$prev[] = $rec->pid;
}
$query = db_select('gallery_assist_item', 'i');
$query
->condition('i.gid', $node->gid, '=')
->condition('i.pid', $pid, '>')
->fields('i', array(
'pid',
))
->orderBy('i.weight', 'ASC');
$result = $query
->execute();
foreach ($result as $rec) {
$next[] = $rec->pid;
}
$n = array_shift($next);
$p = array_shift($prev);
if (!empty($n)) {
$destination = array(
"node/{$node->nid}/{$n}/edit",
);
}
elseif (empty($n) && !empty($p)) {
$destination = array(
"node/{$node->nid}/{$p}/edit",
);
}
else {
$destination = array(
"node/{$node->nid}",
);
}
$form_state['redirect'] = $destination;
#$form_state['redirect'] = 'node/' . $form_state['values']['nid'] . '/' . $form_state['values']['pid'] . '/edit';
}
function gallery_assist_item_delete($x, $form_state, $node, $pid, $action) {
$form = array();
$form['image'] = array(
'#markup' => gallery_assist_build_image($node, $pid),
);
$form['message'] = array(
'#markup' => t('<p>The gallery item will be deleted. This action cannot be undone</p>'),
);
//@TODO
return $form;
}
function gallery_assist_item_delete_submit($form, &$form_state) {
}
function gallery_assist_build_image($node, $pid) {
$item = $node->ga_items[$pid];
$conf = $node->ga_conf[$node->type];
$style = image_style_load($conf['thumbnail_image_style']);
$end = end($style['effects']);
$width = $end['data']['width'];
$height = $end['data']['height'];
if ($end['name'] == 'image_scale_and_crop' || $end['name'] == 'image_crop' || $end['name'] == 'image_crop') {
$width = $end['data']['width'];
$height = $end['data']['height'];
}
$info = image_get_info($item->opath);
if ($info['width'] > $info['height']) {
$info['dir'] = 'h';
$info['new_width'] = $width;
$f = $width * 100 / $info['width'];
$info['new_height'] = $info['height'] * $f / 100;
$info['top'] = $height > $info['new_height'] ? ($height - $info['new_height']) / 2 : 0;
}
$variables = array(
'path' => $item->opath,
'alt' => $item->palt,
'title' => $item->ptitle,
'getsize' => FALSE,
'style_name' => $conf['thumbnail_image_style'],
);
if (isset($info['top']) && $info['top'] > 0) {
#$variables['attributes']['style'] = "margin-top:{$info['top']}px;";
}
$img = theme('image_style', $variables);
return $img;
}
function gallery_assist_item_edit_form($x, $form_state, $node, $scope, $item, $action) {
if (is_object($item)) {
$pid = $item->pid;
}
$prev = $next = array();
$query = db_select('gallery_assist_item', 'i');
$query
->condition('i.gid', $node->gid, '=')
->condition('i.pid', $pid, '<')
->fields('i', array(
'pid',
))
->orderBy('i.weight', 'DESC');
$result = $query
->execute();
foreach ($result as $rec) {
$prev[] = $rec->pid;
}
$query = db_select('gallery_assist_item', 'i');
$query
->condition('i.gid', $node->gid, '=')
->condition('i.pid', $pid, '>')
->fields('i', array(
'pid',
))
->orderBy('i.weight', 'ASC');
$result = $query
->execute();
foreach ($result as $rec) {
$next[] = $rec->pid;
}
$form = array();
$form['pid'] = array(
'#type' => 'hidden',
'#value' => $pid,
);
$form['gid'] = array(
'#type' => 'hidden',
'#value' => $node->gid,
);
$form['gref'] = array(
'#type' => 'hidden',
'#value' => $node->gref,
);
$form['fid'] = array(
'#type' => 'hidden',
#'#value' => $node->ga_items[$pid]->fid,
'#value' => $item->fid,
);
$form['count'] = array(
'#type' => 'hidden',
'#value' => $node->ga_conf[$node->type]['gallcount'],
);
$n = array_shift($next);
$p = array_shift($prev);
$form['prev'] = array(
'#type' => 'hidden',
'#value' => $p,
);
$form['next'] = array(
'#type' => 'hidden',
'#value' => $n,
);
$params = array();
#if (isset($_GET['page'])) {
$params = array(
'page' => 1,
);
#}
$prevlink = empty($p) ? 'previous' : l('previous', "node/{$node->nid}/item/{$p}/edit", array(
'query' => $params,
));
$nextlink = empty($n) ? 'next' : l('next', "node/{$node->nid}/item/{$n}/edit", array(
'query' => $params,
));
$form['navi'] = array(
'#markup' => '<div class="text-align-center">' . $prevlink . ' ' . $nextlink . '</div>',
);
$form['ptitle'] = array(
'#title' => t('Title'),
'#type' => 'textfield',
#'#default_value' => $node->ga_items[$pid]->ptitle,
'#default_value' => $item->ptitle,
);
$form['item'] = array(
'#markup' => gallery_assist_build_image($node, $pid),
);
$form['pdescription'] = array(
'#title' => t('Description'),
'#type' => 'textarea',
#'#default_value' => $node->ga_items[$pid]->pdescription,
'#default_value' => $item->pdescription,
);
$form['cover'] = array(
'#title' => t('Gallery cover image'),
'#type' => 'checkbox',
#'#default_value' => $node->ga_items[$pid]->cover == 1 ? TRUE : FALSE,
'#default_value' => $item->cover == 1 ? TRUE : FALSE,
);
$form['cover_is'] = array(
'#type' => 'hidden',
#'#value' => $node->ga_items[$pid]->cover == 1 ? TRUE : FALSE,
'#value' => $item->cover == 1 ? TRUE : FALSE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
// $form['delete'] = array(
// '#type' => 'submit',
// '#name' => 'delete',
// '#value' => t('Delete'),
// '#submit' => array('gallery_assist_item_delete_confirm'),
// );
return $form;
}
function gallery_assist_item_edit_form_confirm_submit($form, &$form_state) {
}
function gallery_assist_item_edit_form_submit($form, &$form_state) {
db_update('gallery_assist_translated')
->fields(array(
'ptitle' => $form_state['input']['ptitle'],
'pdescription' => $form_state['input']['pdescription'],
))
->condition('pid', $form_state['input']['pid'])
->condition('gid', $form_state['input']['gid'])
->execute();
db_update('gallery_assist_item')
->fields(array(
'cover' => $form_state['input']['cover'],
))
->condition('pid', $form_state['input']['pid'])
->execute();
// if ($form_state['input']['cover'] != $form_state['input']['cover_is']) {
// db_update('gallery_assist_item')
// ->fields(array(
// 'cover' => 0,
// ))
// ->condition('gid', $form_state['input']['gid'])
// ->execute();
// db_update('gallery_assist_item')
// ->fields(array(
// 'cover' => $form_state['input']['cover'],
// ))
// ->condition('pid', $form_state['input']['pid'])
// ->execute();
// }
if (isset($form_state['input']['delete'])) {
$node = $form_state['build_info']['args'][0];
if (!empty($form_state['input']['next'])) {
$destination = array(
"node/{$node->nid}/{$form_state['input']['next']}/edit",
);
}
elseif (empty($form_state['input']['next']) && !empty($form_state['input']['prev'])) {
$destination = array(
"node/{$node->nid}/{$form_state['input']['prev']}/edit",
);
}
else {
$destination = array(
"node/{$node->nid}",
);
}
$form_state['redirect'] = $destination;
unset($_GET['destination']);
drupal_static_reset('drupal_get_destination');
drupal_get_destination();
db_delete('gallery_assist_item')
->condition('pid', $form_state['input']['pid'])
->execute();
db_delete('gallery_assist_translated')
->condition('pid', $form_state['input']['pid'])
->execute();
db_delete('file_managed')
->condition('fid', $form_state['input']['fid'])
->execute();
db_delete('file_usage')
->condition('fid', $form_state['input']['fid'])
->execute();
db_update('gallery_assist')
->fields(array(
'count' => $form_state['input']['count'] - 1,
))
->condition('gref', $form_state['input']['gref'])
->execute();
drupal_set_message(t("@ptitle has been deleted", array(
'@ptitle' => $form_state['input']['ptitle'],
)));
}
else {
drupal_set_message(t("@ptitle has been updated", array(
'@ptitle' => $form_state['input']['ptitle'],
)));
}
}
function gallery_assist_gallery2_node_form($u, $form_state, $node, $op = '', $task = 'items') {
// dsm('$u => ');
// dsm($u);
// dsm('$form_state');
// dsm($form_state);
// dsm('$node');
// dsm($node);
// dsm('$op => ' . $op);
// dsm('$task => ' . $task);
// dsm('find page');
// dsm(pager_find_page());
// dsm(gallery_assist_get_items_count($node));
gallery_assist_load_node_items($node);
// pager_default_initialize(gallery_assist_get_items_count($node), $node->ga_conf[$node->type]['ga_limit']);
$pager = theme('pager', array(
'tags' => NULL,
'element' => 0,
));
$form = array();
$form['set'] = array(
'#type' => 'fieldset',
'#title' => 'Items List',
'#collapsible' => TRUE,
'#prefix' => $pager,
);
foreach ($node->mytest_items as $pid => $item) {
$form['set'][$pid]['#markup'] = '<div>' . $pid . '</div>';
}
return $form;
}
function gallery_assist_get_items_count($node) {
$query = "SELECT COUNT(*) amount FROM {gallery_assist_item} item " . "WHERE item.gref = :gref";
$result = db_query($query, array(
':gref' => $node->ga_gref,
))
->fetch();
return $result->amount;
}
function gallery_assist_load_node_items(&$node) {
// dsm('gallery_assist_load_node_items');
$node->cacao = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$total = $query = db_select('gallery_assist_item', 'p', array(
'fetch' => PDO::FETCH_ASSOC,
))
->extend('PagerDefault')
->element(0);
$query
->join('gallery_assist_translated', 't', 'p.pid = t.pid');
$query
->fields('p');
$query
->fields('t', array(
'lang',
'ptitle',
'palt',
'pdescription',
'format',
));
$query
->condition('p.gref', $node->ga_gref);
$query
->orderBy('p.cover', 'DESC');
$query
->orderBy('p.weight', 'ASC');
// if (is_numeric($node->ga_conf[$node->type]['ga_limit'])) {
// $query->limit($node->ga_conf[$node->type]['ga_limit']);
// }
$query
->limit(4);
// dsm('ITEMS LIST');
$items = array();
foreach ($query
->execute() as $item) {
$items[$item['pid']] = (object) $item;
// dsm($item['pid']);
}
$node->mytest_items = $items;
}
/**
* Form builder; GA settings node form builder.
*
* @param type $u
* @param $form_state
* Array containing the current state of the form.
* @param $node
* The node object.
* @param $op
* @param $task
* Strings containing necessary information to build the current form.
*
* @ingroup forms
*/
function gallery_assist_gallery_node_form($u, $form_state, $node, $op = '', $task = 'items') {
// dsm($node);
$test = module_invoke('gallery_assist', 'node_load', array(
$node->nid => $node,
), array(
$node->type,
));
// dsm($test);
//dsm($test);
// #$result = db_query('SELECT * FROM {gallery_assist} WHERE nid = :nid', array(':nid' => $node->nid));
//
// $q = db_select('gallery_assist', 'ga');
// $q->fields('ga')
// ->condition('nid', $node->nid);
// foreach ($q->execute() as $record) {
// $record->data = unserialize($record->data);
// dsm($record);
// }
$form = array();
module_load_include('inc', 'gallery_assist', 'gallery_assist_form');
$form['set'] = array(
'#type' => 'fieldset',
#'#title' => "<span title=".'"open all"'." class=".'"toogle-fieldsets status-closed ga-pointer"'.">[+]</span> " . t(ucfirst($task)),
'#title' => t(ucfirst($task)),
);
if ($task == 'items') {
$allowed = variable_get("gallery_assist_{$node->type}_allowed", array());
$form['set']['items'] = array();
$form['set']['items'] += gallery_assist_items_form($node, $allowed);
$submit = 'gallery_assist_items_form_submit';
}
if ($task == 'upload') {
$allowed = variable_get("gallery_assist_{$node->type}_allowed", array());
$form['set']['items'] = array();
$form['set']['items'] += gallery_assist_items_upload($node, $allowed);
$submit = 'gallery_assist_items_upload_submit';
}
if ($task == 'settings') {
$allowed = variable_get("gallery_assist_{$node->type}_allowed", array());
$form = array();
$form += gallery_assist_node_settings_form($node, $allowed);
$submit = 'gallery_assist_node_settings_form_submit';
}
$form['node_type'] = array(
'#type' => 'hidden',
'#value' => $node->type,
);
$form['node_nid'] = array(
'#type' => 'hidden',
'#value' => $node->nid,
);
$form['node_ref'] = array(
'#type' => 'hidden',
'#value' => $node->ga_ref,
);
$form['node_gid'] = array(
'#type' => 'hidden',
'#value' => $node->ga_gid,
);
$form['node_gref'] = array(
'#type' => 'hidden',
'#value' => $node->ga_gref,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
if (!empty($submit)) {
$form['submit']['#submit'][] = $submit;
}
return $form;
}
/**
* Implements hook_node_load().
*/
function gallery_assist_node_load($nodes, $types) {
$result = db_query('SELECT * FROM {gallery_assist} WHERE nid IN(:nids)', array(
':nids' => array_keys($nodes),
));
// dsm($nodes);
// Load gallery data.
foreach ($result as $x => $record) {
if (variable_get("gallery_assist_{$nodes[$record->nid]->type}", 0) == 1) {
$record->data = unserialize($record->data);
// Global settings
$settings = variable_get("gallery_assist_{$nodes[$record->nid]->type}_data", array());
// $settings += unserialize($record->data);
$node_settings = !empty($record->data) ? $record->data : array();
$settings = array_merge($settings, $node_settings);
$settings['ganame'] = $record->ganame;
$settings['in_profile'] = $record->in_profile;
$settings['show_title'] = $record->show_title;
$settings['show_in_homepage_block'] = $record->show_in_homepage_block;
$settings['gallery_assist_weight'] = $record->gallery_assist_weight;
// Fill node object
$nodes[$record->nid]->ga_gid = $record->gid;
$nodes[$record->nid]->ga_gref = $record->gref;
$nodes[$record->nid]->ga_ref = $record->ref;
$nodes[$record->nid]->ga_public_status = $record->ga_public_status;
$nodes[$record->nid]->ga_count = $record->count;
// GA items
$nodes[$record->nid]->ga_items = array();
$nodes[$record->nid]->ga_items_ids = array();
$settings['ga_teaser'] = count(arg()) >= 2 ? FALSE : TRUE;
$settings['ga_limit'] = $settings['page_items_per_row'] * $settings['page_rows_per_page'];
if ($settings['ga_teaser']) {
$settings['ga_limit'] = $settings['teaser_items_per_row'] * $settings['teaser_rows_per_page'];
}
// GA settings
$nodes[$record->nid]->ga_conf = array(
$nodes[$record->nid]->type => $settings,
);
// Avoid to print pager on teaser lists
if (count($nodes) == 1 && arg(1) == $record->nid) {
$query = db_select('gallery_assist_item', 'p', array(
'fetch' => PDO::FETCH_ASSOC,
))
->extend('PagerDefault')
->element(0);
}
else {
$query = db_select('gallery_assist_item', 'p', array(
'fetch' => PDO::FETCH_ASSOC,
));
}
$query
->join('gallery_assist_translated', 't', 'p.pid = t.pid');
$query
->fields('p');
$query
->fields('t', array(
'lang',
'ptitle',
'palt',
'pdescription',
'format',
));
$query
->condition('p.gref', $nodes[$record->nid]->ga_gref);
$query
->orderBy('p.cover', 'DESC');
$query
->orderBy('p.weight', $settings['db_order_direction']);
// Add the right syntax depending of pager using
if (count($nodes) == 1 && arg(1) == $record->nid) {
$query
->limit($settings['ga_limit']);
}
else {
$query
->range(0, $settings['ga_limit']);
}
foreach ($query
->execute() as $item) {
$item = (object) $item;
$nodes[$record->nid]->ga_items[$item->pid] = $item;
// $nodes[$record->nid]->ga_items_ids[$item->pid] = new stdClass();
// $nodes[$record->nid]->ga_items_ids[$item->pid]->pid = $item->pid;
// $nodes[$record->nid]->ga_items_ids[$item->pid]->weight = $item->weight;
}
$query = db_select('gallery_assist_item', 'p');
$query
->fields('p', array(
'pid',
'opath',
'weight',
));
$query
->condition('p.gref', $nodes[$record->nid]->ga_gref);
$query
->orderBy('p.cover', 'DESC');
$query
->orderBy('p.weight', $settings['db_order_direction']);
$nodes[$record->nid]->ga_items_ids = array();
foreach ($query
->execute() as $item) {
$nodes[$record->nid]->ga_items_ids[$item->pid] = new stdClass();
$nodes[$record->nid]->ga_items_ids[$item->pid]->pid = $item->pid;
$nodes[$record->nid]->ga_items_ids[$item->pid]->opath = $item->opath;
$nodes[$record->nid]->ga_items_ids[$item->pid]->weight = $item->weight;
}
}
}
}
/**
* Implements hook_form_alter().
*/
function gallery_assist_form_alter(&$form, &$form_state, $form_id) {
if (!empty($form['#node']) && gallery_assist_on($form['#node']->type)) {
}
}
/**
* Give information if a content type has assigned gallery functionality.
*
* @param $type
* A string containing the machine name of the content type should be checked.
* @return
* A bolean value.
*/
function gallery_assist_on($on) {
$type = is_object($on) ? $on->type : $on;
return variable_get("gallery_assist_{$type}", 0) == 1 ? TRUE : FALSE;
}
/**
* Implements hook_node_insert().
*/
function gallery_assist_node_insert($node) {
if (variable_get("gallery_assist_{$node->type}", 0) == 1) {
// Check entries to avoid duplicates.
$result = db_query("SELECT count(gid) count FROM {gallery_assist} WHERE nid = :nid", array(
':nid' => $node->nid,
));
$result = $result
->fetchAssoc();
if ($result['count'] > 0) {
return;
}
// Article settings.
$settings = variable_get("gallery_assist_{$node->type}_data", array());
$allowed = variable_get("gallery_assist_{$node->type}_allowed", array());
$settings = array(
'teaser_container_align' => $settings['teaser_container_align'],
'teaser_container_float' => $settings['teaser_container_float'],
'page_container_align' => $settings['page_container_align'],
'page_container_float' => $settings['page_container_float'],
'display_download_link' => $settings['display_download_link'],
'db_order_field' => $settings['db_order_field'],
'db_order_direction' => $settings['db_order_direction'],
'thm_linked_to' => $settings['thm_linked_to'],
'thm_link_type' => $settings['thm_link_type'],
'display_option' => $settings['display_option'],
'ga_shadow' => $settings['ga_shadow'],
);
$new_settings = serialize($settings);
// Insert.
$gid = db_insert('gallery_assist')
->fields(array(
'ref' => $node->nid,
'gref' => (int) 0,
'lang' => $node->language,
'nid' => $node->nid,
'uid' => $node->uid,
'ganame' => 'gallery_assist' . $node->nid,
'in_profile' => isset($settings['in_profile']) ? $settings['in_profile'] : 0,
'show_title' => isset($settings['show_title']) ? $settings['show_title'] : 0,
'show_in_homepage_block' => isset($settings['show_in_homepage_block']) ? $settings['show_in_homepage_block'] : 0,
'gallery_assist_weight' => isset($settings['gallery_assist_weight']) ? $settings['gallery_assist_weight'] : 0,
'data' => $new_settings,
))
->execute();
db_update('gallery_assist')
->fields(array(
'gref' => $gid,
))
->condition('gid', $gid)
->execute();
}
}
function gallery_assist_prepare_view(&$node) {
// Prepare data
$conf = $node->ga_conf[$node->type];
if (!empty($node->ga_items) && count($node->ga_items)) {
foreach ($node->ga_items as $item) {
$img = theme('image_style', array(
'style_name' => $conf['thumbnail_image_style'],
'path' => $item->opath,
'alt' => $item->palt,
'title' => $item->ptitle,
'attributes' => array(
'class' => array(
'ga-img',
),
),
));
$build = array(
'#markup' => $img,
'#prefix' => '<div class="ga-item"><div class="ga-item-outer' . ($item->cover == 1 ? ' ga-cover' : '') . '">',
'#suffix' => '</div></div>',
);
$node->ga_items[$item->pid]->img = l(drupal_render($build), "node/{$node->nid}/item/{$item->pid}", array(
'html' => TRUE,
'attributes' => array(
'title' => "view {$item->ptitle}",
),
));
$node->ga_items[$item->pid]->file_info = file_load($item->fid);
module_invoke_all('gallery_assist_item_alter', $node->ga_items[$item->pid]);
// dsm($node->ga_items[$item->pid]);
}
include_once GALLERY_ASSIST_CLASSES . 'GalleryAssistPager.inc';
$page_pager = new GalleryAssistPager($node);
$node->page_pager = $page_pager
->getPager(array(), 1);
}
}
function gallery_assist_gallery_assist_item_alter($element) {
// $element->hello = $element->palt;
}
function gallery_assist_node_view($node, $view_mode, $langcode) {
if (gallery_assist_on($node->type) && !empty($node->ga_items)) {
drupal_add_css(drupal_get_path('module', 'gallery_assist') . '/css/gallery-assist.css');
$settings = $node->ga_conf[$node->type];
gallery_assist_prepare_view($node);
$node->content['gallery_assist_container'] = array(
'#markup' => theme('gallery_assist_container', array(
'node' => $node,
)),
'#weight' => $settings['gallery_assist_weight'],
);
// foreach ($node->ga_items as $pid => $data) {
// list($width, $height) = getimagesize($node->ga_items[$pid]->opath , $imageinfo);
// $info = file_load($node->ga_items[$pid]->fid);
// dsm("$width x $height");
// dsm($info);
// }
// dsm($node);
}
}
/**
* Implements hook_node_view().
*
* @todo: Missing a solution for cases - h = w.
*/
function _gallery_assist_node_view($node, $view_mode, $langcode) {
// dsm($view_mode);
if (gallery_assist_on($node->type)) {
if (isset($node->ga_items) && count($node->ga_items) > 0) {
$conf = $node->ga_conf[$node->type];
$conf['view_mode'] = $view_mode;
$js_settings = array(
'galleryAssist' => array(
'settings' => array(
'view_mode' => $view_mode,
'teaser_items_per_row' => $conf['teaser_items_per_row'],
'page_items_per_row' => $conf['page_items_per_row'],
),
),
);
drupal_add_js($js_settings, 'setting');
$params = array();
if (isset($_GET['page'])) {
$params = array(
'page' => $_GET['page'],
);
}
$type_css = variable_get('gallery_assist_directory', FALSE) . '/css/gallery_assist.' . $node->type . '.css';
if (file_exists($type_css)) {
#drupal_add_css($type_css);
}
$style = image_style_load($conf['thumbnail_image_style']);
$end = end($style['effects']);
$width = $end['data']['width'];
$height = $end['data']['height'];
if ($end['name'] == 'image_scale_and_crop' || $end['name'] == 'image_crop' || $end['name'] == 'image_crop') {
$width = $end['data']['width'];
$height = $end['data']['height'];
}
$items = array();
foreach ($node->ga_items as $item) {
$info = image_get_info($item->opath);
if ($info['width'] > $info['height']) {
$info['dir'] = 'h';
$info['new_width'] = $width;
$f = $width * 100 / $info['width'];
$info['new_height'] = $info['height'] * $f / 100;
$info['top'] = $height > $info['new_height'] ? ($height - $info['new_height']) / 2 : 0;
}
$variables = array(
'path' => $item->opath,
'alt' => $item->palt,
'title' => $item->ptitle,
'getsize' => FALSE,
'style_name' => $conf['thumbnail_image_style'],
);
if (isset($info['top']) && $info['top'] > 0) {
$variables['attributes']['style'] = "margin-top:{$info['top']}px;";
}
$img = theme('image_style', $variables);
$ibox_classes = '';
if ($conf['ga_shadow'] == 1) {
$ibox_classes .= ' withshadow';
}
if ($item->cover == 1) {
$ibox_classes .= ' ga-cover';
}
$item->ptitle = strlen($item->ptitle) > 17 && $width <= 100 ? substr($item->ptitle, 0, 17) . '...' : $item->ptitle;
$ititles = $node->ga_conf[$node->type]['show_title'] == 0 ? ' hidden' : '';
$image = '<div class="ga-item ' . $node->type . '">';
$image .= '<div class="ga-item-links">';
$image .= l(t('edit'), "node/{$node->nid}/item/{$item->pid}/edit", array(
'attributes' => array(
'class' => array(
'ga-item-link',
),
),
));
//.'<a class="ga-item-link" href="">edit</a>';
$image .= l(t('delete'), "node/{$node->nid}/{$item->pid}/delete", array(
'attributes' => array(
'class' => array(
'ga-item-link',
),
),
));
$image .= l(t('cover'), "node/{$node->nid}/cover/{$item->pid}", array(
'attributes' => array(
'class' => array(
'ga-item-link',
),
),
));
$image .= '</div>';
$image .= '<div class="ga-item-box' . $ibox_classes . '">';
$image .= l('<div class="ga-image-box">' . $img . '</div>', "node/{$node->nid}/item/{$item->pid}", array(
'query' => $params,
'html' => TRUE,
'attributes' => array(),
));
// $image .= '<a href="#" style="display:block;">';
// $image .= '<div class="ga-image-box">'. $img .'</div>';
// $image .= '</a>';
$image .= '</div>';
$image .= '<div class="ga-item-title-box' . $ititles . '">' . $item->ptitle . '</div>';
$image .= '</div>';
$items[] = $image;
}
#drupal_add_css(variable_get('gallery_assist_directory', FALSE) ."/css/gallery_assist.{$node->type}.css");
$vm = $view_mode == 'teaser' ? $view_mode : 'page';
$attributes = array(
'outer' => array(
'class' => array(
'gallery-container-outer',
$node->type,
$view_mode,
$conf["{$vm}_container_float"] != 'none' ? 'f-' . $conf["{$vm}_container_float"] : '',
),
),
'inner' => array(
'class' => array(
'gallery-container-inner',
$node->type,
$view_mode,
$conf["{$vm}_container_align"],
),
'style' => 'max-width:608px;',
),
);
$container = array(
'<div ' . drupal_attributes($attributes['outer']) . '>',
'<div class="ga-links" style="display:none;">here the link</div>',
' <div ' . drupal_attributes($attributes['inner']) . '>',
implode('', $items),
' </div>',
'</div>',
);
if ($view_mode != 'teaser') {
$node->content['gallery_assist_pager'] = array(
'#markup' => theme('pager', array(
'tags' => NULL,
)),
'#weight' => $node->ga_conf[$node->type]['gallery_assist_weight'] * 1 - 0.5,
);
}
$node->content['gallery_assist_container'] = array(
'#markup' => implode('', $container),
'#weight' => $node->ga_conf[$node->type]['gallery_assist_weight'] * 1,
);
// switch ($view_mode) {
// case 'teaser':
// $js = array(
// 'galleryAssist' => array(
// 'teaser_items_per_row' => $conf['teaser_items_per_row'],
// )
// );
// drupal_add_js($js, 'settings');
// break;
// case 'full':
// $js = array(
// 'page_items_per_row' => $conf['page_items_per_row'],
// );
// break;
// }
}
}
}
/**
* Implements hook_admin_paths().
*/
function gallery_assist_admin_paths() {
$paths = array();
return array(
'*/*/gallery' => TRUE,
'*/*/gallery/settings' => TRUE,
'*/*/gallery/upload' => TRUE,
'*/*/gallery/sort' => TRUE,
'*/*/gallery/item/*' => TRUE,
);
}
/**
* Implements hook_image_default_styles().
*/
function gallery_assist_image_default_styles() {
$styles = array();
// Icon.
$styles['gallery_default_icon_75'] = array(
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => 75,
'height' => 75,
'upscale' => 1,
),
'weight' => 0,
),
),
);
// Thumbnail.
$styles['gallery_default_thumbnail_100'] = array(
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => 100,
'height' => 100,
'upscale' => 1,
),
'weight' => 0,
),
),
);
// Preview.
$styles['gallery_default_preview_550'] = array(
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => 550,
'height' => 550,
'upscale' => 0,
),
'weight' => 0,
),
),
);
return $styles;
}
/**
* Include GA .css files.
*
* @todo: ??? -> array('group' => CSS_THEME, 'browsers' => array('IE' => 'lt IE 7', '!IE' => FALSE), 'preprocess' => FALSE)
*/
function gallery_assist_add_css($file) {
$path = drupal_get_path('module', 'gallery_assist') . "/css/{$file}.css";
drupal_add_css($path);
}
/**
* Include GA .js files.
*/
function gallery_assist_add_js($file) {
drupal_add_js(drupal_get_path('module', 'gallery_assist') . "/js/{$file}.js");
}
/**
* Prepare an array for form items such as select field, multiple radios or checkboxes.
*
* @param $types
* Array containig the contet types which should are checked for assignation.
* @return
* An array containing compact assignments information.
*/
function gallery_assist_check_assignation($types) {
$types = node_type_get_names();
$result = array();
foreach ($types as $key => $value) {
$result[$key] = variable_get("gallery_assist_{$key}", 0) == 1 ? $key : 0;
}
return $result;
}
/**
* Implements hook_contextual_links_view_alter().
*/
function gallery_assist_contextual_links_view_alter(&$element, $items) {
if (isset($element['#contextual_links']) && array_key_exists('node', $element['#contextual_links'])) {
$nid = $element['#contextual_links']['node'][1][0];
$element['#links']['gallery'] = array(
'title' => 'Gallery',
'href' => 'node/' . $nid . '/gallery/settings',
);
}
}
Functions
Constants
Name![]() |
Description |
---|---|
GALLERY_ASSIST_CLASSES | @file Extend drupal with gallery functionalities. Manage galleries. |