mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-26 14:09:43 -08:00
e9b056f66c
* Pave the imports table * Format error response if file is the wrong type. * If a custom field doesn't exist, don't insert a blank string into the custom fields table * CustomField::db_column_name can return the stored name in the db. It's slugified when that value is set initially. This fixes a weird issue where _1 was replaced with _xx
179 lines
5.3 KiB
PHP
179 lines
5.3 KiB
PHP
<?php
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Schema;
|
|
use Watson\Validating\ValidatingTrait;
|
|
use App\Http\Traits\UniqueUndeletedTrait;
|
|
use ForceUTF8\Encoding;
|
|
use EasySlugger\Utf8Slugger;
|
|
use Patchwork\Utf8;
|
|
|
|
class CustomField extends Model
|
|
{
|
|
use ValidatingTrait, UniqueUndeletedTrait;
|
|
public $guarded=["id"];
|
|
public static $PredefinedFormats=[
|
|
"ANY" => "",
|
|
"ALPHA" => "alpha",
|
|
"EMAIL" => "email",
|
|
"DATE" => "date",
|
|
"URL" => "url",
|
|
"NUMERIC" => "numeric",
|
|
"MAC" => "regex:/^[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}$/",
|
|
"IP" => "ip",
|
|
];
|
|
|
|
public $rules = [
|
|
"name" => "required|unique:custom_fields"
|
|
];
|
|
|
|
public static $table_name="assets";
|
|
|
|
public static function name_to_db_name($name)
|
|
{
|
|
return "_snipeit_" . preg_replace("/[^a-zA-Z0-9]/", "_", strtolower($name));
|
|
}
|
|
|
|
public static function boot()
|
|
{
|
|
self::created(function ($custom_field) {
|
|
\Log::debug("\n\nCreating Original Name: ".$custom_field->name);
|
|
\Log::debug('Creating Column Name: '.$custom_field->convertUnicodeDbSlug());
|
|
|
|
|
|
if (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug())) {
|
|
\Log::debug('Column exists. Nothing to do here.');
|
|
return false;
|
|
}
|
|
|
|
Schema::table(CustomField::$table_name, function ($table) use ($custom_field) {
|
|
$table->text($custom_field->convertUnicodeDbSlug())->nullable();
|
|
});
|
|
|
|
$custom_field->db_column = $custom_field->convertUnicodeDbSlug();
|
|
$custom_field->save();
|
|
});
|
|
|
|
|
|
self::updating(function ($custom_field) {
|
|
\Log::debug('Updating column name');
|
|
\Log::debug('Updating Original Name: '.$custom_field->getOriginal("name"));
|
|
\Log::debug('Updating New Column Name: '.$custom_field->convertUnicodeDbSlug());
|
|
|
|
if ($custom_field->isDirty("name")) {
|
|
if (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug())) {
|
|
\Log::debug('Column already exists. Nothing to update.');
|
|
return true;
|
|
}
|
|
|
|
\Log::debug('Updating column name to.'.$custom_field->convertUnicodeDbSlug());
|
|
|
|
return Schema::table(CustomField::$table_name, function ($table) use ($custom_field) {
|
|
$table->renameColumn($custom_field->convertUnicodeDbSlug($custom_field->getOriginal("name")), $custom_field->convertUnicodeDbSlug());
|
|
});
|
|
}
|
|
return true;
|
|
});
|
|
|
|
self::deleting(function ($custom_field) {
|
|
return Schema::table(CustomField::$table_name, function ($table) use ($custom_field) {
|
|
$table->dropColumn($custom_field->convertUnicodeDbSlug());
|
|
});
|
|
});
|
|
}
|
|
|
|
public function fieldset()
|
|
{
|
|
return $this->belongsToMany('\App\Models\CustomFieldset'); //?!?!?!?!?!?
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo('\App\Models\User');
|
|
}
|
|
|
|
|
|
public function check_format($value)
|
|
{
|
|
return preg_match('/^'.$this->attributes['format'].'$/', $value)===1;
|
|
}
|
|
|
|
public function db_column_name()
|
|
{
|
|
return $this->db_column;
|
|
// return self::convertUnicodeDbSlug();
|
|
}
|
|
|
|
//mutators for 'format' attribute
|
|
public function getFormatAttribute($value)
|
|
{
|
|
foreach (self::$PredefinedFormats as $name => $pattern) {
|
|
if ($pattern===$value) {
|
|
return $name;
|
|
}
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
public function setFormatAttribute($value)
|
|
{
|
|
if (isset(self::$PredefinedFormats[$value])) {
|
|
$this->attributes['format']=self::$PredefinedFormats[$value];
|
|
} else {
|
|
$this->attributes['format']=$value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Format a value string as an array for select boxes and checkboxes.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v3.4]
|
|
* @return Array
|
|
*/
|
|
public function formatFieldValuesAsArray()
|
|
{
|
|
$arr = preg_split("/\\r\\n|\\r|\\n/", $this->field_values);
|
|
|
|
$result[''] = 'Select '.strtolower($this->format);
|
|
|
|
for ($x = 0; $x < count($arr); $x++) {
|
|
$arr_parts = explode('|', $arr[$x]);
|
|
if ($arr_parts[0]!='') {
|
|
if (key_exists('1', $arr_parts)) {
|
|
$result[$arr_parts[0]] = $arr_parts[1];
|
|
} else {
|
|
$result[$arr_parts[0]] = $arr_parts[0];
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function isFieldDecryptable($string)
|
|
{
|
|
if (($this->field_encrypted=='1') && ($string!='')) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
public function convertUnicodeDbSlug($original = null)
|
|
{
|
|
$name = $original ? $original : $this->name;
|
|
$id = $this->id ? $this->id : 'xx';
|
|
|
|
if (!function_exists('transliterator_transliterate')) {
|
|
$long_slug = '_snipeit_' . str_slug(\Patchwork\Utf8::utf8_encode(trim($name)), '_');
|
|
} else {
|
|
$long_slug = '_snipeit_' . Utf8Slugger::slugify($name, '_');
|
|
}
|
|
|
|
return substr($long_slug, 0, 50) . '_' . $id;
|
|
}
|
|
}
|