2010-03-14 02:11:23 +00:00
< ? php
/*
* e107 website system
*
2011-12-10 00:00:15 +00:00
* Copyright ( C ) e107 Inc ( e107 . org )
2010-03-14 02:11:23 +00:00
* Released under the terms and conditions of the
* GNU General Public License ( http :// www . gnu . org / licenses / gpl . txt )
*
* Administration - Media Management Class
*
2011-12-10 00:00:15 +00:00
* $URL $
* $Id $
2010-03-14 02:11:23 +00:00
*
*/
if ( ! defined ( 'e107_INIT' )) { exit ; }
2011-12-10 00:00:15 +00:00
/**
* Subject of rewrite / rethinking after the pre - alpha
*/
2010-03-14 02:11:23 +00:00
class e_media
{
2011-12-10 00:00:15 +00:00
public $imagelist = array ();
2011-05-05 11:30:00 +00:00
2010-03-14 02:11:23 +00:00
/**
* Import files from specified path into media database .
* @ param string $cat Category nickname
* @ param string $epath path to file .
* @ param string $fmask [ optional ] filetypes eg . . jpg |. gif
2010-04-15 15:38:16 +00:00
* @ return e_media
2010-03-14 02:11:23 +00:00
*/
2012-04-26 01:33:33 +00:00
public function import ( $cat = '' , $epath , $fmask = '' )
2010-03-14 02:11:23 +00:00
{
2010-04-15 15:38:16 +00:00
if ( ! vartrue ( $cat )){ return $this ;}
2012-04-22 06:19:21 +00:00
2010-03-14 02:11:23 +00:00
if ( ! is_readable ( $epath ))
{
2010-04-15 15:38:16 +00:00
return $this ;
2010-03-14 02:11:23 +00:00
}
2012-04-22 06:19:21 +00:00
2010-03-14 02:11:23 +00:00
$fl = e107 :: getFile ();
$tp = e107 :: getParser ();
$sql = e107 :: getDb ();
$mes = e107 :: getMessage ();
$fl -> setFileInfo ( 'all' );
2010-04-15 15:38:16 +00:00
if ( ! $fmask )
{
$fmask = '[a-zA-z0-9_-]+\.(png|jpg|jpeg|gif|PNG|JPG|JPEG|GIF)$' ;
}
2010-03-14 02:11:23 +00:00
$img_array = $fl -> get_files ( $epath , $fmask , '' , 2 );
2010-04-15 15:38:16 +00:00
if ( ! count ( $img_array )){ return $this ;}
2010-03-14 02:11:23 +00:00
//print_a($img_array);
//return;
foreach ( $img_array as $f )
{
$fullpath = $tp -> createConstants ( $f [ 'path' ] . $f [ 'fname' ], 1 );
2012-04-26 01:33:33 +00:00
// echo "<br />cat=".$cat;
2010-03-14 02:11:23 +00:00
$insert = array (
'media_caption' => $f [ 'fname' ],
'media_description' => '' ,
'media_category' => $cat ,
'media_datestamp' => $f [ 'modified' ],
'media_url' => $fullpath ,
'media_userclass' => 0 ,
'media_name' => $f [ 'fname' ],
'media_author' => USERID ,
'media_size' => $f [ 'fsize' ],
'media_dimensions' => $f [ 'img-width' ] . " x " . $f [ 'img-height' ],
'media_usedby' => '' ,
'media_tags' => '' ,
'media_type' => $f [ 'mime' ]
);
if ( ! $sql -> db_Select ( 'core_media' , 'media_url' , " media_url = ' " . $fullpath . " ' LIMIT 1 " ))
{
2012-04-26 01:33:33 +00:00
2010-03-14 02:11:23 +00:00
if ( $sql -> db_Insert ( " core_media " , $insert ))
{
2010-04-15 15:38:16 +00:00
$mes -> addSuccess ( " Imported Media: " . $f [ 'fname' ]);
2010-03-14 02:11:23 +00:00
}
else
{
2010-04-15 15:38:16 +00:00
$mes -> addError ( " Media not imported: " . $f [ 'fname' ]);
2010-03-14 02:11:23 +00:00
}
}
}
2010-04-15 15:38:16 +00:00
return $this ;
2010-03-14 02:11:23 +00:00
}
/**
* Import icons into media - manager from specified path .
* @ param string $path
2010-04-15 15:38:16 +00:00
* @ return e_media
2010-03-14 02:11:23 +00:00
*/
public function importIcons ( $path )
{
$iconsrch = array ( 16 , 32 , 48 , 64 );
foreach ( $iconsrch as $size )
{
2010-04-15 15:38:16 +00:00
$types = '[a-zA-z0-9_-]+' . $size . '\.(png|PNG)$' ;
2010-03-14 02:11:23 +00:00
$this -> import ( '_icon_' . $size , $path , $types );
}
2010-04-15 15:38:16 +00:00
return $this ;
2010-03-14 02:11:23 +00:00
}
/**
* Remove Media from media table
* @ param string $cat [ optional ] remove a full category of media
* @ return
*/
function removeCat ( $cat )
{
$tp = e107 :: getParser ();
$sql = e107 :: getDb ();
$mes = e107 :: getMessage ();
if ( vartrue ( $cat ))
{
$status = ( $sql -> db_Delete ( 'core_media' , " media_cat = ' " . $cat . " ' " )) ? TRUE : FALSE ;
$mes -> add ( " Removing Media in Category: " . $cat , E_MESSAGE_DEBUG );
return $status ;
}
}
/**
* Remove Media from media table
* @ param string $epath remove media in the specified path .
* @ param string $type [ optional ] image | icon
* @ return
*/
function removePath ( $epath , $type = 'image' )
{
$tp = e107 :: getParser ();
$sql = e107 :: getDb ();
$mes = e107 :: getMessage ();
$qry = ( $type == 'icon' ) ? " AND media_category REGEXP '_icon_16|_icon_32|_icon_48|_icon_64' " : " AND NOT media_category REGEXP '_icon_16|_icon_32|_icon_48|_icon_64' " ;
if ( vartrue ( $epath ))
{
$path = $tp -> createConstants ( $epath , 'rel' );
$status = ( $sql -> db_Delete ( 'core_media' , " media_url LIKE ' " . $path . " %' " . $qry )) ? TRUE : FALSE ;
$message = ( $type == 'image' ) ? " Removing Media with path: " . $path : " Removing Icons with path: " . $path ;
$mes -> add ( $message , E_MESSAGE_DEBUG );
return $status ;
}
}
/**
* Return a list if icons in the specified path
* @ param string $epath
* @ return array
*/
function listIcons ( $epath )
{
if ( ! $epath ) return ;
$ret = array ();
$sql = e107 :: getDb ();
$tp = e107 :: getParser ();
$path = $tp -> createConstants ( $epath , 'rel' );
2012-04-26 05:41:24 +00:00
$status = ( $sql -> db_Select_gen ( " SELECT * FROM `#core_media` WHERE `media_url` LIKE ' " . $path . " %' AND media_category REGEXP '_icon_16|_icon_32|_icon_48|_icon_64' " )) ? TRUE : FALSE ;
2010-03-14 02:11:23 +00:00
while ( $row = $sql -> db_Fetch ())
{
$ret [] = $row [ 'media_url' ];
}
return $ret ;
}
2010-04-15 15:38:16 +00:00
/**
* Create media category .
* 'class' data is optional , 'id' key is ignored
*
* @ param array $data associative array , db keys should be passed without the leading 'media_cat_' e . g . 'class' , 'nick' , etc .
* @ return integer last inserted ID or false on error
*/
public function createCategory ( $data )
{
foreach ( $data as $k => $v )
{
$data [ 'media_cat_' . $k ] = $v ;
}
$data [ 'media_cat_id' ] = 0 ;
if ( ! isset ( $data [ 'media_cat_class' ]) || '' === $data [ 'media_cat_class' ]) $data [ 'media_cat_class' ] = defset ( 'e_UC_MEMBER' , 253 );
return e107 :: getDb () -> db_Insert ( 'core_media_cat' , $data );
}
/**
* Create multiple media categories in once
* @ param array $data
* @ return integer number of successfully inserted records
*/
public function createCategories ( $multi_data )
{
$cnt = 0 ;
foreach ( $multi_data as $cats )
{
if ( $this -> createCategory ( $cats )) $cnt ++ ;
}
return $cnt ;
}
public function deleteCategory ( $id )
{
// TODO
}
2011-05-05 11:30:00 +00:00
2011-05-10 12:47:03 +00:00
/**
* Return an Array of Media Categories
*/
2012-04-22 06:19:21 +00:00
public function getCategories ( $owner = '' )
2011-05-10 12:47:03 +00:00
{
$ret = array ();
2012-04-22 06:19:21 +00:00
$qry = " SELECT * FROM #core_media_cat " ;
$qry .= ( $owner ) ? " WHERE media_cat_owner = ' " . $owner . " ' " : " " ;
$qry .= " ORDER BY media_cat_order " ;
e107 :: getDb () -> db_Select_gen ( $qry );
2011-05-10 12:47:03 +00:00
while ( $row = e107 :: getDb () -> db_Fetch ( mySQL_ASSOC ))
{
2012-04-22 06:19:21 +00:00
$id = $row [ 'media_cat_category' ];
$ret [ $id ] = $row ;
2011-05-10 12:47:03 +00:00
}
return $ret ;
}
2012-04-24 08:36:35 +00:00
/**
* Return the total number of Images in a particular category
2012-04-28 01:31:30 +00:00
*
2012-04-24 08:36:35 +00:00
*/
public function countImages ( $cat )
{
$query = " SELECT media_id FROM #core_media WHERE media_category = ' " . $cat . " ' " ;
return e107 :: getDb () -> db_Select_gen ( $query );
}
2011-05-10 12:47:03 +00:00
/**
* Return an array of Images in a particular category
2012-04-28 01:31:30 +00:00
* @ param string $cat : category name . use + to include _common eg . 'news+'
2011-05-10 12:47:03 +00:00
*/
2012-04-28 01:31:30 +00:00
public function getImages ( $cat = '' , $from = 0 , $amount = null )
2011-05-10 12:47:03 +00:00
{
2012-04-28 01:31:30 +00:00
$inc = array ();
if ( strpos ( $cat , " + " ) || ! $cat )
{
$cat = str_replace ( " + " , " " , $cat );
$inc [] = " media_category = '_common' " ;
}
if ( $cat )
{
$inc [] = " media_category = ' " . $cat . " ' " ;
}
2011-05-10 12:47:03 +00:00
// TODO check the category is valid.
2012-04-28 01:31:30 +00:00
2011-05-10 12:47:03 +00:00
$ret = array ();
2012-04-28 01:31:30 +00:00
$query = " SELECT * FROM #core_media WHERE media_userclass IN ( " . USERCLASS_LIST . " ) AND ( " . implode ( " OR " , $inc ) ;
$query .= " ) ORDER BY media_datestamp DESC " ;
2012-04-24 08:36:35 +00:00
if ( $amount )
{
$query .= " LIMIT " . $from . " , " . $amount ;
}
e107 :: getDb () -> db_Select_gen ( $query );
2011-05-10 12:47:03 +00:00
while ( $row = e107 :: getDb () -> db_Fetch ( mySQL_ASSOC ))
{
$id = $row [ 'media_id' ];
$ret [ $id ] = $row ;
}
return $ret ;
}
2011-05-05 11:30:00 +00:00
/**
* Generate Simple Thumbnail window for image - selection
* TODO Use Whole - page popup window
* TODO Add an upload Tab ? .
* TODO Real - time ajax filter by keyword
*/
public function imageSelect ( $cat , $formid = 'imageSel' )
{
$sql = e107 :: getDb ();
$tp = e107 :: getParser ();
2011-05-07 07:16:45 +00:00
$text .= " <div style='margin-left:500px;text-align:center; position:relative;z-index:1000;float:left;display:none' id=' { $formid } '> " ;
2011-05-05 11:30:00 +00:00
$text .= " <div style='-moz-box-shadow: 3px 3px 3px #808080;
- webkit - box - shadow : 3 px 3 px 3 px #808080;
box - shadow : 3 px 3 px 3 px #808080;
background - color : black ; border : 1 px solid black ; position : absolute ; height : 200 px ; width : 205 px ; overflow - y : scroll ; bottom : 30 px ; right : 100 px ' > " ;
$total = ( $sql -> db_Select_gen ( " SELECT * FROM `#core_media` WHERE media_category = '_common' OR media_category = ' " . $cat . " ' ORDER BY media_category,media_datestamp DESC " )) ? TRUE : FALSE ;
2011-12-10 00:00:15 +00:00
$text .= " <div style='font-size:120%;font-weight:bold;text-align:right;margin-right:10px'><a title='Close' style='text-decoration:none;color:white' href='#' onclick= \" expandit(' { $formid } '); return false; \" >x</a></div> " ;
2011-05-05 11:30:00 +00:00
while ( $row = $sql -> db_Fetch ())
{
$image = $row [ 'media_url' ];
$diz = $row [ 'media_name' ] . " : " . $row [ 'media_dimensions' ];
2011-05-07 07:16:45 +00:00
$insert = " [img] " . $image . " [/img] " ;
2011-05-05 11:30:00 +00:00
$text .= "
< div style = 'border:1px solid silver;margin:5px;width:50px;height:50px;overflow:hidden;float:left' >
2011-12-10 00:00:15 +00:00
< a title = \ " " . $diz . " \" href='#' onclick= \" addtext(' " . $insert . " ', true); expandit(' { $formid } '); return false; \" >
< img src = '".e107::getParser()->thumbUrl($image, ' w = 100 ', true)."' alt = \ " " . $diz . " \" style='width: 50px' />
2011-05-05 11:30:00 +00:00
</ a >
</ div > " ;
}
$text .= " </div></div> " ;
return $text ;
}
2010-03-14 02:11:23 +00:00
2012-04-28 01:31:30 +00:00
public function mediaSelect ( $cat = '' , $tagid = null , $att = null )
{
2012-04-29 11:15:44 +00:00
$onclick = null ; // option to override onclick behavior. See ibrowser.php
2012-04-28 01:31:30 +00:00
2012-04-29 11:15:44 +00:00
$cat = ( $cat ) ? $cat . " + " : " " ; // the '+' loads category '_common' as well as the chosen category.
2012-04-29 10:38:45 +00:00
2012-04-29 11:15:44 +00:00
parse_str ( $att ); // grab 'onclick' .
2012-04-28 01:31:30 +00:00
2012-04-29 11:15:44 +00:00
// $total_images = $this->getImages($cat); // for use by next/prev in filter at some point.
$images = $this -> getImages ( $cat , 0 , 30 );
$att = 'aw=120&ah=100' ;
$prevId = $tagid . " _prev " ;
// EXAMPLE of FILTER GUI.
2012-04-29 10:38:45 +00:00
2012-04-29 11:15:44 +00:00
// This filter should run independently of admin_ui so that it can also be utilized by ibrowser.php
$text .= " <div>Filter: <input type='text' name='non-working-filter-example' value='' /> " ;
$text .= " <input type='button' value='Go' /> " ; // Manual filter, if onkeyup ajax fails for some reason.
$text .= " <input type='button' value='«' /> " ; // see previous page of images.
$text .= " <input type='button' value='»' /> " ; // see next page of images.
$text .= " Displaying 0-30 of 150 images.<br /> </div>
< div > \n " ;
2012-04-29 10:38:45 +00:00
2012-04-29 08:22:17 +00:00
2012-04-29 11:15:44 +00:00
if ( $onclick == null ) // e107 Media Manager Only. TinyMce doesn't need it.
{
$onclick_clear = " parent.document.getElementById(' { $tagid } ').value = '';
2012-04-29 08:22:17 +00:00
parent . document . getElementById ( '".$prevId."' ) . src = '".e_IMAGE_ABS."generic/blank.gif' ;
parent . e107Widgets . DialogManagerDefault . getWindow ( 'e-dialog' ) . close ();
2012-04-29 10:38:45 +00:00
return false ; " ;
2012-04-29 11:15:44 +00:00
$text .= " <a class='media-select-clear' style='float:left' href='#' onclick= \" { $onclick_clear } \" >
< div style = 'display:block;border:1px solid silver;padding-top:40px;text-align:center;vertical-align:middle;width:120px;height:58px' >
No Image </ div > " ;
}
2012-04-29 08:22:17 +00:00
2012-04-29 11:15:44 +00:00
2012-04-29 10:38:45 +00:00
$srch = array ( " { MEDIA_URL} " , " { MEDIA_PATH} " );
2012-04-28 01:31:30 +00:00
foreach ( $images as $im )
{
2012-04-29 11:15:44 +00:00
$media_path = e107 :: getParser () -> replaceConstants ( $im [ 'media_url' ], 'full' );
$realPath = e107 :: getParser () -> thumbUrl ( $im [ 'media_url' ], $att );
$diz = e107 :: getParser () -> toAttribute ( $im [ 'media_title' ]);
$repl = array ( $im [ 'media_url' ], $media_path );
2012-04-29 10:38:45 +00:00
2012-04-29 11:15:44 +00:00
if ( $onclick == null ) // e107 Media Manager
2012-04-29 10:38:45 +00:00
{
$onclicki = " parent.document.getElementById(' { $tagid } ').value = ' { $im [ 'media_url' ] } ';
parent . document . getElementById ( '".$prevId."' ) . src = '{$realPath}' ;
parent . e107Widgets . DialogManagerDefault . getWindow ( 'e-dialog' ) . close ();
return false ; " ;
}
2012-04-29 11:15:44 +00:00
else // TinyMce and other applications.
2012-04-29 10:38:45 +00:00
{
$onclicki = str_replace ( $srch , $repl , $onclick );
}
$text .= " <a class='media-select' title= \" " . $diz . " \" href='#' onclick= \" { $onclicki } \" > \n " ;
$text .= " <img src=' " . e107 :: getParser () -> thumbUrl ( $im [ 'media_url' ], $att ) . " ' alt= \" " . $im [ 'media_title' ] . " \" /> \n " ;
$text .= " </a> \n \n " ;
2012-04-28 01:31:30 +00:00
}
2012-04-29 10:38:45 +00:00
2012-04-29 11:15:44 +00:00
$text .= " </div> " ;
2012-04-28 01:31:30 +00:00
return $text ;
}
2010-03-14 02:11:23 +00:00
}