2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2016-11-24 12:48:03 -08:00
|
|
|
use Schema;
|
2017-01-25 04:52:47 -08:00
|
|
|
use Watson\Validating\ValidatingTrait;
|
|
|
|
use App\Http\Traits\UniqueUndeletedTrait;
|
2017-01-25 18:38:20 -08:00
|
|
|
use ForceUTF8\Encoding;
|
|
|
|
use EasySlugger\Utf8Slugger;
|
2017-01-26 04:59:14 -08:00
|
|
|
use Patchwork\Utf8;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
class CustomField extends Model
|
|
|
|
{
|
2017-01-25 04:52:47 -08:00
|
|
|
use ValidatingTrait, UniqueUndeletedTrait;
|
2016-03-25 01:18:05 -07:00
|
|
|
public $guarded=["id"];
|
|
|
|
public static $PredefinedFormats=[
|
2016-08-23 15:51:14 -07:00
|
|
|
"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",
|
2016-03-25 01:18:05 -07:00
|
|
|
];
|
|
|
|
|
2017-01-25 04:34:11 -08:00
|
|
|
public $rules = [
|
2016-11-24 12:48:03 -08:00
|
|
|
"name" => "required|unique:custom_fields"
|
2016-03-25 01:18:05 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
public static $table_name="assets";
|
|
|
|
|
|
|
|
public static function name_to_db_name($name)
|
|
|
|
{
|
2017-03-14 08:39:03 -07:00
|
|
|
return "_snipeit_" . preg_replace("/[^a-zA-Z0-9]/", "_", strtolower($name));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function boot()
|
|
|
|
{
|
2017-03-14 08:39:03 -07:00
|
|
|
self::created(function ($custom_field) {
|
2017-01-25 18:38:20 -08:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2017-08-24 23:20:51 -07:00
|
|
|
// column exists - nothing to do here
|
2017-01-25 18:38:20 -08:00
|
|
|
if (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug())) {
|
2016-03-25 01:18:05 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-24 12:48:03 -08:00
|
|
|
Schema::table(CustomField::$table_name, function ($table) use ($custom_field) {
|
2017-01-25 18:38:20 -08:00
|
|
|
$table->text($custom_field->convertUnicodeDbSlug())->nullable();
|
2016-03-25 01:18:05 -07:00
|
|
|
});
|
|
|
|
|
2017-01-26 04:59:14 -08:00
|
|
|
$custom_field->db_column = $custom_field->convertUnicodeDbSlug();
|
|
|
|
$custom_field->save();
|
2016-03-25 01:18:05 -07:00
|
|
|
});
|
|
|
|
|
2017-01-25 18:38:20 -08:00
|
|
|
|
2017-03-14 08:39:03 -07:00
|
|
|
self::updating(function ($custom_field) {
|
2017-01-25 18:38:20 -08:00
|
|
|
|
2017-08-24 23:20:51 -07:00
|
|
|
// Column already exists. Nothing to update.
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($custom_field->isDirty("name")) {
|
2017-03-14 08:39:03 -07:00
|
|
|
if (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug())) {
|
2017-01-25 18:38:20 -08:00
|
|
|
return true;
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2017-01-25 18:38:20 -08:00
|
|
|
|
2017-10-11 14:18:08 -07:00
|
|
|
$platform = Schema::getConnection()->getDoctrineSchemaManager()->getDatabasePlatform();
|
|
|
|
$platform->registerDoctrineTypeMapping('enum', 'string');
|
|
|
|
|
2017-10-11 13:09:10 -07:00
|
|
|
Schema::table(CustomField::$table_name, function ($table) use ($custom_field) {
|
2017-01-25 18:38:20 -08:00
|
|
|
$table->renameColumn($custom_field->convertUnicodeDbSlug($custom_field->getOriginal("name")), $custom_field->convertUnicodeDbSlug());
|
2016-11-24 12:48:03 -08:00
|
|
|
});
|
2017-10-11 13:09:10 -07:00
|
|
|
|
|
|
|
|
|
|
|
$custom_field->db_column = $custom_field->convertUnicodeDbSlug();
|
|
|
|
$custom_field->save();
|
|
|
|
|
|
|
|
return true;
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2017-03-14 08:39:03 -07:00
|
|
|
self::deleting(function ($custom_field) {
|
2016-12-29 14:02:18 -08:00
|
|
|
return Schema::table(CustomField::$table_name, function ($table) use ($custom_field) {
|
2017-01-25 18:38:20 -08:00
|
|
|
$table->dropColumn($custom_field->convertUnicodeDbSlug());
|
2016-11-24 12:48:03 -08:00
|
|
|
});
|
2016-03-25 01:18:05 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fieldset()
|
|
|
|
{
|
2017-08-24 23:20:51 -07:00
|
|
|
return $this->belongsToMany('\App\Models\CustomFieldset');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2017-05-03 12:14:35 -07:00
|
|
|
return $this->db_column;
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2017-08-24 23:20:51 -07:00
|
|
|
// mutators for 'format' attribute
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getFormatAttribute($value)
|
|
|
|
{
|
|
|
|
foreach (self::$PredefinedFormats as $name => $pattern) {
|
|
|
|
if ($pattern===$value) {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2017-08-24 23:20:51 -07:00
|
|
|
/**
|
|
|
|
* Format a value string as an array for select boxes and checkboxes.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v3.4]
|
|
|
|
* @return Array
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function setFormatAttribute($value)
|
|
|
|
{
|
|
|
|
if (isset(self::$PredefinedFormats[$value])) {
|
|
|
|
$this->attributes['format']=self::$PredefinedFormats[$value];
|
|
|
|
} else {
|
|
|
|
$this->attributes['format']=$value;
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 18:35:01 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a value string as an array for select boxes and checkboxes.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v3.4]
|
|
|
|
* @return Array
|
|
|
|
*/
|
2016-12-29 14:02:18 -08:00
|
|
|
public function formatFieldValuesAsArray()
|
|
|
|
{
|
2016-08-25 18:35:01 -07:00
|
|
|
$arr = preg_split("/\\r\\n|\\r|\\n/", $this->field_values);
|
|
|
|
|
2016-08-25 21:04:10 -07:00
|
|
|
$result[''] = 'Select '.strtolower($this->format);
|
|
|
|
|
2016-08-25 18:35:01 -07:00
|
|
|
for ($x = 0; $x < count($arr); $x++) {
|
|
|
|
$arr_parts = explode('|', $arr[$x]);
|
2016-08-25 21:04:10 -07:00
|
|
|
if ($arr_parts[0]!='') {
|
2016-12-29 14:02:18 -08:00
|
|
|
if (key_exists('1', $arr_parts)) {
|
2016-08-25 21:04:10 -07:00
|
|
|
$result[$arr_parts[0]] = $arr_parts[1];
|
|
|
|
} else {
|
|
|
|
$result[$arr_parts[0]] = $arr_parts[0];
|
|
|
|
}
|
2016-08-25 18:35:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 21:04:10 -07:00
|
|
|
|
2016-08-25 18:35:01 -07:00
|
|
|
return $result;
|
|
|
|
}
|
2016-08-25 21:04:10 -07:00
|
|
|
|
2016-12-29 14:02:18 -08:00
|
|
|
public function isFieldDecryptable($string)
|
|
|
|
{
|
2016-08-25 21:04:10 -07:00
|
|
|
if (($this->field_encrypted=='1') && ($string!='')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-25 18:38:20 -08:00
|
|
|
|
|
|
|
|
|
|
|
public function convertUnicodeDbSlug($original = null)
|
|
|
|
{
|
|
|
|
$name = $original ? $original : $this->name;
|
2017-01-26 04:59:14 -08:00
|
|
|
$id = $this->id ? $this->id : 'xx';
|
|
|
|
|
|
|
|
if (!function_exists('transliterator_transliterate')) {
|
2017-03-14 08:39:03 -07:00
|
|
|
$long_slug = '_snipeit_' . str_slug(\Patchwork\Utf8::utf8_encode(trim($name)), '_');
|
2017-01-26 04:59:14 -08:00
|
|
|
} else {
|
2017-03-14 08:39:03 -07:00
|
|
|
$long_slug = '_snipeit_' . Utf8Slugger::slugify($name, '_');
|
2017-01-26 04:59:14 -08:00
|
|
|
}
|
|
|
|
|
2017-03-14 08:39:03 -07:00
|
|
|
return substr($long_slug, 0, 50) . '_' . $id;
|
2017-01-25 18:38:20 -08:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|