use one instance specific image_map to translate between image-names and URLs, which can be transfered to the browser for etemplate2 (next commit)

This commit is contained in:
Ralf Becker 2011-08-30 22:03:17 +00:00
parent 131a032da6
commit a048840e44

View File

@ -635,6 +635,8 @@ class common
* *
* this is just a workaround for idots, better to use find_image, which has a fallback \ * this is just a workaround for idots, better to use find_image, which has a fallback \
* on a per image basis to the default dir * on a per image basis to the default dir
*
* @deprecated
*/ */
static function is_image_dir($dir) static function is_image_dir($dir)
{ {
@ -662,6 +664,7 @@ class common
* get image dir of an application * get image dir of an application
* *
* @param $appname application name optional can be derived from $GLOBALS['egw_info']['flags']['currentapp']; * @param $appname application name optional can be derived from $GLOBALS['egw_info']['flags']['currentapp'];
* @deprecated
*/ */
static function get_image_dir($appname = '') static function get_image_dir($appname = '')
{ {
@ -701,6 +704,7 @@ class common
* get image path of an application * get image path of an application
* *
* @param $appname appication name optional can be derived from $GLOBALS['egw_info']['flags']['currentapp']; * @param $appname appication name optional can be derived from $GLOBALS['egw_info']['flags']['currentapp'];
* @deprecated
*/ */
static function get_image_path($appname = '') static function get_image_path($appname = '')
{ {
@ -734,205 +738,157 @@ class common
} }
/** /**
* Searches and image by a given search order (it maintains a cache of the existing images): * Searches a appname, template and maybe language and type-specific image
* - image dir of the application for the given template
* - image dir of the application for the default template
* - image dir of the API for the given template
* - image dir of the API for the default template
* *
* @param string $appname * @param string $appname
* @param string $image * @param string|array $image one or more image-name in order of precedence
* @return string url of the image * @return string url of image or null if not found
*/ */
static function find_image($appname,$image) static function image($app,$image)
{ {
if ($appname == 'phpgwapi') static $image_map;
{ if (is_null($image_map)) $image_map = self::image_map();
$imagedir = $GLOBALS['egw']->framework->template_dir.'/images';
}
else
{
$imagedir = '/'.$appname.'/templates/'.$GLOBALS['egw_info']['user']['preferences']['common']['template_set'].'/images';
}
$vfs_imagedir = $GLOBALS['egw_info']['server']['vfs_image_dir'];
if (!isset(self::$found_files[$appname])) // array of images in descending precedence
if (is_array($image))
{ {
$imagedir_olddefault = '/'.$appname.'/templates/default/images'; foreach($image as $img)
$imagedir_default = '/'.$appname.'/templates/idots/images'; {
if (($url = self::image($app, $img)))
{
return $url;
}
}
//error_log(__METHOD__."('$app', ".array2string($image).") NONE found!");
return null;
}
if (file_exists(EGW_INCLUDE_ROOT.$imagedir_olddefault) && ($d = dir(EGW_INCLUDE_ROOT.$imagedir_olddefault))) $webserver_url = $GLOBALS['egw_info']['server']['webserver_url'];
{
while (($entry = $d->read())) // instance specific images have highest precedence
{ if (isset($image_map['vfs'][$image]))
if ($entry[0] != '.')
{
self::$found_files[$appname][$entry] = $imagedir_olddefault;
}
}
$d->close();
}
if (file_exists(EGW_INCLUDE_ROOT.$imagedir_default) && ($d = dir(EGW_INCLUDE_ROOT.$imagedir_default)))
{
while (($entry = $d->read()))
{
if ($entry[0] != '.')
{
self::$found_files[$appname][$entry] = $imagedir_default;
}
}
$d->close();
}
if (file_exists(EGW_INCLUDE_ROOT.$imagedir) && ($d = dir(EGW_INCLUDE_ROOT.$imagedir)))
{
while (($entry = $d->read()))
{
if ($entry[0] != '.')
{
self::$found_files[$appname][$entry] = $imagedir;
}
}
$d->close();
}
//echo $appname; _debug_array(self::$found_files[$appname]);
}
if (!isset(self::$found_files['vfs']))
{ {
self::$found_files['vfs'] = array(); // so it get's scaned only once return $webserver_url.$image_map['vfs'][$image];
if (egw_vfs::file_exists($vfs_imagedir) &&
egw_vfs::is_dir($vfs_imagedir) && ($d = egw_vfs::opendir($vfs_imagedir)))
{
while (($entry = readdir($d)) !== false)
{
if (!egw_vfs::is_dir($vfs_imagedir.'/'.$entry))
{
list($type,$subtype) = explode('/',egw_vfs::mime_content_type($vfs_imagedir.'/'.$entry));
if ($type == 'image')
{
self::$found_files['vfs'][$entry] = $vfs_imagedir;
}
}
}
closedir($d);
//echo 'vfs'; _debug_array(self::$found_files['vfs']);
}
} }
// then app specific ones
if(isset($image_map[$app][$image]))
{
return $webserver_url.$image_map[$app][$image];
}
// then api
if(isset($image_map['phpgwapi'][$image]))
{
return $webserver_url.$image_map['phpgwapi'][$image];
}
// if image not found, check if it has an extension and try withoug
if (strpos($image, '.') !== false)
{
self::get_extension($image, $name);
return self::image($app, $name);
}
//error_log(__METHOD__."('$app', '$image') image NOT found!");
return null;
}
/**
* Scan filesystem for images of all apps
*
* For each application and image-name (without extension) one full path is returned.
* The path takes template-set and image-type-priority ($GLOBALS['egw_info']['server']['image_type']) into account.
*
* VFS image directory is treated like an application named 'vfs'.
*
* @param string $template_set=null 'default', 'idots', 'jerryr', default is template-set from user prefs
* @return array of application => image-name => full path
*/
static function image_map($template_set=null)
{
if (is_null($image_map))
{
$template_set = $GLOBALS['egw_info']['user']['preferences']['common']['template_set'];
}
if (($map = egw_cache::getInstance(__CLASS__, 'image_map_'.$template_set)))
{
return $map;
}
$starttime = microtime(true);
if (!$GLOBALS['egw_info']['server']['image_type']) if (!$GLOBALS['egw_info']['server']['image_type'])
{ {
// priority: GIF->JPG->PNG // priority: GIF->JPG->PNG
$img_type = array('.gif','.jpg','.png',''); $img_types = array('gif','jpg','png','ico');
} }
else else
{ {
// priority: : PNG->JPG->GIF // priority: : PNG->JPG->GIF
$img_type = array('.png','.jpg','.gif',''); $img_types = array('png','jpg','gif','ico');
} }
$imgfile = ''; $map = array();
// first look in the instance specific image dir in vfs foreach($GLOBALS['egw_info']['apps'] as $app => $data)
foreach($img_type as $type)
{ {
// first look in the instance specific image dir in vfs $app_map =& $map[$app];
if(isset(self::$found_files['vfs'][$image.$type])) $app_map = array();
$imagedirs = array();
if ($app == 'phpgwapi')
{ {
$imgfile = egw::link(egw_vfs::download_url(self::$found_files['vfs'][$image.$type].'/'.$image.$type)); $imagedir = $GLOBALS['egw']->framework->template_dir.'/images';
break;
} }
// then look in the selected template dir else
if(self::$found_files[$appname][$image.$type] == $imagedir)
{ {
$imgfile = $GLOBALS['egw_info']['server']['webserver_url'].self::$found_files[$appname][$image.$type].'/'.$image.$type; $imagedir = '/'.$app.'/templates/'.$template_set.'/images';
break;
} }
//then look everywhere else if ($template_set != 'idots') $imagedirs[] = '/'.$app.'/templates/idots/images';
if (isset(self::$found_files[$appname][$image.$type])) $imagedirs[] = '/'.$app.'/templates/default/images';
{
$imgfile = $GLOBALS['egw_info']['server']['webserver_url'].self::$found_files[$appname][$image.$type].'/'.$image.$type;
break;
}
}
if (empty($imgfile)) foreach($imagedirs as $imagedir)
{
// searching the image in the api-dirs
if (!isset(self::$found_files['phpgwapi']))
{ {
self::find_image('phpgwapi',''); if (!file_exists($dir = EGW_SERVER_ROOT.$imagedir) || !is_readable($dir)) continue;
}
foreach($img_type as $type) foreach(scandir($dir) as $img)
{
if(isset(self::$found_files['phpgwapi'][$image.$type]))
{ {
$imgfile = $GLOBALS['egw_info']['server']['webserver_url'].self::$found_files['phpgwapi'][$image.$type].'/'.$image.$type; if ($img[0] == '.' || !in_array($ext = self::get_extension($img, $name), $img_types) || empty($name)) continue;
break;
if (!isset($app_map[$name]) || array_search($ext, $img_types) < array_search(self::get_extension($app_map[$name]), $img_types))
{
$app_map[$name] = $imagedir.'/'.$img;
}
} }
} }
} }
//echo "<p>".__METHOD__."($appname,$image) = $imgfile</p>\n"; $app_map =& $map['vfs'];
return $imgfile; $app_map = array();
if (($dir = $GLOBALS['egw_info']['server']['vfs_image_dir']) && egw_vfs::file_exists($dir) && egw_vfs::is_readable($dir))
{
foreach(egw_vfs::find($dir) as $img)
{
if ($img[0] == '.' || !in_array($ext = self::get_extension($img, $name), $img_types) || empty($name)) continue;
if (!isset($app_map[$name]) || array_search($ext, $img_types) < array_search(self::get_extension($app_map[$name]), $img_types))
{
$app_map[$name] = egw_vfs::download_url($dir.'/'.$img);
}
}
}
//error_log(__METHOD__."('$template_set') took ".(microtime(true)-$starttime).' secs');
egw_cache::setInstance(__CLASS__, 'image_map_'.$template_set, $map, 86400); // cache for one day
return $map;
} }
/** /**
* Searches a appname, template and maybe language and type-specific image * Get extension (and optional basename without extension) of a given path
* *
* @param string $appname * @param string $path
* @param string $image * @param string &$name on return basename without extension
* @param string $ext * @return string extension without dot, eg. 'php'
* @param boolean $use_lang
* @return string url of the image
*/ */
static function image($appname,$image='',$ext='',$use_lang=True) public static function get_extension($path, &$name=null)
{ {
static $cache; // do some caching in the request $parts = explode('.', basename($path));
$ext = array_pop($parts);
$image_found =& $cache[$appname.implode('-',(array)$image).$ext.$use_lang]; $name = implode('.', $parts);
return $ext;
if (!isset($image_found))
{
if (!is_array($image))
{
if (empty($image))
{
return '';
}
}
if ($use_lang)
{
foreach((array)$image as $img)
{
$lang_images[] = $img . '_' . $GLOBALS['egw_info']['user']['preferences']['common']['lang'];
$lang_images[] = $img;
}
$image = $lang_images;
}
foreach((array)$image as $img)
{
if (($image_found = self::find_image($appname,$img.$ext))) break;
}
}
//else $cache_hit = ' *** CACHE ***';
//echo '<p>'.__METHOD__."($appname,".array2string($image).",$ext,$use_lang) = ".array2string($image_found)." $cache_hit</p>\n";
return $image_found;
}
/**
* Searches an image of a given type, if not found also without type/extension
*
* @param string $appname
* @param string $image
* @param string $extension
* @return string url of the image
*/
static function image_on($appname,$image,$extension='_on')
{
if (($with_extension = self::image($appname,$image,$extension)))
{
return $with_extension;
}
if(($without_extension = self::image($appname,$image)))
{
return $without_extension;
}
return '';
} }
/** /**