) // modified gallery_1.module from // http://drupal.org/project/comments/add/45159 // to comply with Gallery 2.1 // see http://fisheye.gallery2.org/viewrep/gallery/gallery2/upgrade/index.php // revision 1.23.2.1 notes /** * Implementation of hook_help */ function gallery_help($section) { switch($section) { case 'admin/modules#description': return t('Integration with Gallery2.1 (%gallery_url)', array('%gallery_url' => 'http://gallery.menalto.com/')); } } /** * Implementation of hook_menu */ function gallery_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array('path' => 'gallery', 'title' => t('gallery'), 'callback' => 'gallery_page', 'access' => user_access('access gallery'), 'type' => MENU_NORMAL_ITEM); } return $items; } /** * Implementation of hook_perm(). */ function gallery_perm() { return array('access gallery'); } /** * Implementation of hook_settings */ function gallery_settings() { $form['gallery_dir'] = array( '#type' => 'textfield', '#title' => t('Location of Gallery2'), '#default_value' => variable_get('gallery_dir', 'gallery2/'), '#description' => t('Path to your gallery2 directory, relative to the root directory of your drupal installation. Please include a trailing slash ("/").'), ); $form['gallery_drupal_dir'] = array( '#type' => 'textfield', '#title' => t('Location of Drupal'), '#default_value' => variable_get('gallery_drupal_dir', '/'), '#description' => t('Path to your drupal directory, relative to the root of your website. Please include a trailing slash ("/").'), ); $form['gallery_error_mode'] = array( '#type' => 'checkboxes', '#title' => t('Error logging'), '#default_value' => variable_get('gallery_error_mode', array(1)), '#options' => array(1 => t('Watchdog'), 2 => t('Output to the browser')), '#description' => t('Choose where errors are displayed'), ); return $form; } /** * Implementation of hook_user */ function gallery_user($op, &$edit, &$user, $category = NULL) { switch ($op) { case 'login': /* _gallery_init() will try to create the user, if necessary */ list ($success, $ret) = _gallery_init(); if (!$success) { gallery_error(t('Unable to log in to Gallery'), $ret); return; } break; case 'insert': list ($success, $ret) = _gallery_init(); if (!$success) { gallery_error(t('Unable to initialize embedded Gallery'), $ret); return; } $ret = GalleryEmbed::createUser($user->uid, array('username' => $user->name, 'email' => $user->mail, 'fullname' => $user->name, 'language' => $user->language, 'hashedpassword' => $user->pass, 'hashmethod' => 'md5' )); if ($ret) { gallery_error(t('Error creating Gallery user'), $ret); return; } GalleryEmbed::done(); break; case 'update': list ($success, $ret) = _gallery_init(); if (!$success) { gallery_error(t('Unable to initialize embedded Gallery'), $ret); return; } // on update we can't be sure how much info $edit will contain. $name = ($edit['name']) ? $edit['name'] : $user->name; $language = ($edit['language']) ? $edit['language'] : $user->language; $pass = ($edit['pass']) ? md5($edit['pass']) : $user->pass; $email = ($edit['email']) ? $edit['mail'] : $user->mail; $ret = GalleryEmbed::updateUser($user->uid, array('username' => $name, 'fullname' => $name, 'email' => $mail, 'language' => $language, 'hashedpassword' => $pass, 'hashmethod' => 'md5')); if ($ret) { // try to create user then. $ret = GalleryEmbed::createUser($user->uid, array('username' => $name, 'fullname' => $name, 'email' => $mail, 'language' => $language, 'hashedpassword' => $pass, 'hashmethod' => 'md5' )); if ($ret) { gallery_error(t('Error updating Gallery user'), $ret); return; } } GalleryEmbed::done(); break; case 'delete': list ($success, $ret) = _gallery_init(); if (!$success) { gallery_error(t('Unable to initialize embedded Gallery'), $ret); return; } $ret = GalleryEmbed::deleteUser($user->uid); if ($ret) { gallery_error(t('Error deleting Gallery user'), $ret); } GalleryEmbed::done(); break; } } /** * Implementation of hook_block * * - gallery navigation block (recommended) * - gallery imageblock (random, most viewed, etc) */ function gallery_block($op = 'list', $delta = 0, $edit = array()) { $typeMap = array('randomImage' => t('Random Image'), 'recentImage' => t('Newest Image'), 'viewedImage' => t('Most Viewed Image'), 'randomAlbum' => t('Random Album'), 'recentAlbum' => t('Newest Album'), 'viewedAlbum' => t('Most Viewed Album'), 'dailyImage' => t('Picture of the Day'), 'weeklyImage' => t('Picture of the Week'), 'monthlyImage' => t('Picture of the Month'), 'dailyAlbum' => t('Album of the Day'), 'weeklyAlbum' => t('Album of the Week'), 'monthlyAlbum' => t('Album of the Month')); switch ($op) { case 'list': $blocks[0]['info'] = t('Gallery Block'); return $blocks; case 'configure': $form['gallery_block_block_'. $delta] = array( '#type' => 'checkboxes', '#title' => t('Image type'), '#default_value' => variable_get('gallery_block_block_' . $delta, array('randomImage')), '#options' => $typeMap, '#description' => t("Pick the type of image you'd like to see"), ); $form['gallery_block_show_'. $delta] = array( '#type' => 'checkboxes', '#title' => t('Image data'), '#default_value' => variable_get('gallery_block_show_' . $delta, array('title', 'heading')), '#options' => array('title' => 'Title', 'date' => 'Date', 'views' => 'View Count', 'owner' => 'Item owner', 'heading' => 'Heading', 'fullSize' => 'Full Size'), '#description' => t("Choose the item metadata you'd like to display") ); return $form; case 'save': // if no image types selected, assume randomImage if (count($edit['gallery_block_block_' . $delta])) { variable_set('gallery_block_block_' . $delta, $edit['gallery_block_block_' . $delta]); } else { variable_set('gallery_block_block_' . $delta, array('randomImage')); } variable_set('gallery_block_show_' . $delta, $edit['gallery_block_show_' . $delta]); break; case 'view': list ($success, $ret) = _gallery_init(true); if (!$success) { gallery_error(t('Unable to initialize embedded Gallery'), $ret); return; } $params['blocks'] = implode('|', variable_get('gallery_block_block_' . $delta, array('randomImage'))); $params['show'] = implode('|', variable_get('gallery_block_show_' . $delta, array())); // TODO: parameterize this $params['maxSize'] = 150; $block = array(); list($ret, $content, $head) = GalleryEmbed::getImageBlock($params); if ($ret) { gallery_error(t('Unable to get Gallery image block'), $ret); return; } else { if ($content) { if (count(variable_get('gallery_block_block_' . $delta, 'randomImage')) > 1) { $block['subject'] = t('Gallery'); } else { $block['subject'] = $typeMap[$params['blocks']]; } $block['content'] = '
'. $content . '
'; } if ($head) { drupal_set_html_head($head); } } $ret = GalleryEmbed::done(); if ($ret) { gallery_error(t('Unable to complete Gallery request'), $ret); return; } return $block; } } /** * implementation of hook_search */ function gallery_search($op = 'search', $keys = null) { switch ($op) { case 'name': return t('gallery'); case 'search': $find = array(); list ($success, $ret) = _gallery_init(true); if (!$success) { gallery_error(t('Unable to initialize embedded Gallery'), $ret); return; } list ($ret, $results) = GalleryEmbed::searchScan($keys, 20); if (!$ret) { $urlGenerator =& $GLOBALS['gallery']->getUrlGenerator(); foreach ($results as $name => $module) { if (count($module['results']) > 0) { foreach ($module['results'] as $result) { $excerpt = array(); foreach ($result['fields'] as $field) { $words = search_keywords_split($keys); foreach ($words as $word) { if (preg_match("/$word/", $field['value'])) { $excerpt[] = $field['key'] .': '.search_excerpt($keys, $field['value']); } } } $find[] = array('title' => $result['fields'][0]['value'], 'link' => $urlGenerator->generateUrl(array('itemId' => $result['itemId'])), 'type' => $module['name'], 'snippet' => implode('
', $excerpt)); } } } } return $find; } } /** * Main gallery display page */ function gallery_page() { list ($success, $ret) = _gallery_init(true); if (!$success) { gallery_error(t('Unable to initialize embedded Gallery'), $ret); print theme('page', 'You need to configure your embedded Gallery'); return; } /* Pass any excess path info to G2 */ $path = substr($_GET['q'], 7 /* length of 'gallery/' */); if (!empty($path)) { $_GET[GALLERY_FORM_VARIABLE_PREFIX . 'path'] = $path; } $result = GalleryEmbed::handleRequest(); if (!$result['isDone']) { list($title, $css, $javascript) = GalleryEmbed::parseHead($result['headHtml']); if (!empty($javascript)) { drupal_set_html_head(implode("\n",$javascript)); } drupal_set_html_head(implode("\n",$css)); drupal_set_title($title); print theme('page', $result['bodyHtml']); } } /** * fetch a galleryEmbed object */ function _gallery_init($full = false) { global $user, $base_url; $galleryDir = variable_get('gallery_dir', 'gallery2/'); $embedUri = $base_url . '/index.php?q=gallery'; //$embedUri = $base_url . '/gallery'; //possible with clean url enabled $g2Uri = '/'.$galleryDir; require_once($galleryDir . 'embed.php'); if ($user->uid==0 && module_exist('locale')) { $result = db_query('SELECT locale, name FROM {locales_meta} WHERE isdefault = 1'); $row = db_fetch_object($result); $lang = $row->locale; } else { $lang = $user->language; if(empty($lang)) { $result = db_query('SELECT locale, name FROM {locales_meta} WHERE isdefault = 1'); $row = db_fetch_object($result); $lang = $row->locale; } } $params = array('embedUri' => $embedUri, 'g2Uri' => $g2Uri, 'loginRedirect' => url('user/login'), 'activeUserId' => $user->uid, 'activeLanguage' => $lang, 'fullInit' => $full); $ret = GalleryEmbed::init($params); if ($ret) { if ($ret->getErrorCode() & ERROR_MISSING_OBJECT) { // Our user mapping is missing. Make a mapping, or create a new user. $g2_user = null; // Get the G2 user that matches the Drupal username list ($ret, $g2_user) = GalleryCoreApi::fetchUserByUsername($user->name); if ($ret) { if ($ret && !($ret->getErrorCode() & ERROR_MISSING_OBJECT)) { return array(false, $ret); } } if (!isset($g2_user)) { // No G2 user with a matching username. If this is the admin user, we're going to // try a little harder and match it to the oldest admin in G2. if ($user->uid == 1) { list ($ret, $admin_group_id) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup'); if ($ret) { return array(false, $ret); } list ($ret, $g2_users) = GalleryCoreApi::fetchUsersForGroup($admin_group_id); if ($ret) { return array(false, $ret); } $keys = array_keys($g2_users); $g2_user_name = $users[$keys[0]]; list ($ret, $g2_user) = GalleryCoreApi::fetchUserByUsername($g2_user_name); if ($ret) { return array(false, $ret); } } } if (isset($g2_user)) { $ret = GalleryEmbed::addExternalIdMapEntry($user->uid, $g2_user->getId(), 'GalleryUser'); if ($ret) { return array(false, $ret); } } else { // No matching G2 user found -- create one. $ret = GalleryEmbed::createUser($user->uid, array('username' => $user->name, 'email' => $user->mail, 'fullname' => $user->name, 'language' => $user->language, 'hashedpassword' => $user->pass, 'hashmethod' => 'md5')); if ($ret) { return array(false, $ret); } } } } return array(true, null); } function gallery_error($message, $ret) { $error_mode = variable_get('gallery_error_mode', array()); if (in_array(2, $error_mode)) { drupal_set_message($message); } if (isset($ret)) { $full_message = $message . '
' . $ret->getAsHtml(); } else { $full_message = $message; } if (in_array(1, $error_mode)) { watchdog('gallery', $full_message, WATCHDOG_ERROR); } } ?>