View source
<?php
define('ICON_ADMIN_PATH', 'admin/config/media/icon');
$includes = array(
'bundles',
'cache',
'element',
'render',
'theme',
'utilities',
);
foreach ($includes as $include) {
require_once dirname(__FILE__) . "/includes/{$include}.inc";
}
function icon_hook_info() {
$hooks = array();
$icon_hooks = array(
'icon_bundles',
'icon_bundle_delete',
'icon_bundle_list_alter',
'icon_permission',
'icon_providers',
'icon_render_hooks',
);
foreach ($icon_hooks as $hook) {
$hooks[$hook] = array(
'group' => 'icon',
);
}
return $hooks;
}
function icon_library() {
$libaries_path = drupal_get_path('module', 'icon') . '/libraries';
$libraries['icon_selector'] = array(
'title' => 'Icon Selector',
'website' => 'http://drupal.org/project/icon',
'version' => '1.0.0',
'css' => array(
$libaries_path . '/icon_selector/css/icon_selector.css' => array(),
),
'js' => array(
$libaries_path . '/icon_selector/js/icon_selector.js' => array(),
0 => array(
'type' => 'setting',
'data' => array(
'icon_selector' => array(
'bundles' => array_keys(icon_bundles()),
),
),
),
),
);
return $libraries;
}
function icon_menu() {
$module_path = drupal_get_path('module', 'icon');
$items = array();
$items[ICON_ADMIN_PATH] = array(
'title' => 'Icons',
'description' => 'Overview of all available icons.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'icon_bundle_overview_form',
),
'access arguments' => array(
'administer icons',
),
'file' => 'admin.inc',
'file path' => $module_path . '/includes',
);
$items[ICON_ADMIN_PATH . '/overview'] = array(
'type' => MENU_DEFAULT_LOCAL_TASK,
'title' => 'Bundles',
'description' => 'Overview of all available icon bundles.',
'weight' => -10,
);
$import_providers = icon_providers_support_import();
if (!empty($import_providers)) {
$items[ICON_ADMIN_PATH . '/import'] = array(
'type' => MENU_LOCAL_TASK,
'title' => 'Import',
'description' => 'Import a bundle from a provider.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'icon_provider_import_form',
),
'access arguments' => array(
'administer icons',
),
'file' => 'import.inc',
'file path' => $module_path . '/includes',
);
}
$items[ICON_ADMIN_PATH . '/bundle/%icon_bundle'] = array(
'title callback' => 'icon_bundle_get_title',
'title arguments' => array(
5,
),
'description' => 'An icon bundle.',
'page callback' => 'icon_bundle_list',
'page arguments' => array(
5,
),
'access arguments' => array(
'administer icons',
),
'theme callback' => 'icon_bundle_get_theme',
'theme arguments' => array(
5,
),
'file' => 'admin.inc',
'file path' => $module_path . '/includes',
);
$items[ICON_ADMIN_PATH . '/bundle/%icon_bundle/icons'] = array(
'type' => MENU_DEFAULT_LOCAL_TASK,
'title' => 'Icons',
'description' => 'Provide an overview of all available icon bundles.',
'weight' => -10,
);
$items[ICON_ADMIN_PATH . '/bundle/%icon_bundle/configure'] = array(
'type' => MENU_LOCAL_TASK,
'title' => 'Configure',
'description' => 'Form callback for configuring an icon bundle.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'icon_bundle_configure_form',
5,
),
'access arguments' => array(
'administer icons',
),
'file' => 'admin.inc',
'file path' => $module_path . '/includes',
);
$items[ICON_ADMIN_PATH . '/bundle/%icon_bundle/delete'] = array(
'title' => 'Delete',
'description' => 'Confirmation page for deleting an icon bundle from the database.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'icon_bundle_delete_form',
5,
),
'type' => MENU_CALLBACK,
'access arguments' => array(
'administer icons',
),
'file' => 'admin.inc',
'file path' => $module_path . '/includes',
);
$items[ICON_ADMIN_PATH . '/bundle/%icon_bundle/reset'] = array(
'title' => 'Reset',
'description' => 'Confirmation page for resetting a module or theme provided bundle that has been overridden in the database.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'icon_bundle_reset_form',
5,
),
'type' => MENU_CALLBACK,
'access arguments' => array(
'administer icons',
),
'file' => 'admin.inc',
'file path' => $module_path . '/includes',
);
$view_path_alias = ICON_ADMIN_PATH . '/tmp';
$items[$view_path_alias] = $items[ICON_ADMIN_PATH];
$items[$view_path_alias]['access arguments'] = array(
'view icons',
);
$items[$view_path_alias . '/%icon_bundle'] = $items[ICON_ADMIN_PATH . '/bundle/%icon_bundle'];
$items[$view_path_alias . '/%icon_bundle']['access arguments'] = array(
'view icons',
);
$items[$view_path_alias . '/%icon_bundle']['title arguments'] = array(
1,
);
$items[$view_path_alias . '/%icon_bundle']['page arguments'] = array(
1,
);
$items[$view_path_alias . '/%icon_bundle']['theme arguments'] = array(
1,
);
return $items;
}
function icon_menu_alter(&$items) {
$view_path_alias = variable_get('icon_api_view_path_alias');
if ($view_path_alias === NULL && empty($items['icons'])) {
$view_path_alias = 'icons';
variable_set('icon_api_view_path_alias', $view_path_alias);
}
$tmp_path = ICON_ADMIN_PATH . '/tmp';
if (!empty($view_path_alias)) {
$items[$view_path_alias] = $items[$tmp_path];
$items[$view_path_alias . '/%icon_bundle'] = $items[$tmp_path . '/%icon_bundle'];
}
unset($items[$tmp_path]);
unset($items[$tmp_path . '/%icon_bundle']);
}
function icon_permission() {
return array_merge_recursive(array(
'administer icons' => array(
'title' => t('Administer Icons'),
'description' => t('Grants selected roles full administrative permissions for all aspects of the Icon API. It supersedes all permissions below.'),
'restrict access' => TRUE,
),
'view icons' => array(
'title' => t('View Icons'),
'description' => t('Grants selected roles permission to view all icons in enabled bundles.'),
),
), module_invoke_all('icon_permission'));
}
function &icon_render_hooks($hook = NULL, $reset = FALSE) {
$hooks =& drupal_static(__FUNCTION__);
if (!isset($hooks) || $reset) {
if (!$reset && ($cache = cache_get('icon_render_hooks')) && !empty($cache->data)) {
$hooks = $cache->data;
}
else {
$hooks = array();
foreach (icon_extension_implements('icon_render_hooks') as $extension => $type) {
$extension_hooks = (array) icon_extension_invoke($type, $extension, 'icon_render_hooks');
foreach ($extension_hooks as $render_hook => $data) {
if (!is_string($render_hook) && is_string($data)) {
$render_hook = $data;
$data = array();
}
$data['name'] = $render_hook;
$data['type'] = $type;
$data[$type] = $extension;
if (!isset($data['file'])) {
$data['file'] = 'module' === $type ? $extension . '.module' : 'template.php';
}
if (!isset($data['path'])) {
$data['path'] = drupal_get_path($type, $extension);
}
$hooks[$render_hook] = $data;
}
}
drupal_alter('icon_render_hooks', $hooks);
cache_set('icon_render_hooks', $hooks);
}
}
if (isset($hook)) {
if (!empty($hooks[$hook])) {
return $hooks[$hook];
}
else {
$false = FALSE;
return $false;
}
}
return $hooks;
}
function icon_bundle_defaults(&$bundle = array(), $name = '') {
$bundle += array(
'icons' => array(),
'name' => $name,
'provider' => '',
'settings' => array(),
'status' => 1,
'title' => $name,
'version' => '',
'#attached' => array(),
);
return $bundle;
}
function icon_bundles($name = NULL, $reset = FALSE) {
$bundles =& drupal_static(__FUNCTION__);
if (!isset($bundles) || $reset) {
if (!$reset && ($cache = cache_get('icon_bundles')) && !empty($cache->data)) {
$bundles = $cache->data;
}
else {
$bundles = array();
foreach (icon_extension_implements('icon_bundles') as $extension => $type) {
$extension_bundles = (array) icon_extension_invoke($type, $extension, 'icon_bundles');
foreach ($extension_bundles as $bundle_name => $bundle) {
icon_bundle_defaults($bundle, $bundle_name);
if (empty($bundle['provider'])) {
$bundle['provider'] = $extension;
}
if (!empty($bundle['icons'])) {
ksort($bundle['icons']);
}
if ($type === 'theme' && $bundle['render'] === 'sprite' && empty($bundle['#attached']['css'])) {
$bundle['theme'] = $extension;
}
$bundles[$bundle_name] = $bundle;
}
}
$database_bundles = db_select('icon_bundle', 'b')
->fields('b')
->execute();
foreach ($database_bundles as $database_bundle) {
$bundle = unserialize($database_bundle->bundle);
if ($bundle === FALSE) {
$bundle = array();
}
else {
icon_bundle_defaults($bundle, $bundle['name']);
if (!empty($bundles[$database_bundle->name])) {
$bundle['overridden'] = TRUE;
}
}
$bundle['database'] = TRUE;
$bundle['status'] = (int) $database_bundle->status;
if (isset($bundles[$database_bundle->name])) {
$bundle = array_merge($bundles[$database_bundle->name], $bundle);
}
$bundles[$database_bundle->name] = $bundle;
}
drupal_alter('icon_bundles', $bundles);
cache_set('icon_bundles', $bundles);
}
}
if (isset($name)) {
if (!empty($bundles[$name])) {
return $bundles[$name];
}
else {
$false = FALSE;
return $false;
}
}
return $bundles;
}
function icon_bundle_delete(array $bundle = array()) {
if (empty($bundle['name']) || !icon_bundle_load($bundle['name'])) {
return FALSE;
}
try {
db_delete('icon_bundle')
->condition('name', $bundle['name'])
->execute();
if (empty($bundle['overridden']) && strpos($bundle['path'], 'public://') === 0 && file_exists($bundle['path'])) {
file_unmanaged_delete_recursive($bundle['path']);
}
$hook = 'icon_bundle_' . (!empty($bundle['overridden']) ? t('reset') : t('delete'));
foreach (icon_extension_implements($hook) as $extension => $type) {
icon_extension_invoke($type, $extension, $hook, $bundle);
}
icon_clear_all_caches();
drupal_set_message(t('The icon bundle %bundle has been successfully !action.', array(
'!action' => !empty($bundle['overridden']) ? t('reset') : t('deleted'),
'%bundle' => $bundle['title'],
)));
return TRUE;
} catch (Exception $e) {
drupal_set_message(t('An error occurred while attempting to !action the icon bundle "%bundle":<br /><code>@message</code>', array(
'!action' => !empty($bundle['overridden']) ? t('reset') : t('delete'),
'%bundle' => $bundle['title'],
'@message' => $e
->getMessage(),
)), 'error');
}
return FALSE;
}
function icon_bundle_disable(array $bundle = array()) {
$bundle['status'] = 0;
if (icon_bundle_save($bundle)) {
drupal_set_message(t('The icon bundle %bundle has been disabled.', array(
'%bundle' => $bundle['title'],
)));
icon_clear_all_caches();
return TRUE;
}
drupal_set_message(t('An error occurred while attemping to disable the icon bundle: %bundle.', array(
'%bundle' => $bundle['title'],
)), 'error');
return FALSE;
}
function icon_bundle_enable(array $bundle = array()) {
$bundle['status'] = 1;
if (icon_bundle_save($bundle)) {
drupal_set_message(t('The icon bundle %bundle has been enabled.', array(
'%bundle' => $bundle['title'],
)));
icon_clear_all_caches();
return TRUE;
}
drupal_set_message(t('An error occurred while attemping to enable the icon bundle: %bundle.', array(
'%bundle' => $bundle['title'],
)), 'error');
return FALSE;
}
function icon_bundle_get_theme($bundle, $global = FALSE) {
global $theme;
return !empty($bundle['theme']) && !$global ? $bundle['theme'] : $theme;
}
function icon_bundle_get_title($bundle) {
return !empty($bundle['title']) ? $bundle['title'] : t('Bundle');
}
function icon_bundle_load($name) {
$loaded =& drupal_static(__FUNCTION__, array());
if (empty($loaded[$name])) {
$loaded[$name] = icon_bundles($name);
}
return $loaded[$name];
}
function icon_bundle_save(array $bundle = array()) {
if (empty($bundle['name'])) {
return FALSE;
}
drupal_alter('icon_bundle_save', $bundle);
$primary_keys = array();
$record = array(
'name' => $bundle['name'],
);
$existing_bundle = icon_bundle_load($bundle['name']);
if (!$existing_bundle) {
$existing_bundle = array();
}
if (!empty($existing_bundle['database'])) {
$primary_keys[] = 'name';
}
$diff = icon_array_diff_recursive($existing_bundle, $bundle);
if (isset($diff['status'])) {
$record['status'] = (int) $bundle['status'];
unset($diff['status']);
}
if (!empty($diff)) {
$record['bundle'] = $bundle;
}
if (isset($record['status']) || isset($record['bundle'])) {
if ($status = drupal_write_record('icon_bundle', $record, $primary_keys)) {
icon_clear_all_caches();
return $status;
}
}
return FALSE;
}
function icon_provider_defaults(&$provider = array(), $name = '') {
$provider += array(
'default bundle' => array(),
'name' => $name,
'title' => $name,
'url' => '',
);
return $provider;
}
function icon_providers($name = NULL, $reset = FALSE) {
$providers =& drupal_static(__FUNCTION__);
if (!isset($providers) || $reset) {
if (!$reset && ($cache = cache_get('icon_providers')) && !empty($cache->data)) {
$providers = $cache->data;
}
else {
$providers = array();
foreach (icon_extension_implements('icon_providers') as $extension => $type) {
$extension_providers = (array) icon_extension_invoke($type, $extension, 'icon_providers');
foreach ($extension_providers as $provider_name => $provider) {
if ($provider_name === 'automatic') {
drupal_set_message(t('The !type %extension tried to specify a provider with the name: %name. This is a reserved name and cannot be used, please rename your provider.', array(
'!type' => $type,
'%extension' => $extension,
'%name' => $provider_name,
)), 'warning');
continue;
}
icon_provider_defaults($provider, $provider_name);
$provider['name'] = $provider_name;
$provider['type'] = $type;
$provider[$type] = $extension;
$providers[$provider_name] = $provider;
}
}
drupal_alter('icon_providers', $providers);
cache_set('icon_providers', $providers);
}
}
if (!empty($name)) {
if (!empty($providers[$name])) {
return $providers[$name];
}
return FALSE;
}
return $providers;
}
function icon_providers_support_import($name = NULL) {
$providers =& drupal_static(__FUNCTION__);
if (!isset($providers)) {
$providers = array();
foreach (icon_providers() as $provider) {
if (icon_extension_hook($provider['type'], $provider[$provider['type']], 'icon_' . $provider['name'] . '_import_validate') && icon_extension_hook($provider['type'], $provider[$provider['type']], 'icon_' . $provider['name'] . '_import_process')) {
$providers[$provider['name']] = $provider;
}
}
}
if (!empty($name)) {
if (!empty($providers[$name])) {
return $providers[$name];
}
return FALSE;
}
return $providers;
}
function icon_provider_load($name) {
$loaded =& drupal_static(__FUNCTION__, array());
if (empty($loaded[$name])) {
if (($cache = cache_get($name, 'cache_icon_providers')) && !empty($cache->data)) {
$loaded[$name] = $cache->data;
}
else {
$provider = icon_providers($name);
cache_set($name, $provider, 'cache_icon_providers');
$loaded[$name] = $provider;
}
}
return $loaded[$name];
}
function icon_wrapper_options() {
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['options'] =& drupal_static(__FUNCTION__);
}
$options =& $drupal_static_fast['options'];
if (!isset($options)) {
$options = array(
'' => t('None'),
'div' => 'Div',
'span' => 'Span',
'h2' => 'H2',
'h3' => 'H3',
'h4' => 'H4',
'h5' => 'H5',
'h6' => 'H6',
'p' => 'P',
'em' => 'Em',
'strong' => 'Strong',
);
drupal_alter(__FUNCTION__, $options);
}
return $options;
}