2016-03-25 01:18:05 -07:00
< ? php
2021-06-10 13:15:52 -07:00
2016-03-25 01:18:05 -07:00
namespace App\Helpers ;
use App\Models\Accessory ;
2019-03-13 20:12:03 -07:00
use App\Models\Component ;
2016-03-25 01:18:05 -07:00
use App\Models\Consumable ;
2019-03-13 20:12:03 -07:00
use App\Models\CustomField ;
use App\Models\CustomFieldset ;
use App\Models\Depreciation ;
2017-03-03 17:29:41 -08:00
use App\Models\Setting ;
2019-03-13 20:12:03 -07:00
use App\Models\Statuslabel ;
2016-08-25 20:58:56 -07:00
use Crypt ;
use Illuminate\Contracts\Encryption\DecryptException ;
2021-06-10 13:15:52 -07:00
use Image ;
2022-11-29 09:51:42 -08:00
use Carbon\Carbon ;
2016-03-25 01:18:05 -07:00
class Helper
{
2016-10-25 10:52:37 -07:00
/**
2016-10-25 11:11:47 -07:00
* Simple helper to invoke the markdown parser
2016-10-25 10:52:37 -07:00
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
2016-10-25 11:11:47 -07:00
* @ since [ v2 . 0 ]
2021-06-10 13:15:52 -07:00
* @ return string
2016-10-25 10:52:37 -07:00
*/
2022-08-29 11:26:47 -07:00
public static function parseEscapedMarkedown ( $str = null )
2016-12-29 14:02:18 -08:00
{
2016-05-24 10:21:33 -07:00
$Parsedown = new \Parsedown ();
2022-08-29 11:26:47 -07:00
$Parsedown -> setSafeMode ( true );
2016-05-24 10:21:33 -07:00
if ( $str ) {
2022-08-29 11:26:47 -07:00
return $Parsedown -> text ( $str );
2016-05-24 10:21:33 -07:00
}
}
2023-07-11 03:41:58 -07:00
public static function parseEscapedMarkedownInline ( $str = null )
{
$Parsedown = new \Parsedown ();
$Parsedown -> setSafeMode ( true );
2016-05-24 10:21:33 -07:00
if ( $str ) {
2023-07-11 03:33:29 -07:00
return $Parsedown -> line ( $str );
2016-05-24 10:21:33 -07:00
}
}
2016-10-25 11:11:47 -07:00
/**
* The importer has formatted number strings since v3 ,
* so the value might be a string , or an integer .
* If it ' s a number , format it as a string .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v2 . 0 ]
2021-06-10 13:15:52 -07:00
* @ return string
2016-10-25 11:11:47 -07:00
*/
2016-08-16 18:47:53 -07:00
public static function formatCurrencyOutput ( $cost )
2016-08-15 19:57:34 -07:00
{
if ( is_numeric ( $cost )) {
2021-06-28 16:14:32 -07:00
2021-08-17 21:59:33 -07:00
if ( Setting :: getSettings () -> digit_separator == '1.234,56' ) {
return number_format ( $cost , 2 , ',' , '.' );
}
return number_format ( $cost , 2 , '.' , ',' );
2016-08-15 19:57:34 -07:00
}
// It's already been parsed.
return $cost ;
}
2016-03-25 01:18:05 -07:00
2016-10-25 11:11:47 -07:00
/**
* Static colors for pie charts .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v3 . 3 ]
2021-06-10 13:15:52 -07:00
* @ return array
2016-10-25 11:11:47 -07:00
*/
2020-11-18 07:06:14 -08:00
public static function defaultChartColors ( $index = 0 )
2016-05-24 16:05:51 -07:00
{
$colors = [
2021-06-10 13:15:52 -07:00
'#008941' ,
'#FF4A46' ,
'#006FA6' ,
'#A30059' ,
'#1CE6FF' ,
'#FFDBE5' ,
'#7A4900' ,
'#0000A6' ,
'#63FFAC' ,
'#B79762' ,
'#004D43' ,
'#8FB0FF' ,
'#997D87' ,
'#5A0007' ,
'#809693' ,
'#FEFFE6' ,
'#1B4400' ,
'#4FC601' ,
'#3B5DFF' ,
'#4A3B53' ,
'#FF2F80' ,
'#61615A' ,
'#BA0900' ,
'#6B7900' ,
'#00C2A0' ,
'#FFAA92' ,
'#FF90C9' ,
'#B903AA' ,
'#D16100' ,
'#DDEFFF' ,
'#000035' ,
'#7B4F4B' ,
'#A1C299' ,
'#300018' ,
'#0AA6D8' ,
'#013349' ,
'#00846F' ,
'#372101' ,
'#FFB500' ,
'#C2FFED' ,
'#A079BF' ,
'#CC0744' ,
'#C0B9B2' ,
'#C2FF99' ,
'#001E09' ,
'#00489C' ,
'#6F0062' ,
'#0CBD66' ,
'#EEC3FF' ,
'#456D75' ,
'#B77B68' ,
'#7A87A1' ,
'#788D66' ,
'#885578' ,
'#FAD09F' ,
'#FF8A9A' ,
'#D157A0' ,
'#BEC459' ,
'#456648' ,
'#0086ED' ,
'#886F4C' ,
'#34362D' ,
'#B4A8BD' ,
'#00A6AA' ,
'#452C2C' ,
'#636375' ,
'#A3C8C9' ,
'#FF913F' ,
'#938A81' ,
'#575329' ,
'#00FECF' ,
'#B05B6F' ,
'#8CD0FF' ,
'#3B9700' ,
'#04F757' ,
'#C8A1A1' ,
'#1E6E00' ,
'#7900D7' ,
'#A77500' ,
'#6367A9' ,
'#A05837' ,
'#6B002C' ,
'#772600' ,
'#D790FF' ,
'#9B9700' ,
'#549E79' ,
'#FFF69F' ,
'#201625' ,
'#72418F' ,
'#BC23FF' ,
'#99ADC0' ,
'#3A2465' ,
'#922329' ,
'#5B4534' ,
'#FDE8DC' ,
'#404E55' ,
'#0089A3' ,
'#CB7E98' ,
'#A4E804' ,
'#324E72' ,
'#6A3A4C' ,
'#83AB58' ,
'#001C1E' ,
'#D1F7CE' ,
'#004B28' ,
'#C8D0F6' ,
'#A3A489' ,
'#806C66' ,
'#222800' ,
'#BF5650' ,
'#E83000' ,
'#66796D' ,
'#DA007C' ,
'#FF1A59' ,
'#8ADBB4' ,
'#1E0200' ,
'#5B4E51' ,
'#C895C5' ,
'#320033' ,
'#FF6832' ,
'#66E1D3' ,
'#CFCDAC' ,
'#D0AC94' ,
'#7ED379' ,
'#012C58' ,
'#7A7BFF' ,
'#D68E01' ,
'#353339' ,
'#78AFA1' ,
'#FEB2C6' ,
'#75797C' ,
'#837393' ,
'#943A4D' ,
'#B5F4FF' ,
'#D2DCD5' ,
'#9556BD' ,
'#6A714A' ,
'#001325' ,
'#02525F' ,
'#0AA3F7' ,
'#E98176' ,
'#DBD5DD' ,
'#5EBCD1' ,
'#3D4F44' ,
'#7E6405' ,
'#02684E' ,
'#962B75' ,
'#8D8546' ,
'#9695C5' ,
'#E773CE' ,
'#D86A78' ,
'#3E89BE' ,
'#CA834E' ,
'#518A87' ,
'#5B113C' ,
'#55813B' ,
'#E704C4' ,
'#00005F' ,
'#A97399' ,
'#4B8160' ,
'#59738A' ,
'#FF5DA7' ,
'#F7C9BF' ,
'#643127' ,
'#513A01' ,
'#6B94AA' ,
'#51A058' ,
'#A45B02' ,
'#1D1702' ,
'#E20027' ,
'#E7AB63' ,
'#4C6001' ,
'#9C6966' ,
'#64547B' ,
'#97979E' ,
'#006A66' ,
'#391406' ,
'#F4D749' ,
'#0045D2' ,
'#006C31' ,
'#DDB6D0' ,
'#7C6571' ,
'#9FB2A4' ,
'#00D891' ,
'#15A08A' ,
'#BC65E9' ,
'#FFFFFE' ,
'#C6DC99' ,
'#203B3C' ,
'#671190' ,
'#6B3A64' ,
'#F5E1FF' ,
'#FFA0F2' ,
'#CCAA35' ,
'#374527' ,
'#8BB400' ,
'#797868' ,
'#C6005A' ,
'#3B000A' ,
'#C86240' ,
'#29607C' ,
'#402334' ,
'#7D5A44' ,
'#CCB87C' ,
'#B88183' ,
'#AA5199' ,
'#B5D6C3' ,
'#A38469' ,
'#9F94F0' ,
'#A74571' ,
'#B894A6' ,
'#71BB8C' ,
'#00B433' ,
'#789EC9' ,
'#6D80BA' ,
'#953F00' ,
'#5EFF03' ,
'#E4FFFC' ,
'#1BE177' ,
'#BCB1E5' ,
'#76912F' ,
'#003109' ,
'#0060CD' ,
'#D20096' ,
'#895563' ,
'#29201D' ,
'#5B3213' ,
'#A76F42' ,
'#89412E' ,
'#1A3A2A' ,
'#494B5A' ,
'#A88C85' ,
'#F4ABAA' ,
'#A3F3AB' ,
'#00C6C8' ,
'#EA8B66' ,
'#958A9F' ,
'#BDC9D2' ,
'#9FA064' ,
'#BE4700' ,
'#658188' ,
'#83A485' ,
'#453C23' ,
'#47675D' ,
'#3A3F00' ,
'#061203' ,
'#DFFB71' ,
'#868E7E' ,
'#98D058' ,
'#6C8F7D' ,
'#D7BFC2' ,
'#3C3E6E' ,
'#D83D66' ,
'#2F5D9B' ,
'#6C5E46' ,
'#D25B88' ,
'#5B656C' ,
'#00B57F' ,
'#545C46' ,
'#866097' ,
'#365D25' ,
'#252F99' ,
'#00CCFF' ,
'#674E60' ,
'#FC009C' ,
'#92896B' ,
2016-05-24 16:05:51 -07:00
];
2020-11-18 07:06:14 -08:00
2023-03-23 11:43:02 -07:00
$total_colors = count ( $colors );
2020-11-18 07:06:14 -08:00
2023-03-23 11:43:02 -07:00
if ( $index >= $total_colors ) {
$index = $index - $total_colors ;
}
2020-11-18 07:06:14 -08:00
return $colors [ $index ];
}
/**
* Increases or decreases the brightness of a color by a percentage of the current brightness .
*
* @ param string $hexCode Supported formats : `#FFF` , `#FFFFFF` , `FFF` , `FFFFFF`
* @ param float $adjustPercent A number between - 1 and 1. E . g . 0.3 = 30 % lighter ; - 0.4 = 40 % darker .
*
* @ return string
*/
2021-06-10 13:15:52 -07:00
public static function adjustBrightness ( $hexCode , $adjustPercent )
{
2020-11-18 07:06:14 -08:00
$hexCode = ltrim ( $hexCode , '#' );
if ( strlen ( $hexCode ) == 3 ) {
2021-06-10 13:15:52 -07:00
$hexCode = $hexCode [ 0 ] . $hexCode [ 0 ] . $hexCode [ 1 ] . $hexCode [ 1 ] . $hexCode [ 2 ] . $hexCode [ 2 ];
2020-11-18 07:06:14 -08:00
}
$hexCode = array_map ( 'hexdec' , str_split ( $hexCode , 2 ));
2021-06-10 13:15:52 -07:00
foreach ( $hexCode as & $color ) {
2020-11-18 07:06:14 -08:00
$adjustableLimit = $adjustPercent < 0 ? $color : 255 - $color ;
$adjustAmount = ceil ( $adjustableLimit * $adjustPercent );
$color = str_pad ( dechex ( $color + $adjustAmount ), 2 , '0' , STR_PAD_LEFT );
}
2021-06-10 13:15:52 -07:00
return '#' . implode ( $hexCode );
2016-05-24 16:05:51 -07:00
}
2016-10-25 11:11:47 -07:00
/**
* Static background ( highlight ) colors for pie charts
* This is inelegant , and could be refactored later .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v3 . 2 ]
2021-06-10 13:15:52 -07:00
* @ return array
2016-10-25 11:11:47 -07:00
*/
2016-05-24 16:05:51 -07:00
public static function chartBackgroundColors ()
{
$colors = [
'#f56954' ,
'#00a65a' ,
'#f39c12' ,
'#00c0ef' ,
'#3c8dbc' ,
'#d2d6de' ,
'#3c8dbc' ,
'#3c8dbc' ,
'#3c8dbc' ,
];
2021-06-10 13:15:52 -07:00
2016-05-24 16:05:51 -07:00
return $colors ;
}
2016-10-25 11:11:47 -07:00
/**
* Format currency using comma for thousands until local info is property used .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v2 . 7 ]
2021-06-10 13:15:52 -07:00
* @ return string
2016-10-25 11:11:47 -07:00
*/
2016-03-25 01:18:05 -07:00
public static function ParseFloat ( $floatString )
{
2021-09-28 18:20:39 -07:00
/*******
*
* WARNING : This does conversions based on * locale * - a Unix - ey - like thing .
*
* Everything else in the system tends to convert based on the Snipe - IT settings
*
* So it ' s very likely this is * not * what you want - instead look for the new
*
* ParseCurrency ( $currencyString )
*
* Which should be directly below here
*
*/
2016-03-25 01:18:05 -07:00
$LocaleInfo = localeconv ();
2021-06-10 13:15:52 -07:00
$floatString = str_replace ( ',' , '' , $floatString );
$floatString = str_replace ( $LocaleInfo [ 'decimal_point' ], '.' , $floatString );
2017-01-10 16:19:18 -08:00
// Strip Currency symbol
2017-11-24 10:42:11 -08:00
// If no currency symbol is set, default to $ because Murica
$currencySymbol = $LocaleInfo [ 'currency_symbol' ];
if ( empty ( $currencySymbol )) {
$currencySymbol = '$' ;
}
$floatString = str_replace ( $currencySymbol , '' , $floatString );
2021-06-10 13:15:52 -07:00
2016-03-25 01:18:05 -07:00
return floatval ( $floatString );
}
2021-09-28 18:20:39 -07:00
/**
* Format currency using comma or period for thousands , and period or comma for decimal , based on settings .
*
* @ author [ B . Wetherington ] [ < bwetherington @ grokability . com > ]
* @ since [ v5 . 2 ]
* @ return Float
*/
public static function ParseCurrency ( $currencyString ) {
$without_currency = str_replace ( Setting :: getSettings () -> default_currency , '' , $currencyString ); //generally shouldn't come up, since we don't do this in fields, but just in case it does...
if ( Setting :: getSettings () -> digit_separator == '1.234,56' ) {
//EU format
$without_thousands = str_replace ( '.' , '' , $without_currency );
$corrected_decimal = str_replace ( ',' , '.' , $without_thousands );
} else {
$without_thousands = str_replace ( ',' , '' , $without_currency );
$corrected_decimal = $without_thousands ; // decimal is already OK
}
return floatval ( $corrected_decimal );
}
2016-03-25 01:18:05 -07:00
2016-10-25 11:11:47 -07:00
/**
* Get the list of status labels in an array to make a dropdown menu
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v2 . 5 ]
2021-06-10 13:15:52 -07:00
* @ return array
2016-10-25 11:11:47 -07:00
*/
2016-03-25 01:18:05 -07:00
public static function statusLabelList ()
{
2021-06-10 13:15:52 -07:00
$statuslabel_list = [ '' => trans ( 'general.select_statuslabel' )] + Statuslabel :: orderBy ( 'default_label' , 'desc' ) -> orderBy ( 'name' , 'asc' ) -> orderBy ( 'deployable' , 'desc' )
2016-05-24 00:49:56 -07:00
-> pluck ( 'name' , 'id' ) -> toArray ();
2021-06-10 13:15:52 -07:00
2016-03-25 01:18:05 -07:00
return $statuslabel_list ;
}
2021-02-03 01:29:54 -08:00
/**
* Get the list of deployable status labels in an array to make a dropdown menu
*
* @ todo This should probably be a selectlist , same as the other endpoints
* and we should probably add to the API controllers to make sure that
* the status_id submitted is actually really deployable .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v5 . 1.0 ]
2021-06-10 13:15:52 -07:00
* @ return array
2021-02-03 01:29:54 -08:00
*/
public static function deployableStatusLabelList ()
{
2021-06-10 13:15:52 -07:00
$statuslabel_list = Statuslabel :: where ( 'deployable' , '=' , '1' ) -> orderBy ( 'default_label' , 'desc' )
-> orderBy ( 'name' , 'asc' )
-> orderBy ( 'deployable' , 'desc' )
2021-02-03 01:29:54 -08:00
-> pluck ( 'name' , 'id' ) -> toArray ();
2021-06-10 13:15:52 -07:00
2021-02-03 01:29:54 -08:00
return $statuslabel_list ;
}
2016-10-25 11:11:47 -07:00
/**
* Get the list of status label types in an array to make a dropdown menu
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v2 . 5 ]
2021-06-10 13:15:52 -07:00
* @ return array
2016-10-25 11:11:47 -07:00
*/
2016-03-25 01:18:05 -07:00
public static function statusTypeList ()
{
2017-01-12 06:50:54 -08:00
$statuslabel_types =
2021-06-10 13:15:52 -07:00
[ '' => trans ( 'admin/hardware/form.select_statustype' )]
+ [ 'deployable' => trans ( 'admin/hardware/general.deployable' )]
+ [ 'pending' => trans ( 'admin/hardware/general.pending' )]
+ [ 'undeployable' => trans ( 'admin/hardware/general.undeployable' )]
+ [ 'archived' => trans ( 'admin/hardware/general.archived' )];
2016-03-25 01:18:05 -07:00
return $statuslabel_types ;
}
2016-10-25 11:11:47 -07:00
/**
* Get the list of depreciations in an array to make a dropdown menu
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v2 . 5 ]
2021-06-10 13:15:52 -07:00
* @ return array
2016-10-25 11:11:47 -07:00
*/
2016-03-25 01:18:05 -07:00
public static function depreciationList ()
{
$depreciation_list = [ '' => 'Do Not Depreciate' ] + Depreciation :: orderBy ( 'name' , 'asc' )
2016-05-24 00:49:56 -07:00
-> pluck ( 'name' , 'id' ) -> toArray ();
2021-06-10 13:15:52 -07:00
2016-03-25 01:18:05 -07:00
return $depreciation_list ;
}
2016-10-25 11:11:47 -07:00
/**
* Get the list of category types in an array to make a dropdown menu
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v2 . 5 ]
2021-06-10 13:15:52 -07:00
* @ return array
2016-10-25 11:11:47 -07:00
*/
2023-03-15 15:57:45 -07:00
public static function categoryTypeList ( $selection = null )
2016-03-25 01:18:05 -07:00
{
2021-06-10 13:15:52 -07:00
$category_types = [
2018-05-04 21:01:38 -07:00
'' => '' ,
2023-03-24 07:14:02 -07:00
'accessory' => trans ( 'general.accessory' ),
2023-03-13 13:33:03 -07:00
'asset' => trans ( 'general.asset' ),
'consumable' => trans ( 'general.consumable' ),
'component' => trans ( 'general.component' ),
'license' => trans ( 'general.license' ),
2021-06-10 13:15:52 -07:00
];
2023-07-19 00:56:17 -07:00
if ( $selection != null ){
return $category_types [ strtolower ( $selection )];
2023-03-13 13:33:03 -07:00
}
2023-03-15 15:57:45 -07:00
else
2016-05-24 00:49:56 -07:00
return $category_types ;
2016-03-25 01:18:05 -07:00
}
2016-10-25 11:11:47 -07:00
/**
* Get the list of custom fields in an array to make a dropdown menu
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v2 . 5 ]
2021-06-10 13:15:52 -07:00
* @ return array
2016-10-25 11:11:47 -07:00
*/
2016-03-25 01:18:05 -07:00
public static function customFieldsetList ()
{
2021-06-10 13:15:52 -07:00
$customfields = [ '' => trans ( 'admin/models/general.no_custom_field' )] + CustomFieldset :: pluck ( 'name' , 'id' ) -> toArray ();
2016-03-25 01:18:05 -07:00
return $customfields ;
}
2016-10-25 11:11:47 -07:00
/**
* Get the list of custom field formats in an array to make a dropdown menu
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v3 . 4 ]
2021-06-10 13:15:52 -07:00
* @ return array
2016-10-25 11:11:47 -07:00
*/
2016-03-25 01:18:05 -07:00
public static function predefined_formats ()
{
2018-12-06 20:05:04 -08:00
$keys = array_keys ( CustomField :: PREDEFINED_FORMATS );
2017-01-25 03:10:35 -08:00
$stuff = array_combine ( $keys , $keys );
2018-12-06 20:05:04 -08:00
2018-06-21 09:35:04 -07:00
return $stuff ;
2016-03-25 01:18:05 -07:00
}
2016-10-25 11:11:47 -07:00
/**
* Get the list of barcode dimensions
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v3 . 3 ]
2021-06-10 13:15:52 -07:00
* @ return array
2016-10-25 11:11:47 -07:00
*/
2016-03-25 01:18:05 -07:00
public static function barcodeDimensions ( $barcode_type = 'QRCODE' )
{
if ( $barcode_type == 'C128' ) {
$size [ 'height' ] = '-1' ;
$size [ 'width' ] = '-10' ;
} elseif ( $barcode_type == 'PDF417' ) {
$size [ 'height' ] = '-3' ;
$size [ 'width' ] = '-10' ;
} else {
$size [ 'height' ] = '-3' ;
$size [ 'width' ] = '-3' ;
}
2021-06-10 13:15:52 -07:00
2016-03-25 01:18:05 -07:00
return $size ;
}
2016-10-25 11:11:47 -07:00
/**
* Generates a random string
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v3 . 0 ]
2021-06-10 13:15:52 -07:00
* @ return array
2016-10-25 11:11:47 -07:00
*/
2016-03-25 01:18:05 -07:00
public static function generateRandomString ( $length = 10 )
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ;
$charactersLength = strlen ( $characters );
$randomString = '' ;
for ( $i = 0 ; $i < $length ; $i ++ ) {
$randomString .= $characters [ rand ( 0 , $charactersLength - 1 )];
}
2021-06-10 13:15:52 -07:00
2016-03-25 01:18:05 -07:00
return $randomString ;
}
2016-05-24 00:49:56 -07:00
/**
* This nasty little method gets the low inventory info for the
* alert dropdown
2016-10-25 11:11:47 -07:00
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v3 . 0 ]
2021-06-10 13:15:52 -07:00
* @ return array
2016-10-25 11:11:47 -07:00
*/
2016-03-25 01:18:05 -07:00
public static function checkLowInventory ()
{
2018-07-24 22:40:05 -07:00
$consumables = Consumable :: withCount ( 'consumableAssignments as consumable_assignments_count' ) -> whereNotNull ( 'min_amt' ) -> get ();
$accessories = Accessory :: withCount ( 'users as users_count' ) -> whereNotNull ( 'min_amt' ) -> get ();
2022-08-04 05:58:25 -07:00
$components = Component :: whereNotNull ( 'min_amt' ) -> get ();
2016-03-25 01:18:05 -07:00
$avail_consumables = 0 ;
2021-06-10 13:15:52 -07:00
$items_array = [];
2016-03-25 01:18:05 -07:00
$all_count = 0 ;
foreach ( $consumables as $consumable ) {
2018-07-12 16:45:12 -07:00
$avail = $consumable -> numRemaining ();
2016-05-19 19:33:53 -07:00
if ( $avail < ( $consumable -> min_amt ) + \App\Models\Setting :: getSettings () -> alert_threshold ) {
Partialize forms (#2884)
* Consolidate edit form elements into reusable partials.
This is a large code change that doesn't do much immediately. It
refactors all of the various edit.blade.php files to reference
standardized partials, so that they all reference the same base html
layout. This has the side effect of moving everything to the new fancy
"required" indicators, and making things look consistent.
In addition, I've gone ahead and renamed a few database fields. We had
Assetmodel::modelno and Consumable::model_no, I've renamed both to
model_number. We had items using ::note and ::notes, I've standardized
on ::notes. Component used total_qty where consumables and accessories
used qty, so I've moved everything to qty (And fixed a few bugs in the
helper file in the process.
TODO includes looking at how/where to place the modal javascripts to
allow for on the fly creation from all places, rather than just the
asset page.
Rename assetmodel::modelno to model_number for clarity and consistency
Rename consumable::model_no to model_number for clarity and consistency
Rename assetmodel::note to notes for clarity and consistency
Port asset and assetmodel to new partials layout. Adapt all code to the renamed model_number and notes database changes. Fix some stying.
* Share a settings variable with all views.
* Allow editing the per_page setting. We showed the value, but we never showed it on the edit page..
* use snipeSettings in all views instead of the long ugly path.
* War on partials. Centralize all bootstrap table javascript
* Use model_number instead of modelno in importer
* Codacy fix.
* More unification/deduplication. Create an edit form template layout that we use as the base for all edit forms. This gives the same interface for editing everything and makes the edit.blade.* files much easier to read.
* Use a ViewComposer instead of sharing the variable directly. Fixes artisan optimize trying to hit the db--which ruins new installs
* Fix DB seeder.
* Base sql dump and csv's to import data from for tests.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* A few things to make acceptance tests work. Add a name to the companies table, and make the locations table have the correct name
* Use a .env.tests file for testing functional and unit to allow a separate database.
* Add functional tests for compoents, groups, and licenses.
* Now that the config is in the functional.yml, this just confuses things.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* update db dump
* Update tests to new reality
* env for the test setup
* only load the database at beginning of tests, not between each Functional test.
* Fix a miss from renaming note to notes.
* Set Termination date when creating an asset. It was only set on edit.
* Rename serial_number to serial in components for consistency.
* Update validation rules to match limits in database. Currently we just accepted the values and they were truncated when adding to DB.
* Much more detailed functional testing of creating items. This checks to make sure all values on form have been successfully persisted to database.
2016-11-16 16:56:57 -08:00
if ( $consumable -> qty > 0 ) {
2017-12-19 00:32:39 -08:00
$percent = number_format ((( $avail / $consumable -> qty ) * 100 ), 0 );
2016-05-24 00:49:56 -07:00
} else {
$percent = 100 ;
}
2016-03-25 01:18:05 -07:00
$items_array [ $all_count ][ 'id' ] = $consumable -> id ;
$items_array [ $all_count ][ 'name' ] = $consumable -> name ;
$items_array [ $all_count ][ 'type' ] = 'consumables' ;
$items_array [ $all_count ][ 'percent' ] = $percent ;
2017-12-19 00:32:39 -08:00
$items_array [ $all_count ][ 'remaining' ] = $avail ;
2021-06-10 13:15:52 -07:00
$items_array [ $all_count ][ 'min_amt' ] = $consumable -> min_amt ;
2016-03-25 01:18:05 -07:00
$all_count ++ ;
}
}
foreach ( $accessories as $accessory ) {
2017-12-19 00:32:39 -08:00
$avail = $accessory -> qty - $accessory -> users_count ;
2016-05-19 19:33:53 -07:00
if ( $avail < ( $accessory -> min_amt ) + \App\Models\Setting :: getSettings () -> alert_threshold ) {
Partialize forms (#2884)
* Consolidate edit form elements into reusable partials.
This is a large code change that doesn't do much immediately. It
refactors all of the various edit.blade.php files to reference
standardized partials, so that they all reference the same base html
layout. This has the side effect of moving everything to the new fancy
"required" indicators, and making things look consistent.
In addition, I've gone ahead and renamed a few database fields. We had
Assetmodel::modelno and Consumable::model_no, I've renamed both to
model_number. We had items using ::note and ::notes, I've standardized
on ::notes. Component used total_qty where consumables and accessories
used qty, so I've moved everything to qty (And fixed a few bugs in the
helper file in the process.
TODO includes looking at how/where to place the modal javascripts to
allow for on the fly creation from all places, rather than just the
asset page.
Rename assetmodel::modelno to model_number for clarity and consistency
Rename consumable::model_no to model_number for clarity and consistency
Rename assetmodel::note to notes for clarity and consistency
Port asset and assetmodel to new partials layout. Adapt all code to the renamed model_number and notes database changes. Fix some stying.
* Share a settings variable with all views.
* Allow editing the per_page setting. We showed the value, but we never showed it on the edit page..
* use snipeSettings in all views instead of the long ugly path.
* War on partials. Centralize all bootstrap table javascript
* Use model_number instead of modelno in importer
* Codacy fix.
* More unification/deduplication. Create an edit form template layout that we use as the base for all edit forms. This gives the same interface for editing everything and makes the edit.blade.* files much easier to read.
* Use a ViewComposer instead of sharing the variable directly. Fixes artisan optimize trying to hit the db--which ruins new installs
* Fix DB seeder.
* Base sql dump and csv's to import data from for tests.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* A few things to make acceptance tests work. Add a name to the companies table, and make the locations table have the correct name
* Use a .env.tests file for testing functional and unit to allow a separate database.
* Add functional tests for compoents, groups, and licenses.
* Now that the config is in the functional.yml, this just confuses things.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* update db dump
* Update tests to new reality
* env for the test setup
* only load the database at beginning of tests, not between each Functional test.
* Fix a miss from renaming note to notes.
* Set Termination date when creating an asset. It was only set on edit.
* Rename serial_number to serial in components for consistency.
* Update validation rules to match limits in database. Currently we just accepted the values and they were truncated when adding to DB.
* Much more detailed functional testing of creating items. This checks to make sure all values on form have been successfully persisted to database.
2016-11-16 16:56:57 -08:00
if ( $accessory -> qty > 0 ) {
2017-12-19 00:32:39 -08:00
$percent = number_format ((( $avail / $accessory -> qty ) * 100 ), 0 );
2016-05-24 00:49:56 -07:00
} else {
$percent = 100 ;
}
2016-03-25 01:18:05 -07:00
$items_array [ $all_count ][ 'id' ] = $accessory -> id ;
$items_array [ $all_count ][ 'name' ] = $accessory -> name ;
$items_array [ $all_count ][ 'type' ] = 'accessories' ;
$items_array [ $all_count ][ 'percent' ] = $percent ;
2017-12-19 00:32:39 -08:00
$items_array [ $all_count ][ 'remaining' ] = $avail ;
2021-06-10 13:15:52 -07:00
$items_array [ $all_count ][ 'min_amt' ] = $accessory -> min_amt ;
2016-03-25 01:18:05 -07:00
$all_count ++ ;
}
}
foreach ( $components as $component ) {
2022-08-04 07:20:20 -07:00
$avail = $component -> numRemaining ();
2016-05-19 19:33:53 -07:00
if ( $avail < ( $component -> min_amt ) + \App\Models\Setting :: getSettings () -> alert_threshold ) {
Partialize forms (#2884)
* Consolidate edit form elements into reusable partials.
This is a large code change that doesn't do much immediately. It
refactors all of the various edit.blade.php files to reference
standardized partials, so that they all reference the same base html
layout. This has the side effect of moving everything to the new fancy
"required" indicators, and making things look consistent.
In addition, I've gone ahead and renamed a few database fields. We had
Assetmodel::modelno and Consumable::model_no, I've renamed both to
model_number. We had items using ::note and ::notes, I've standardized
on ::notes. Component used total_qty where consumables and accessories
used qty, so I've moved everything to qty (And fixed a few bugs in the
helper file in the process.
TODO includes looking at how/where to place the modal javascripts to
allow for on the fly creation from all places, rather than just the
asset page.
Rename assetmodel::modelno to model_number for clarity and consistency
Rename consumable::model_no to model_number for clarity and consistency
Rename assetmodel::note to notes for clarity and consistency
Port asset and assetmodel to new partials layout. Adapt all code to the renamed model_number and notes database changes. Fix some stying.
* Share a settings variable with all views.
* Allow editing the per_page setting. We showed the value, but we never showed it on the edit page..
* use snipeSettings in all views instead of the long ugly path.
* War on partials. Centralize all bootstrap table javascript
* Use model_number instead of modelno in importer
* Codacy fix.
* More unification/deduplication. Create an edit form template layout that we use as the base for all edit forms. This gives the same interface for editing everything and makes the edit.blade.* files much easier to read.
* Use a ViewComposer instead of sharing the variable directly. Fixes artisan optimize trying to hit the db--which ruins new installs
* Fix DB seeder.
* Base sql dump and csv's to import data from for tests.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* A few things to make acceptance tests work. Add a name to the companies table, and make the locations table have the correct name
* Use a .env.tests file for testing functional and unit to allow a separate database.
* Add functional tests for compoents, groups, and licenses.
* Now that the config is in the functional.yml, this just confuses things.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* update db dump
* Update tests to new reality
* env for the test setup
* only load the database at beginning of tests, not between each Functional test.
* Fix a miss from renaming note to notes.
* Set Termination date when creating an asset. It was only set on edit.
* Rename serial_number to serial in components for consistency.
* Update validation rules to match limits in database. Currently we just accepted the values and they were truncated when adding to DB.
* Much more detailed functional testing of creating items. This checks to make sure all values on form have been successfully persisted to database.
2016-11-16 16:56:57 -08:00
if ( $component -> qty > 0 ) {
2017-12-19 00:32:39 -08:00
$percent = number_format ((( $avail / $component -> qty ) * 100 ), 0 );
2016-05-24 00:49:56 -07:00
} else {
$percent = 100 ;
}
2016-03-25 01:18:05 -07:00
$items_array [ $all_count ][ 'id' ] = $component -> id ;
$items_array [ $all_count ][ 'name' ] = $component -> name ;
$items_array [ $all_count ][ 'type' ] = 'components' ;
$items_array [ $all_count ][ 'percent' ] = $percent ;
2017-12-19 00:32:39 -08:00
$items_array [ $all_count ][ 'remaining' ] = $avail ;
2021-06-10 13:15:52 -07:00
$items_array [ $all_count ][ 'min_amt' ] = $component -> min_amt ;
2016-03-25 01:18:05 -07:00
$all_count ++ ;
}
}
return $items_array ;
}
2016-03-25 20:38:27 -07:00
2016-10-25 11:11:47 -07:00
/**
* Check if the file is an image , so we can show a preview
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v3 . 0 ]
* @ param File $file
2021-06-10 13:15:52 -07:00
* @ return string | Boolean
2016-10-25 11:11:47 -07:00
*/
2016-03-25 20:38:27 -07:00
public static function checkUploadIsImage ( $file )
{
$finfo = @ finfo_open ( FILEINFO_MIME_TYPE ); // return mime type ala mimetype extension
$filetype = @ finfo_file ( $finfo , $file );
finfo_close ( $finfo );
2021-06-10 13:15:52 -07:00
if (( $filetype == 'image/jpeg' ) || ( $filetype == 'image/jpg' ) || ( $filetype == 'image/png' ) || ( $filetype == 'image/bmp' ) || ( $filetype == 'image/gif' )) {
2016-03-25 20:38:27 -07:00
return $filetype ;
}
return false ;
}
2016-04-28 20:56:47 -07:00
/**
2016-05-24 00:49:56 -07:00
* Walks through the permissions in the permissions config file and determines if
* permissions are granted based on a $selected_arr array .
*
* The $permissions array is a multidimensional array broke down by section .
* ( Licenses , Assets , etc )
*
* The $selected_arr should be a flattened array that contains just the
* corresponding permission name and a true or false boolean to determine
* if that group / user has been granted that permission .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net ]
* @ param array $permissions
* @ param array $selected_arr
* @ since [ v1 . 0 ]
2021-06-10 13:15:52 -07:00
* @ return array
2016-05-24 00:49:56 -07:00
*/
2021-06-10 13:15:52 -07:00
public static function selectedPermissionsArray ( $permissions , $selected_arr = [])
2016-04-28 20:56:47 -07:00
{
2021-06-10 13:15:52 -07:00
$permissions_arr = [];
2016-04-28 20:56:47 -07:00
foreach ( $permissions as $permission ) {
for ( $x = 0 ; $x < count ( $permission ); $x ++ ) {
$permission_name = $permission [ $x ][ 'permission' ];
if ( $permission [ $x ][ 'display' ] === true ) {
if ( $selected_arr ) {
2016-12-29 14:02:18 -08:00
if ( array_key_exists ( $permission_name , $selected_arr )) {
2016-06-15 20:45:45 -07:00
$permissions_arr [ $permission_name ] = $selected_arr [ $permission_name ];
2016-04-28 20:56:47 -07:00
} else {
2016-05-09 15:38:29 -07:00
$permissions_arr [ $permission_name ] = '0' ;
2016-04-28 20:56:47 -07:00
}
} else {
2016-05-09 15:38:29 -07:00
$permissions_arr [ $permission_name ] = '0' ;
2016-04-28 20:56:47 -07:00
}
}
}
}
2016-05-24 00:49:56 -07:00
2016-04-28 20:56:47 -07:00
return $permissions_arr ;
}
2016-07-22 00:29:23 -07:00
/**
* Introspects into the model validation to see if the field passed is required .
* This is used by the blades to add a required class onto the HTML element .
* This isn ' t critical , but is helpful to keep form fields in sync with the actual
* model level validation .
*
* This does not currently handle form request validation requiredness : (
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v3 . 0 ]
2021-06-10 13:15:52 -07:00
* @ return bool
2016-07-22 00:29:23 -07:00
*/
2016-12-29 14:02:18 -08:00
public static function checkIfRequired ( $class , $field )
{
2016-07-22 00:29:23 -07:00
$rules = $class :: rules ();
foreach ( $rules as $rule_name => $rule ) {
if ( $rule_name == $field ) {
if ( strpos ( $rule , 'required' ) === false ) {
return false ;
} else {
return true ;
}
}
}
}
2016-08-12 16:01:45 -07:00
/**
* Check to see if the given key exists in the array , and trim excess white space before returning it
*
* @ author Daniel Melzter
* @ since 3.0
* @ param $array array
* @ param $key string
* @ param $default string
* @ return string
*/
public static function array_smart_fetch ( array $array , $key , $default = '' )
{
2016-12-29 14:02:18 -08:00
array_change_key_case ( $array , CASE_LOWER );
2016-08-12 16:01:45 -07:00
2021-06-10 13:15:52 -07:00
return array_key_exists ( strtolower ( $key ), array_change_key_case ( $array )) ? e ( trim ( $array [ $key ])) : $default ;
}
2016-08-12 16:01:45 -07:00
2016-10-25 11:11:47 -07:00
/**
2021-04-06 05:59:31 -07:00
* Gracefully handle decrypting encrypted fields ( custom fields , etc ) .
2016-10-25 11:11:47 -07:00
*
2021-04-06 05:59:31 -07:00
* @ todo allow this to handle more than just strings ( arrays , etc )
2016-10-25 11:11:47 -07:00
*
* @ author A . Gianotto
* @ since 3.6
* @ param CustomField $field
2021-06-10 13:15:52 -07:00
* @ param string $string
2016-10-25 11:11:47 -07:00
* @ return string
*/
2016-12-29 14:02:18 -08:00
public static function gracefulDecrypt ( CustomField $field , $string )
{
2016-08-25 20:58:56 -07:00
if ( $field -> isFieldDecryptable ( $string )) {
try {
Crypt :: decrypt ( $string );
2021-06-10 13:15:52 -07:00
return Crypt :: decrypt ( $string );
2016-08-25 20:58:56 -07:00
} catch ( DecryptException $e ) {
return 'Error Decrypting: ' . $e -> getMessage ();
}
2021-09-21 23:46:50 -07:00
}
2016-08-25 20:58:56 -07:00
return $string ;
}
2021-06-10 13:15:52 -07:00
public static function formatStandardApiResponse ( $status , $payload = null , $messages = null )
2016-08-12 16:01:45 -07:00
2021-06-10 13:15:52 -07:00
{
2017-01-12 02:19:17 -08:00
$array [ 'status' ] = $status ;
2017-08-25 03:27:31 -07:00
$array [ 'messages' ] = $messages ;
2021-06-10 13:15:52 -07:00
if (( $messages ) && ( is_array ( $messages )) && ( count ( $messages ) > 0 )) {
2017-01-12 02:19:17 -08:00
$array [ 'messages' ] = $messages ;
}
2017-08-25 03:27:31 -07:00
( $payload ) ? $array [ 'payload' ] = $payload : $array [ 'payload' ] = null ;
2021-06-10 13:15:52 -07:00
2017-01-12 02:19:17 -08:00
return $array ;
}
2017-01-25 03:10:35 -08:00
/*
Possible solution for unicode fieldnames
*/
2021-06-10 13:15:52 -07:00
public static function make_slug ( $string )
{
2017-01-25 03:10:35 -08:00
return preg_replace ( '/\s+/u' , '_' , trim ( $string ));
}
2022-06-03 15:25:39 -07:00
/**
* Return an array ( or null ) of the the raw and formatted date object for easy use in
* the API and the bootstrap table listings .
*
* @ param $date
* @ param $type
* @ param $array
* @ return array | string | null
*/
2022-06-03 15:30:34 -07:00
2021-06-10 13:15:52 -07:00
public static function getFormattedDateObject ( $date , $type = 'datetime' , $array = true )
{
if ( $date == '' ) {
2017-03-03 17:29:41 -08:00
return null ;
}
$settings = Setting :: getSettings ();
2022-06-03 15:25:39 -07:00
/**
* Wrap this in a try / catch so that if Carbon crashes , for example if the $date value
* isn 't actually valid, we don' t crash out completely .
*
* While this * shouldn ' t * typically happen since we validate dates before entering them
* into the database ( and we use date / datetime fields for native fields in the system ),
2022-06-03 15:28:00 -07:00
* it is a possible scenario that a custom field could be created as an " ANY " field , data gets
2022-06-03 15:25:39 -07:00
* added , and then the custom field format gets edited later . If someone put bad data in the
* database before then - or if they manually edited the field ' s value - it will crash .
*
*/
2022-06-03 15:30:34 -07:00
2022-06-03 15:25:39 -07:00
try {
$tmp_date = new \Carbon ( $date );
if ( $type == 'datetime' ) {
$dt [ 'datetime' ] = $tmp_date -> format ( 'Y-m-d H:i:s' );
$dt [ 'formatted' ] = $tmp_date -> format ( $settings -> date_display_format . ' ' . $settings -> time_display_format );
} else {
$dt [ 'date' ] = $tmp_date -> format ( 'Y-m-d' );
$dt [ 'formatted' ] = $tmp_date -> format ( $settings -> date_display_format );
}
if ( $array == 'true' ) {
return $dt ;
}
return $dt [ 'formatted' ];
2017-03-03 17:29:41 -08:00
2022-06-03 15:25:39 -07:00
} catch ( \Exception $e ) {
\Log :: warning ( $e );
2022-06-03 15:36:52 -07:00
return $date . ' (Invalid ' . $type . ' value.)' ;
2017-03-03 17:29:41 -08:00
}
}
2018-02-16 21:17:41 -08:00
// Nicked from Drupal :)
// Returns a file size limit in bytes based on the PHP upload_max_filesize
// and post_max_size
2021-06-10 13:15:52 -07:00
public static function file_upload_max_size ()
{
2018-02-16 21:17:41 -08:00
static $max_size = - 1 ;
if ( $max_size < 0 ) {
2018-05-04 21:01:38 -07:00
2018-02-16 21:17:41 -08:00
// Start with post_max_size.
2021-06-10 13:15:52 -07:00
$post_max_size = self :: parse_size ( ini_get ( 'post_max_size' ));
2018-02-16 21:17:41 -08:00
if ( $post_max_size > 0 ) {
$max_size = $post_max_size ;
}
// If upload_max_size is less, then reduce. Except if upload_max_size is
// zero, which indicates no limit.
2021-06-10 13:15:52 -07:00
$upload_max = self :: parse_size ( ini_get ( 'upload_max_filesize' ));
2018-02-16 21:17:41 -08:00
if ( $upload_max > 0 && $upload_max < $max_size ) {
$max_size = $upload_max ;
}
}
2018-05-04 21:01:38 -07:00
2018-02-16 21:17:41 -08:00
return $max_size ;
}
2021-06-10 13:15:52 -07:00
public static function file_upload_max_size_readable ()
{
2018-02-16 21:17:41 -08:00
static $max_size = - 1 ;
if ( $max_size < 0 ) {
// Start with post_max_size.
2021-06-10 13:15:52 -07:00
$post_max_size = self :: parse_size ( ini_get ( 'post_max_size' ));
2018-02-16 21:17:41 -08:00
if ( $post_max_size > 0 ) {
$max_size = ini_get ( 'post_max_size' );
}
// If upload_max_size is less, then reduce. Except if upload_max_size is
// zero, which indicates no limit.
2021-06-10 13:15:52 -07:00
$upload_max = self :: parse_size ( ini_get ( 'upload_max_filesize' ));
2021-08-25 13:27:25 -07:00
if ( $upload_max > 0 && $upload_max < $post_max_size ) {
2018-02-16 21:17:41 -08:00
$max_size = ini_get ( 'upload_max_filesize' );
}
}
2021-06-10 13:15:52 -07:00
2018-02-16 21:17:41 -08:00
return $max_size ;
}
2021-06-10 13:15:52 -07:00
public static function parse_size ( $size )
{
2018-02-16 21:17:41 -08:00
$unit = preg_replace ( '/[^bkmgtpezy]/i' , '' , $size ); // Remove the non-unit characters from the size.
$size = preg_replace ( '/[^0-9\.]/' , '' , $size ); // Remove the non-numeric characters from the size.
if ( $unit ) {
// Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
return round ( $size * pow ( 1024 , stripos ( 'bkmgtpezy' , $unit [ 0 ])));
2021-06-10 13:15:52 -07:00
} else {
2018-02-16 21:17:41 -08:00
return round ( $size );
}
}
2021-06-10 13:15:52 -07:00
public static function filetype_icon ( $filename )
{
$extension = substr ( strrchr ( $filename , '.' ), 1 );
2018-05-02 14:13:06 -07:00
2020-05-23 09:17:56 -07:00
$allowedExtensionMap = [
// Images
2021-09-24 06:07:46 -07:00
'jpg' => 'far fa-image' ,
'jpeg' => 'far fa-image' ,
'gif' => 'far fa-image' ,
'png' => 'far fa-image' ,
2020-05-23 09:17:56 -07:00
// word
2021-09-24 06:07:46 -07:00
'doc' => 'far fa-file-word' ,
'docx' => 'far fa-file-word' ,
2020-05-23 09:17:56 -07:00
// Excel
2021-09-24 06:07:46 -07:00
'xls' => 'far fa-file-excel' ,
'xlsx' => 'far fa-file-excel' ,
2020-05-23 09:17:56 -07:00
// archive
2021-09-24 06:07:46 -07:00
'zip' => 'fas fa-file-archive' ,
'rar' => 'fas fa-file-archive' ,
2020-05-23 09:17:56 -07:00
//Text
2021-09-24 06:07:46 -07:00
'txt' => 'far fa-file-alt' ,
'rtf' => 'far fa-file-alt' ,
'xml' => 'far fa-file-alt' ,
2020-05-23 09:17:56 -07:00
// Misc
2021-09-24 06:07:46 -07:00
'pdf' => 'far fa-file-pdf' ,
'lic' => 'far fa-save' ,
2020-05-23 09:17:56 -07:00
];
if ( $extension && array_key_exists ( $extension , $allowedExtensionMap )) {
return $allowedExtensionMap [ $extension ];
2018-05-02 14:13:06 -07:00
}
2021-09-24 06:07:46 -07:00
return 'far fa-file' ;
2021-06-10 13:15:52 -07:00
}
2018-05-02 14:13:06 -07:00
2021-06-10 13:15:52 -07:00
public static function show_file_inline ( $filename )
{
$extension = substr ( strrchr ( $filename , '.' ), 1 );
2018-05-02 14:13:06 -07:00
if ( $extension ) {
switch ( $extension ) {
case 'jpg' :
case 'jpeg' :
case 'gif' :
case 'png' :
return true ;
break ;
default :
return false ;
}
}
2021-06-10 13:15:52 -07:00
2018-05-02 14:13:06 -07:00
return false ;
}
2019-01-15 14:05:47 -08:00
/**
* Generate a random encrypted password .
*
* @ author Wes Hulette < jwhulette @ gmail . com >
*
* @ since 5.0 . 0
*
* @ return string
*/
public static function generateEncyrptedPassword () : string
{
2021-06-10 13:15:52 -07:00
return bcrypt ( self :: generateUnencryptedPassword ());
2019-01-15 14:05:47 -08:00
}
2018-05-02 14:13:06 -07:00
2019-01-15 14:05:47 -08:00
/**
* Get a random unencrypted password .
*
* @ author Steffen Buehl < sb @ sbuehl . com >
*
* @ since 5.0 . 0
*
* @ return string
*/
public static function generateUnencryptedPassword () : string
{
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ;
2018-02-16 21:17:41 -08:00
2019-01-15 14:05:47 -08:00
$password = '' ;
2021-06-10 13:15:52 -07:00
for ( $i = 0 ; $i < 20 ; $i ++ ) {
$password .= substr ( $chars , random_int ( 0 , strlen ( $chars ) - 1 ), 1 );
2019-01-15 14:05:47 -08:00
}
2021-06-10 13:15:52 -07:00
2019-01-15 14:05:47 -08:00
return $password ;
}
2019-03-13 21:00:40 -07:00
/**
* Process base64 encoded image data and save it on supplied path
*
* @ param string $image_data base64 encoded image data with mime type
* @ param string $save_path path to a folder where the image should be saved
* @ return string path to uploaded image or false if something went wrong
*/
2021-06-10 13:15:52 -07:00
public static function processUploadedImage ( String $image_data , String $save_path )
{
2021-04-05 22:03:15 -07:00
if ( $image_data == null || $save_path == null ) {
return false ;
}
2019-03-13 21:00:40 -07:00
2021-04-05 22:03:15 -07:00
// After modification, the image is prefixed by mime info like the following:
// data:image/jpeg;base64,; This causes the image library to be unhappy, so we need to remove it.
$header = explode ( ';' , $image_data , 2 )[ 0 ];
// Grab the image type from the header while we're at it.
2021-06-10 13:15:52 -07:00
$extension = substr ( $header , strpos ( $header , '/' ) + 1 );
2021-04-05 22:03:15 -07:00
// Start reading the image after the first comma, postceding the base64.
2021-06-10 13:15:52 -07:00
$image = substr ( $image_data , strpos ( $image_data , ',' ) + 1 );
2019-03-13 21:00:40 -07:00
2021-06-10 13:15:52 -07:00
$file_name = str_random ( 25 ) . '.' . $extension ;
2019-03-13 21:00:40 -07:00
2021-06-10 13:15:52 -07:00
$directory = public_path ( $save_path );
2021-04-05 22:03:15 -07:00
// Check if the uploads directory exists. If not, try to create it.
2021-06-10 13:15:52 -07:00
if ( ! file_exists ( $directory )) {
2021-04-05 22:03:15 -07:00
mkdir ( $directory , 0755 , true );
2019-03-13 21:00:40 -07:00
}
2021-04-05 22:03:15 -07:00
$path = public_path ( $save_path . $file_name );
try {
Image :: make ( $image ) -> resize ( 500 , 500 , function ( $constraint ) {
$constraint -> aspectRatio ();
$constraint -> upsize ();
}) -> save ( $path );
} catch ( \Exception $e ) {
return false ;
}
return $file_name ;
2019-03-13 21:00:40 -07:00
}
2022-05-13 19:33:14 -07:00
2023-03-08 16:47:08 -08:00
/**
* Universal helper to show file size in human - readable formats
*
* @ author A . Gianotto < snipe @ snipe . net >
* @ since 5.0
*
* @ return string []
*/
2022-05-13 19:33:14 -07:00
public static function formatFilesizeUnits ( $bytes )
{
if ( $bytes >= 1073741824 )
{
$bytes = number_format ( $bytes / 1073741824 , 2 ) . ' GB' ;
}
elseif ( $bytes >= 1048576 )
{
$bytes = number_format ( $bytes / 1048576 , 2 ) . ' MB' ;
}
elseif ( $bytes >= 1024 )
{
$bytes = number_format ( $bytes / 1024 , 2 ) . ' KB' ;
}
elseif ( $bytes > 1 )
{
$bytes = $bytes . ' bytes' ;
}
elseif ( $bytes == 1 )
{
$bytes = $bytes . ' byte' ;
}
else
{
$bytes = '0 bytes' ;
}
return $bytes ;
}
2023-03-08 16:47:27 -08:00
/**
* This is weird but used by the side nav to determine which URL to point the user to
*
* @ author A . Gianotto < snipe @ snipe . net >
* @ since 5.0
*
* @ return string []
*/
2022-05-18 13:06:06 -07:00
public static function SettingUrls (){
$settings = [ '#' , 'fields.index' , 'statuslabels.index' , 'models.index' , 'categories.index' , 'manufacturers.index' , 'suppliers.index' , 'departments.index' , 'locations.index' , 'companies.index' , 'depreciations.index' ];
2022-05-23 09:35:19 -07:00
return $settings ;
2022-05-18 13:06:06 -07:00
}
2023-03-08 16:36:08 -08:00
/**
* Generic helper ( largely used by livewire right now ) that returns the font - awesome icon
* for the object type .
*
* @ author A . Gianotto < snipe @ snipe . net >
* @ since 6.1 . 0
*
* @ return string
*/
2023-03-08 16:34:04 -08:00
public static function iconTypeByItem ( $item ) {
switch ( $item ) {
case 'asset' :
return 'fas fa-barcode' ;
break ;
case 'accessory' :
return 'fas fa-keyboard' ;
break ;
case 'component' :
return 'fas fa-hdd' ;
break ;
case 'consumable' :
return 'fas fa-tint' ;
break ;
case 'license' :
return 'far fa-save' ;
break ;
case 'location' :
return 'fas fa-map-marker-alt' ;
break ;
case 'user' :
return 'fas fa-user' ;
break ;
2022-11-29 09:51:42 -08:00
}
}
2023-03-21 18:59:34 -07:00
2023-03-21 19:22:18 -07:00
/*
* This is a shorter way to see if the app is in demo mode .
*
* This makes it cleanly available in blades and in controllers , e . g .
*
* Blade :
2023-03-21 19:27:05 -07:00
* {{ Helper :: isDemoMode () ? ' disabled' : '' }} for form blades where we need to disable a form
2023-03-21 19:22:18 -07:00
*
* Controller :
2023-03-21 19:27:05 -07:00
* if ( Helper :: isDemoMode ()) {
2023-03-21 19:22:18 -07:00
* // don't allow the thing
* }
* @ todo - use this everywhere else in the app where we have very long if / else config ( 'app.lock_passwords' ) stuff
*/
2023-03-21 21:21:01 -07:00
public static function isDemoMode () {
2023-03-21 19:22:18 -07:00
if ( config ( 'app.lock_passwords' ) === true ) {
return true ;
\Log :: debug ( 'app locked!' );
2022-11-29 09:51:42 -08:00
}
2023-01-23 17:53:42 -08:00
2023-07-30 16:57:29 -07:00
return false ;
2023-01-23 17:53:42 -08:00
}
2022-05-18 13:06:06 -07:00
2023-07-30 16:57:29 -07:00
2022-11-01 04:49:57 -07:00
/**
* Conversion between units of measurement
*
* @ author Grant Le Roux < grant . leroux + snipe - it @ gmail . com >
* @ since 5.0
* @ param float $value Measurement value to convert
* @ param string $srcUnit Source unit of measurement
* @ param string $dstUnit Destination unit of measurement
* @ param int $round Round the result to decimals ( Default false - No rounding )
* @ return float
*/
public static function convertUnit ( $value , $srcUnit , $dstUnit , $round = false ) {
$srcFactor = static :: getUnitConversionFactor ( $srcUnit );
$dstFactor = static :: getUnitConversionFactor ( $dstUnit );
$output = $value * $srcFactor / $dstFactor ;
return ( $round !== false ) ? round ( $output , $round ) : $output ;
}
2023-07-30 16:57:29 -07:00
2022-11-01 04:49:57 -07:00
/**
* Get conversion factor from unit of measurement to mm
*
* @ author Grant Le Roux < grant . leroux + snipe - it @ gmail . com >
* @ since 5.0
* @ param string $unit Unit of measurement
* @ return float
*/
public static function getUnitConversionFactor ( $unit ) {
switch ( strtolower ( $unit )) {
case 'mm' : return 1.0 ;
case 'cm' : return 10.0 ;
case 'm' : return 1000.0 ;
case 'in' : return 25.4 ;
case 'ft' : return 12 * static :: getUnitConversionFactor ( 'in' );
case 'yd' : return 3 * static :: getUnitConversionFactor ( 'ft' );
case 'pt' : return ( 1 / 72 ) * static :: getUnitConversionFactor ( 'in' );
default : throw new \InvalidArgumentException ( 'Unit: \'' . $unit . '\' is not supported' );
2023-03-21 19:22:18 -07:00
return false ;
}
2023-03-21 18:59:34 -07:00
/*
* I know it ' s gauche to return a shitty HTML string , but this is just a helper and since it will be the same every single time ,
* it seemed pretty safe to do here . Don ' t you judge me .
*/
2023-03-21 21:21:01 -07:00
public static function showDemoModeFieldWarning () {
2023-03-21 19:22:18 -07:00
if ( Helper :: isDemoMode ()) {
2023-03-21 18:59:34 -07:00
return " <p class= \" text-warning \" ><i class= \" fas fa-lock \" ></i> " . trans ( 'general.feature_disabled' ) . " </p> " ;
2022-11-01 04:49:57 -07:00
}
}
2016-03-25 01:18:05 -07:00
}