2015-08-24 20:22:07 +00:00
< ? php
/*----------------------------------------------------------------------------------| www . vdm . io |----/
Vast Development Method
/-------------------------------------------------------------------------------------------------------/
2015-12-29 19:39:38 +00:00
@ version 1.0 . 5
2016-02-21 00:07:38 +00:00
@ build 21 st February , 2016
2015-12-03 02:17:44 +00:00
@ created 5 th August , 2015
2015-08-24 20:22:07 +00:00
@ package Demo
@ subpackage demo . php
@ author Llewellyn van der Merwe < https :// www . vdm . io />
@ copyright Copyright ( C ) 2015. All Rights Reserved
@ license GNU / GPL Version 2 or later - http :// www . gnu . org / licenses / gpl - 2.0 . html
____ _____ _____ __ __ __ __ ___ _____ __ __ ____ _____ _ _ ____ _ _ ____
( _ _ )( _ )( _ )( \ / )( ) / __\ / __ )( _ )( \ / )( _ \ ( _ )( \ ( )( ___ )( \ ( )( _ _ )
.- _ )( )( _ )( )( _ )( ) ( )( __ / ( __ ) \ ( ( __ )( _ )( ) ( ) ___ / )( _ )( ) ( ) __ ) ) ( )(
\____ ) ( _____ )( _____ )( _ / \ / \_ )( ____ )( __ )( __ ) \___ )( _____ )( _ / \ / \_ )( __ ) ( _____ )( _ ) \_ )( ____ )( _ ) \_ ) ( __ )
/------------------------------------------------------------------------------------------------------*/
// No direct access to this file
defined ( '_JEXEC' ) or die ( 'Restricted access' );
/**
* Demo component helper .
*/
abstract class DemoHelper
2015-12-03 02:17:44 +00:00
{
2015-08-24 20:22:07 +00:00
/**
2015-12-03 02:17:44 +00:00
* Load the Component xml manifest .
2015-08-24 20:22:07 +00:00
**/
2015-12-03 02:17:44 +00:00
public static function manifest ()
{
$manifestUrl = JPATH_ADMINISTRATOR . " /components/com_demo/demo.xml " ;
return simplexml_load_file ( $manifestUrl );
}
/**
* Load the Contributors details .
**/
public static function getContributors ()
{
// get params
$params = JComponentHelper :: getParams ( 'com_demo' );
// start contributors array
$contributors = array ();
// get all Contributors (max 20)
$searchArray = range ( '0' , '20' );
foreach ( $searchArray as $nr )
{
if (( NULL !== $params -> get ( " showContributor " . $nr )) && ( $params -> get ( " showContributor " . $nr ) == 1 || $params -> get ( " showContributor " . $nr ) == 3 ))
{
// set link based of selected option
if ( $params -> get ( " useContributor " . $nr ) == 1 )
{
$link_front = '<a href="mailto:' . $params -> get ( " emailContributor " . $nr ) . '" target="_blank">' ;
$link_back = '</a>' ;
}
elseif ( $params -> get ( " useContributor " . $nr ) == 2 )
{
$link_front = '<a href="' . $params -> get ( " linkContributor " . $nr ) . '" target="_blank">' ;
$link_back = '</a>' ;
}
else
{
$link_front = '' ;
$link_back = '' ;
}
$contributors [ $nr ][ 'title' ] = self :: htmlEscape ( $params -> get ( " titleContributor " . $nr ));
$contributors [ $nr ][ 'name' ] = $link_front . self :: htmlEscape ( $params -> get ( " nameContributor " . $nr )) . $link_back ;
}
}
return $contributors ;
2015-08-24 20:22:07 +00:00
}
/**
* Load the Component Help URLs .
**/
public static function getHelpUrl ( $view )
{
$user = JFactory :: getUser ();
2015-12-03 02:17:44 +00:00
$groups = $user -> get ( 'groups' );
$db = JFactory :: getDbo ();
2015-08-24 20:22:07 +00:00
$query = $db -> getQuery ( true );
$query -> select ( array ( 'a.id' , 'a.groups' , 'a.target' , 'a.type' , 'a.article' , 'a.url' ));
$query -> from ( '#__demo_help_document AS a' );
$query -> where ( 'a.admin_view = ' . $db -> quote ( $view ));
$query -> where ( 'a.location = 1' );
$query -> where ( 'a.published = 1' );
$db -> setQuery ( $query );
$db -> execute ();
if ( $db -> getNumRows ())
{
$helps = $db -> loadObjectList ();
if ( self :: checkArray ( $helps ))
{
foreach ( $helps as $nr => $help )
{
if ( $help -> target == 1 )
{
$targetgroups = json_decode ( $help -> groups , true );
if ( ! array_intersect ( $targetgroups , $groups ))
{
// if user not in those target groups then remove the item
unset ( $helps [ $nr ]);
continue ;
}
}
// set the return type
switch ( $help -> type )
{
// set joomla article
case 1 :
return self :: loadArticleLink ( $help -> article );
break ;
// set help text
case 2 :
return self :: loadHelpTextLink ( $help -> id );
break ;
// set Link
case 3 :
return $help -> url ;
break ;
}
}
}
}
return false ;
}
/**
* Get the Article Link .
**/
protected static function loadArticleLink ( $id )
{
return JURI :: root () . 'index.php?option=com_content&view=article&id=' . $id . '&tmpl=component&layout=modal' ;
}
/**
* Get the Help Text Link .
**/
protected static function loadHelpTextLink ( $id )
{
$token = JSession :: getFormToken ();
return 'index.php?option=com_demo&task=help.getText&id=' . ( int ) $id . '&token=' . $token ;
}
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
/**
2015-12-03 02:17:44 +00:00
* Configure the Linkbar .
2015-08-24 20:22:07 +00:00
**/
2015-12-03 02:17:44 +00:00
public static function addSubmenu ( $submenu )
2015-08-24 20:22:07 +00:00
{
2015-12-03 02:17:44 +00:00
// load user for access menus
$user = JFactory :: getUser ();
// load the submenus to sidebar
JHtmlSidebar :: addEntry ( JText :: _ ( 'COM_DEMO_SUBMENU_DASHBOARD' ), 'index.php?option=com_demo&view=demo' , $submenu == 'demo' );
2015-12-03 02:39:36 +00:00
if ( $user -> authorise ( 'look.access' , 'com_demo' ) && $user -> authorise ( 'look.submenu' , 'com_demo' ))
{
JHtmlSidebar :: addEntry ( JText :: _ ( 'COM_DEMO_SUBMENU_LOOKS' ), 'index.php?option=com_demo&view=looks' , $submenu == 'looks' );
}
2015-12-03 02:17:44 +00:00
if ( $user -> authorise ( 'help_document.access' , 'com_demo' ) && $user -> authorise ( 'help_document.submenu' , 'com_demo' ))
2015-08-24 20:22:07 +00:00
{
JHtmlSidebar :: addEntry ( JText :: _ ( 'COM_DEMO_SUBMENU_HELP_DOCUMENTS' ), 'index.php?option=com_demo&view=help_documents' , $submenu == 'help_documents' );
}
2015-12-03 02:17:44 +00:00
}
2015-08-24 20:22:07 +00:00
/**
* Prepares the xml document
*/
public static function xls ( $rows , $fileName = null , $title = null , $subjectTab = null , $creator = 'Vast Development Method' , $description = null , $category = null , $keywords = null , $modified = null )
{
// set the user
$user = JFactory :: getUser ();
// set fieldname if not set
if ( ! $fileName )
{
$fileName = 'exported_' . JFactory :: getDate () -> format ( 'jS_F_Y' );
}
// set modiefied if not set
if ( ! $modified )
{
$modified = $user -> name ;
}
// set title if not set
if ( ! $title )
{
$title = 'Book1' ;
}
// set tab name if not set
if ( ! $subjectTab )
{
$subjectTab = 'Sheet1' ;
}
// make sure the file is loaded
JLoader :: import ( 'PHPExcel' , JPATH_COMPONENT_ADMINISTRATOR . '/helpers' );
// Create new PHPExcel object
$objPHPExcel = new PHPExcel ();
// Set document properties
$objPHPExcel -> getProperties () -> setCreator ( $creator )
-> setCompany ( 'Vast Development Method' )
-> setLastModifiedBy ( $modified )
-> setTitle ( $title )
-> setSubject ( $subjectTab );
if ( ! $description )
{
$objPHPExcel -> getProperties () -> setDescription ( $description );
}
if ( ! $keywords )
{
$objPHPExcel -> getProperties () -> setKeywords ( $keywords );
}
if ( ! $category )
{
$objPHPExcel -> getProperties () -> setCategory ( $category );
}
// Some styles
$headerStyles = array (
'font' => array (
'bold' => true ,
'color' => array ( 'rgb' => '1171A3' ),
'size' => 12 ,
'name' => 'Verdana'
));
$sideStyles = array (
'font' => array (
'bold' => true ,
'color' => array ( 'rgb' => '444444' ),
'size' => 11 ,
'name' => 'Verdana'
));
$normalStyles = array (
'font' => array (
'color' => array ( 'rgb' => '444444' ),
'size' => 11 ,
'name' => 'Verdana'
));
// Add some data
if ( self :: checkArray ( $rows ))
{
$i = 1 ;
foreach ( $rows as $array ){
$a = 'A' ;
foreach ( $array as $value ){
$objPHPExcel -> setActiveSheetIndex ( 0 ) -> setCellValue ( $a . $i , $value );
if ( $i == 1 ){
$objPHPExcel -> getActiveSheet () -> getColumnDimension ( $a ) -> setAutoSize ( true );
$objPHPExcel -> getActiveSheet () -> getStyle ( $a . $i ) -> applyFromArray ( $headerStyles );
$objPHPExcel -> getActiveSheet () -> getStyle ( $a . $i ) -> getAlignment () -> setHorizontal ( PHPExcel_Style_Alignment :: HORIZONTAL_CENTER );
} elseif ( $a == 'A' ){
$objPHPExcel -> getActiveSheet () -> getStyle ( $a . $i ) -> applyFromArray ( $sideStyles );
} else {
$objPHPExcel -> getActiveSheet () -> getStyle ( $a . $i ) -> applyFromArray ( $normalStyles );
}
$a ++ ;
}
$i ++ ;
}
}
else
{
return false ;
}
// Rename worksheet
$objPHPExcel -> getActiveSheet () -> setTitle ( $subjectTab );
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel -> setActiveSheetIndex ( 0 );
// Redirect output to a client's web browser (Excel5)
header ( 'Content-Type: application/vnd.ms-excel' );
header ( 'Content-Disposition: attachment;filename="' . $fileName . '.xls"' );
header ( 'Cache-Control: max-age=0' );
// If you're serving to IE 9, then the following may be needed
header ( 'Cache-Control: max-age=1' );
// If you're serving to IE over SSL, then the following may be needed
header ( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); // Date in the past
header ( 'Last-Modified: ' . gmdate ( 'D, d M Y H:i:s' ) . ' GMT' ); // always modified
header ( 'Cache-Control: cache, must-revalidate' ); // HTTP/1.1
header ( 'Pragma: public' ); // HTTP/1.0
$objWriter = PHPExcel_IOFactory :: createWriter ( $objPHPExcel , 'Excel5' );
$objWriter -> save ( 'php://output' );
jexit ();
}
/**
* Get CSV Headers
*/
public static function getFileHeaders ( $dataType )
{
// make sure the file is loaded
JLoader :: import ( 'PHPExcel' , JPATH_COMPONENT_ADMINISTRATOR . '/helpers' );
// get session object
2015-12-29 19:39:38 +00:00
$session = JFactory :: getSession ();
2015-08-24 20:22:07 +00:00
$package = $session -> get ( 'package' , null );
$package = json_decode ( $package , true );
// set the headers
if ( isset ( $package [ 'dir' ]))
{
$inputFileType = PHPExcel_IOFactory :: identify ( $package [ 'dir' ]);
$excelReader = PHPExcel_IOFactory :: createReader ( $inputFileType );
$excelReader -> setReadDataOnly ( true );
$excelObj = $excelReader -> load ( $package [ 'dir' ]);
2015-12-29 19:39:38 +00:00
$headers = array ();
2015-08-24 20:22:07 +00:00
foreach ( $excelObj -> getActiveSheet () -> getRowIterator () as $row )
{
if ( $row -> getRowIndex () == 1 )
{
$cellIterator = $row -> getCellIterator ();
$cellIterator -> setIterateOnlyExistingCells ( false );
foreach ( $cellIterator as $cell )
{
if ( ! is_null ( $cell ))
{
$headers [ $cell -> getColumn ()] = $cell -> getValue ();
}
}
$excelObj -> disconnectWorksheets ();
unset ( $excelObj );
break ;
}
}
return $headers ;
}
return false ;
}
2015-12-03 02:17:44 +00:00
public static function getVar ( $table , $where = null , $whereString = 'user' , $what = 'id' , $operator = '=' , $main = 'demo' )
{
if ( ! $where )
{
$where = JFactory :: getUser () -> id ;
}
// Get a db connection.
$db = JFactory :: getDbo ();
// Create a new query object.
$query = $db -> getQuery ( true );
$query -> select ( $db -> quoteName ( array ( $what )));
$query -> from ( $db -> quoteName ( '#__' . $main . '_' . $table ));
if ( is_numeric ( $where ))
{
$query -> where ( $db -> quoteName ( $whereString ) . ' ' . $operator . ' ' . ( int ) $where );
}
elseif ( is_string ( $where ))
{
$query -> where ( $db -> quoteName ( $whereString ) . ' ' . $operator . ' ' . $db -> quote (( string ) $where ));
}
else
{
return false ;
}
$db -> setQuery ( $query );
$db -> execute ();
if ( $db -> getNumRows ())
{
return $db -> loadResult ();
}
return false ;
}
public static function getVars ( $table , $where = null , $whereString = 'user' , $what = 'id' , $operator = 'IN' , $main = 'demo' , $unique = true )
{
if ( ! $where )
{
$where = JFactory :: getUser () -> id ;
}
if ( ! self :: checkArray ( $where ) && $where > 0 )
{
$where = array ( $where );
}
if ( self :: checkArray ( $where ))
{
// Get a db connection.
$db = JFactory :: getDbo ();
// Create a new query object.
$query = $db -> getQuery ( true );
$query -> select ( $db -> quoteName ( array ( $what )));
$query -> from ( $db -> quoteName ( '#__' . $main . '_' . $table ));
$query -> where ( $db -> quoteName ( $whereString ) . ' ' . $operator . ' (' . implode ( ',' , $where ) . ')' );
$db -> setQuery ( $query );
$db -> execute ();
if ( $db -> getNumRows ())
{
if ( $unique )
{
return array_unique ( $db -> loadColumn ());
}
return $db -> loadColumn ();
}
}
return false ;
}
2015-08-24 20:22:07 +00:00
public static function jsonToString ( $value , $sperator = " , " )
{
2015-12-03 02:17:44 +00:00
// check if string is JSON
$result = json_decode ( $value , true );
if ( json_last_error () === JSON_ERROR_NONE ) {
// is JSON
2015-08-24 20:22:07 +00:00
if ( self :: checkArray ( $result ))
{
$value = '' ;
$counter = 0 ;
foreach ( $result as $string )
{
if ( $counter )
{
$value .= $sperator . $string ;
}
else
{
$value .= $string ;
}
$counter ++ ;
}
return $value ;
}
2015-12-03 02:17:44 +00:00
return json_decode ( $value );
}
return $value ;
2015-08-24 20:22:07 +00:00
}
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
public static function isPublished ( $id , $type )
{
if ( $type == 'raw' )
2015-12-03 02:17:44 +00:00
{
2015-08-24 20:22:07 +00:00
$type = 'item' ;
}
$db = JFactory :: getDbo ();
$query = $db -> getQuery ( true );
$query -> select ( array ( 'a.published' ));
$query -> from ( '#__demo_' . $type . ' AS a' );
$query -> where ( 'a.id = ' . $id );
$query -> where ( 'a.published = 1' );
$db -> setQuery ( $query );
$db -> execute ();
$found = $db -> getNumRows ();
if ( $found )
2015-12-03 02:17:44 +00:00
{
2015-08-24 20:22:07 +00:00
return true ;
}
return false ;
}
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
public static function getGroupName ( $id )
{
$db = JFactory :: getDBO ();
$query = $db -> getQuery ( true );
$query -> select ( array ( 'a.title' ));
$query -> from ( '#__usergroups AS a' );
$query -> where ( 'a.id = ' . $id );
$db -> setQuery ( $query );
$db -> execute ();
$found = $db -> getNumRows ();
if ( $found )
2015-12-03 02:17:44 +00:00
{
2015-08-24 20:22:07 +00:00
return $db -> loadResult ();
}
return $id ;
}
2015-12-03 02:17:44 +00:00
/**
2015-08-24 20:22:07 +00:00
* Get the actions permissions
**/
2015-12-03 02:17:44 +00:00
public static function getActions ( $view , & $record = null , $views = null )
2015-08-24 20:22:07 +00:00
{
jimport ( 'joomla.access.access' );
$user = JFactory :: getUser ();
$result = new JObject ;
$view = self :: safeString ( $view );
2015-12-03 02:17:44 +00:00
if ( self :: checkString ( $views ))
{
2015-08-24 20:22:07 +00:00
$views = self :: safeString ( $views );
2015-12-03 02:17:44 +00:00
}
2015-08-24 20:22:07 +00:00
// get all actions from component
$actions = JAccess :: getActions ( 'com_demo' , 'component' );
2015-12-03 02:17:44 +00:00
// set acctions only set in component settiongs
$componentActions = array ( 'core.admin' , 'core.manage' , 'core.options' , 'core.export' );
2015-08-24 20:22:07 +00:00
// loop the actions and set the permissions
foreach ( $actions as $action )
2015-12-03 02:17:44 +00:00
{
2015-08-24 20:22:07 +00:00
// set to use component default
2016-02-20 20:19:24 +00:00
$fallback = true ;
2015-08-24 20:22:07 +00:00
if ( self :: checkObject ( $record ) && isset ( $record -> id ) && $record -> id > 0 && ! in_array ( $action -> name , $componentActions ))
{
// The record has been set. Check the record permissions.
$permission = $user -> authorise ( $action -> name , 'com_demo.' . $view . '.' . ( int ) $record -> id );
if ( ! $permission && ! is_null ( $permission ))
{
if ( $action -> name == 'core.edit' || $action -> name == $view . '.edit' )
{
if ( $user -> authorise ( 'core.edit.own' , 'com_demo.' . $view . '.' . ( int ) $record -> id ))
{
// If the owner matches 'me' then allow.
if ( isset ( $record -> created_by ) && $record -> created_by > 0 && ( $record -> created_by == $user -> id ))
{
$result -> set ( $action -> name , true );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
else
{
$result -> set ( $action -> name , false );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
}
elseif ( $user -> authorise ( $view . 'edit.own' , 'com_demo.' . $view . '.' . ( int ) $record -> id ))
{
// If the owner matches 'me' then allow.
if ( isset ( $record -> created_by ) && $record -> created_by > 0 && ( $record -> created_by == $user -> id ))
{
$result -> set ( $action -> name , true );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
else
{
$result -> set ( $action -> name , false );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
}
elseif ( $user -> authorise ( 'core.edit.own' , 'com_demo' ))
{
// If the owner matches 'me' then allow.
if ( isset ( $record -> created_by ) && $record -> created_by > 0 && ( $record -> created_by == $user -> id ))
{
$result -> set ( $action -> name , true );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
else
{
$result -> set ( $action -> name , false );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
}
elseif ( $user -> authorise ( $view . 'edit.own' , 'com_demo' ))
{
// If the owner matches 'me' then allow.
if ( isset ( $record -> created_by ) && $record -> created_by > 0 && ( $record -> created_by == $user -> id ))
{
$result -> set ( $action -> name , true );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
else
{
$result -> set ( $action -> name , false );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
}
}
}
elseif ( self :: checkString ( $views ) && isset ( $record -> catid ) && $record -> catid > 0 )
{
2015-12-03 02:17:44 +00:00
// make sure we use the core. action check for the categories
if ( strpos ( $action -> name , $view ) !== false && strpos ( $action -> name , 'core.' ) === false ) {
$coreCheck = explode ( '.' , $action -> name );
$coreCheck [ 0 ] = 'core' ;
$categoryCheck = implode ( '.' , $coreCheck );
}
else
{
$categoryCheck = $action -> name ;
}
// The record has a category. Check the category permissions.
2015-08-24 20:22:07 +00:00
$catpermission = $user -> authorise ( $categoryCheck , 'com_demo.' . $views . '.category.' . ( int ) $record -> catid );
if ( ! $catpermission && ! is_null ( $catpermission ))
{
if ( $action -> name == 'core.edit' || $action -> name == $view . '.edit' )
{
if ( $user -> authorise ( 'core.edit.own' , 'com_demo.' . $views . '.category.' . ( int ) $record -> catid ))
{
// If the owner matches 'me' then allow.
if ( isset ( $record -> created_by ) && $record -> created_by > 0 && ( $record -> created_by == $user -> id ))
{
$result -> set ( $action -> name , true );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
else
{
$result -> set ( $action -> name , false );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
}
elseif ( $user -> authorise ( $view . 'edit.own' , 'com_demo.' . $views . '.category.' . ( int ) $record -> catid ))
{
// If the owner matches 'me' then allow.
if ( isset ( $record -> created_by ) && $record -> created_by > 0 && ( $record -> created_by == $user -> id ))
{
$result -> set ( $action -> name , true );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
else
{
$result -> set ( $action -> name , false );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
}
elseif ( $user -> authorise ( 'core.edit.own' , 'com_demo' ))
{
// If the owner matches 'me' then allow.
if ( isset ( $record -> created_by ) && $record -> created_by > 0 && ( $record -> created_by == $user -> id ))
{
$result -> set ( $action -> name , true );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
else
{
$result -> set ( $action -> name , false );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
}
elseif ( $user -> authorise ( $view . 'edit.own' , 'com_demo' ))
{
// If the owner matches 'me' then allow.
if ( isset ( $record -> created_by ) && $record -> created_by > 0 && ( $record -> created_by == $user -> id ))
{
$result -> set ( $action -> name , true );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
else
{
$result -> set ( $action -> name , false );
// set not to use component default
2016-02-20 20:19:24 +00:00
$fallback = false ;
2015-08-24 20:22:07 +00:00
}
}
}
}
}
}
2016-02-20 20:19:24 +00:00
// if allowed then fallback on component global settings
if ( $fallback )
2015-08-24 20:22:07 +00:00
{
$result -> set ( $action -> name , $user -> authorise ( $action -> name , 'com_demo' ));
}
}
return $result ;
}
/**
* Get any component ' s model
**/
public static function getModel ( $name , $path = JPATH_COMPONENT_ADMINISTRATOR , $component = 'demo' )
{
// load some joomla helpers
2015-12-03 02:17:44 +00:00
JLoader :: import ( 'joomla.application.component.model' );
2015-08-24 20:22:07 +00:00
// load the model file
JLoader :: import ( $name , $path . '/models' );
// return instance
return JModelLegacy :: getInstance ( $name , $component . 'Model' );
}
2015-12-03 02:17:44 +00:00
/**
* Add to asset Table
*/
public static function setAsset ( $id , $table )
{
$parent = JTable :: getInstance ( 'Asset' );
$parent -> loadByName ( 'com_demo' );
$parentId = $parent -> id ;
$name = 'com_demo.' . $table . '.' . $id ;
$title = '' ;
$asset = JTable :: getInstance ( 'Asset' );
$asset -> loadByName ( $name );
// Check for an error.
$error = $asset -> getError ();
if ( $error )
{
return false ;
}
else
{
// Specify how a new or moved node asset is inserted into the tree.
if ( $asset -> parent_id != $parentId )
{
$asset -> setLocation ( $parentId , 'last-child' );
}
// Prepare the asset to be stored.
$asset -> parent_id = $parentId ;
$asset -> name = $name ;
$asset -> title = $title ;
// get the default asset rules
$rules = self :: getDefaultAssetRules ( 'com_demo' , $table );
if ( $rules instanceof JAccessRules )
{
$asset -> rules = ( string ) $rules ;
}
if ( ! $asset -> check () || ! $asset -> store ())
{
JError :: raiseWarning ( 500 , $asset -> getError ());
return false ;
}
else
{
// Create an asset_id or heal one that is corrupted.
$object = new stdClass ();
// Must be a valid primary key value.
$object -> id = $id ;
$object -> asset_id = ( int ) $asset -> id ;
// Update their asset_id to link to the asset table.
return JFactory :: getDbo () -> updateObject ( '#__demo_' . $table , $object , 'id' );
}
}
return false ;
}
/**
* Gets the default asset Rules for a component / view .
*/
protected static function getDefaultAssetRules ( $component , $view )
{
// Need to find the asset id by the name of the component.
$db = JFactory :: getDbo ();
$query = $db -> getQuery ( true )
-> select ( $db -> quoteName ( 'id' ))
-> from ( $db -> quoteName ( '#__assets' ))
-> where ( $db -> quoteName ( 'name' ) . ' = ' . $db -> quote ( $component ));
$db -> setQuery ( $query );
$db -> execute ();
if ( $db -> loadRowList ())
{
// asset alread set so use saved rules
$assetId = ( int ) $db -> loadResult ();
$result = JAccess :: getAssetRules ( $assetId );
if ( $result instanceof JAccessRules )
{
$_result = ( string ) $result ;
$_result = json_decode ( $_result );
foreach ( $_result as $name => & $rule )
{
$v = explode ( '.' , $name );
if ( $view !== $v [ 0 ])
{
// remove since it is not part of this view
unset ( $_result -> $name );
}
else
{
// clear the value since we inherit
$rule = array ();
}
}
// check if there are any view values remaining
if ( count ( $_result ))
{
$_result = json_encode ( $_result );
$_result = array ( $_result );
// Instantiate and return the JAccessRules object for the asset rules.
$rules = new JAccessRules ( $_result );
return $rules ;
}
return $result ;
}
}
return JAccess :: getAssetRules ( 0 );
}
2015-08-24 20:22:07 +00:00
public static function renderBoolButton ()
{
$args = func_get_args ();
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
// get the radio element
$button = JFormHelper :: loadFieldType ( 'radio' );
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
// setup the properties
$name = self :: htmlEscape ( $args [ 0 ]);
$additional = isset ( $args [ 1 ]) ? ( string ) $args [ 1 ] : '' ;
$value = $args [ 2 ];
$yes = isset ( $args [ 3 ]) ? self :: htmlEscape ( $args [ 3 ]) : 'JYES' ;
$no = isset ( $args [ 4 ]) ? self :: htmlEscape ( $args [ 4 ]) : 'JNO' ;
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
// prepare the xml
$element = new SimpleXMLElement ( '<field name="' . $name . '" type="radio" class="btn-group"><option ' . $additional . ' value="0">' . $no . '</option><option ' . $additional . ' value="1">' . $yes . '</option></field>' );
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
// run
$button -> setup ( $element , $value );
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
return $button -> input ;
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
}
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
public static function checkObject ( $object )
{
if ( isset ( $object ) && is_object ( $object ) && count ( $object ) > 0 )
{
2015-12-03 02:17:44 +00:00
return true ;
2015-08-24 20:22:07 +00:00
}
return false ;
}
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
public static function checkArray ( $array )
{
if ( isset ( $array ) && is_array ( $array ) && count ( $array ) > 0 )
{
return true ;
}
return false ;
}
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
public static function checkString ( $string )
{
if ( isset ( $string ) && is_string ( $string ) && strlen ( $string ) > 0 )
{
return true ;
}
return false ;
}
2015-12-03 02:17:44 +00:00
public static function mergeArrays ( $arrays )
{
if ( self :: checkArray ( $arrays ))
{
$arrayBuket = array ();
foreach ( $arrays as $array )
{
if ( self :: checkArray ( $array ))
{
$arrayBuket = array_merge ( $arrayBuket , $array );
}
}
return $arrayBuket ;
}
return false ;
}
2015-08-24 20:22:07 +00:00
public static function sorten ( $string , $length = 40 , $addTip = true )
{
if ( self :: checkString ( $string ))
{
$initial = strlen ( $string );
$words = preg_split ( '/([\s\n\r]+)/' , $string , null , PREG_SPLIT_DELIM_CAPTURE );
$words_count = count ( $words );
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
$word_length = 0 ;
$last_word = 0 ;
for (; $last_word < $words_count ; ++ $last_word )
{
$word_length += strlen ( $words [ $last_word ]);
if ( $word_length > $length )
{
break ;
}
}
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
$newString = implode ( array_slice ( $words , 0 , $last_word ));
$final = strlen ( $newString );
if ( $initial != $final && $addTip )
{
$title = self :: sorten ( $string , 400 , false );
return '<span class="hasTip" title="' . $title . '" style="cursor:help">' . trim ( $newString ) . '...</span>' ;
}
elseif ( $initial != $final && ! $addTip )
{
return trim ( $newString ) . '...' ;
}
}
return $string ;
}
2015-12-03 02:17:44 +00:00
2015-08-24 20:22:07 +00:00
public static function safeString ( $string , $type = 'L' , $spacer = '_' )
{
// remove all numbers and replace with english text version (works well only up to a thousand)
2015-12-03 02:17:44 +00:00
$string = self :: replaceNumbers ( $string );
if ( self :: checkString ( $string ))
{
// remove all other characters
$string = trim ( $string );
$string = preg_replace ( '/' . $spacer . '+/' , ' ' , $string );
$string = preg_replace ( '/\s+/' , ' ' , $string );
$string = preg_replace ( " /[^A-Za-z ]/ " , '' , $string );
// return a string with all first letter of each word uppercase(no undersocre)
if ( $type == 'W' )
{
return ucwords ( strtolower ( $string ));
}
elseif ( $type == 'w' )
{
return strtolower ( $string );
}
elseif ( $type == 'Ww' )
{
return ucfirst ( strtolower ( $string ));
}
elseif ( $type == 'WW' )
{
return strtoupper ( $string );
}
elseif ( $type == 'U' )
{
// replace white space with underscore
$string = preg_replace ( '/\s+/' , $spacer , $string );
// return all upper
return strtoupper ( $string );
}
elseif ( $type == 'F' )
{
// replace white space with underscore
$string = preg_replace ( '/\s+/' , $spacer , $string );
// return with first caracter to upper
return ucfirst ( strtolower ( $string ));
}
elseif ( $type == 'L' )
{
// replace white space with underscore
$string = preg_replace ( '/\s+/' , $spacer , $string );
// default is to return lower
return strtolower ( $string );
}
// return string
return $string ;
}
// not a string
return '' ;
2015-08-24 20:22:07 +00:00
}
2015-12-03 02:17:44 +00:00
public static function htmlEscape ( $var , $charset = 'UTF-8' , $sorten = false , $length = 40 )
2015-08-24 20:22:07 +00:00
{
if ( self :: checkString ( $var ))
{
$filter = new JFilterInput ();
$string = $filter -> clean ( html_entity_decode ( htmlentities ( $var , ENT_COMPAT , $charset )), 'HTML' );
if ( $sorten )
{
2015-12-03 02:17:44 +00:00
return self :: sorten ( $string , $length );
2015-08-24 20:22:07 +00:00
}
return $string ;
2015-12-03 02:17:44 +00:00
}
2015-08-24 20:22:07 +00:00
else
{
return '' ;
2015-12-03 02:17:44 +00:00
}
2015-08-24 20:22:07 +00:00
}
2015-12-03 02:17:44 +00:00
public static function replaceNumbers ( $string )
2015-08-24 20:22:07 +00:00
{
2015-12-03 02:17:44 +00:00
// set numbers array
$numbers = array ();
// first get all numbers
preg_match_all ( '!\d+!' , $string , $numbers );
// check if we have any numbers
if ( isset ( $numbers [ 0 ]) && self :: checkArray ( $numbers [ 0 ]))
{
foreach ( $numbers [ 0 ] as $number )
{
$searchReplace [ $number ] = self :: numberToString (( int ) $number );
}
// now replace numbers in string
$string = str_replace ( array_keys ( $searchReplace ), array_values ( $searchReplace ), $string );
// check if we missed any, strange if we did.
return self :: replaceNumbers ( $string );
2015-08-24 20:22:07 +00:00
}
2015-12-03 02:17:44 +00:00
// return the string with no numbers remaining.
return $string ;
2015-08-24 20:22:07 +00:00
}
2015-12-03 02:17:44 +00:00
/**
* Convert an integer into an English word string
* Thanks to Tom Nicholson < http :// php . net / manual / en / function . strval . php #41988>
*
* @ input an int
* @ returns a string
**/
public static function numberToString ( $x )
{
$nwords = array ( " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " ,
" eight " , " nine " , " ten " , " eleven " , " twelve " , " thirteen " ,
" fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " ,
" nineteen " , " twenty " , 30 => " thirty " , 40 => " forty " ,
50 => " fifty " , 60 => " sixty " , 70 => " seventy " , 80 => " eighty " ,
90 => " ninety " );
if ( ! is_numeric ( $x ))
{
$w = $x ;
}
elseif ( fmod ( $x , 1 ) != 0 )
{
$w = $x ;
}
else
{
if ( $x < 0 )
{
$w = 'minus ' ;
$x = - $x ;
}
else
{
$w = '' ;
// ... now $x is a non-negative integer.
}
if ( $x < 21 ) // 0 to 20
{
$w .= $nwords [ $x ];
}
elseif ( $x < 100 ) // 21 to 99
{
$w .= $nwords [ 10 * floor ( $x / 10 )];
$r = fmod ( $x , 10 );
if ( $r > 0 )
{
$w .= ' ' . $nwords [ $r ];
}
}
elseif ( $x < 1000 ) // 100 to 999
{
$w .= $nwords [ floor ( $x / 100 )] . ' hundred' ;
$r = fmod ( $x , 100 );
if ( $r > 0 )
{
$w .= ' and ' . self :: numberToString ( $r );
}
}
elseif ( $x < 1000000 ) // 1000 to 999999
{
$w .= self :: numberToString ( floor ( $x / 1000 )) . ' thousand' ;
$r = fmod ( $x , 1000 );
if ( $r > 0 )
{
$w .= ' ' ;
if ( $r < 100 )
{
$w .= 'and ' ;
}
$w .= self :: numberToString ( $r );
}
}
else // millions
{
$w .= self :: numberToString ( floor ( $x / 1000000 )) . ' million' ;
$r = fmod ( $x , 1000000 );
if ( $r > 0 )
{
$w .= ' ' ;
if ( $r < 100 )
{
$word .= 'and ' ;
}
$w .= self :: numberToString ( $r );
}
}
}
return $w ;
}
2015-08-24 20:22:07 +00:00
/**
* Random Key
*
* @ returns a string
**/
public static function randomkey ( $size )
{
$bag = " abcefghijknopqrstuwxyzABCDDEFGHIJKLLMMNOPQRSTUVVWXYZabcddefghijkllmmnopqrstuvvwxyzABCEFGHIJKNOPQRSTUWXYZ " ;
$key = array ();
$bagsize = strlen ( $bag ) - 1 ;
for ( $i = 0 ; $i < $size ; $i ++ )
{
$get = rand ( 0 , $bagsize );
$key [] = $bag [ $get ];
}
return implode ( $key );
}
}