| Total Complexity | 208 |
| Total Lines | 1058 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Datastore often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Datastore, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Datastore{ |
||
| 19 | |||
| 20 | protected $__asset = false; |
||
| 21 | protected $__asset_properties = false; |
||
| 22 | protected $__value_equals = false; |
||
| 23 | protected $__status_equals = false; |
||
| 24 | protected $__start_date = null; |
||
| 25 | protected $__end_date = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * |
||
| 29 | * @var DatastoreModel |
||
| 30 | */ |
||
| 31 | protected $__model; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * |
||
| 35 | * @var array of datastore_model |
||
| 36 | */ |
||
| 37 | public $ownDatastore = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The current record id |
||
|
|
|||
| 41 | * @var record id |
||
| 42 | */ |
||
| 43 | public $id = 0; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * construct a new datastore instance |
||
| 47 | * if an id is passed populate the data |
||
| 48 | * @param int $id |
||
| 49 | */ |
||
| 50 | public function __construct($id = 0) |
||
| 51 | { |
||
| 52 | |||
| 53 | $this->id = $id; |
||
| 54 | if ($id) |
||
| 55 | { |
||
| 56 | $this->__model = DatastoreModel::findOrFail($id); |
||
| 57 | |||
| 58 | if(!$this->__model){ |
||
| 59 | //could not load <div class="">thr</div> |
||
| 60 | throw new DatastoreException("Record not found"); |
||
| 61 | } |
||
| 62 | |||
| 63 | foreach ($this->__model->toArray() as $k => $v) |
||
| 64 | { |
||
| 65 | if($k === 'options' || $k === 'meta'){ |
||
| 66 | $v = json_decode($v, true); |
||
| 67 | } |
||
| 68 | if(in_array($k, ['created_at','updated_at','date_start','date_end'])){ |
||
| 69 | $v = Carbon::parse($v); |
||
| 70 | } |
||
| 71 | $this->{$k} = $v; |
||
| 72 | } |
||
| 73 | |||
| 74 | |||
| 75 | $stores = $this->__model->properties; |
||
| 76 | if ($stores) |
||
| 77 | { |
||
| 78 | foreach ($stores as $ds) |
||
| 79 | { |
||
| 80 | $this->ownDatastore[] = self::getAssetById($ds->id); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | }else{ |
||
| 84 | $this->__model = new DatastoreModel(); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Gets an existing asset by ID |
||
| 90 | * |
||
| 91 | * @param [type] $id |
||
| 92 | * |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | public static function getAssetById($id){ |
||
| 96 | $datastore = new self($id); |
||
| 97 | $datastore->setAssetType($datastore->type); |
||
| 98 | return $datastore; |
||
| 99 | } |
||
| 100 | |||
| 101 | public static function find($id){ |
||
| 102 | if(!is_array($id)){ |
||
| 103 | if(is_object($id)){ |
||
| 104 | return self::getAssetById($id->id); |
||
| 105 | } |
||
| 106 | return self::getAssetById($id); |
||
| 107 | } |
||
| 108 | |||
| 109 | if(is_array($id)){ |
||
| 110 | $assets = []; |
||
| 111 | foreach($id as $i){ |
||
| 112 | $assets[] = self::getAssetById($i); |
||
| 113 | } |
||
| 114 | return $assets; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Gets a new asset instance |
||
| 120 | * @param string $type |
||
| 121 | * @param int $id |
||
| 122 | * @return \DF_Datastore |
||
| 123 | */ |
||
| 124 | public static function getAsset($type, $id = false) |
||
| 125 | { |
||
| 126 | $ds = new self($id); |
||
| 127 | $ds->setAssetType($type); |
||
| 128 | return $ds; |
||
| 129 | } |
||
| 130 | |||
| 131 | |||
| 132 | public function ownDatastore(){ |
||
| 133 | return ($this->ownDatastore)?$this->ownDatastore: array(); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Sets the type of asset & maps identifier keys |
||
| 138 | * @param string $type |
||
| 139 | * @return DF_Datastore |
||
| 140 | */ |
||
| 141 | public function setAssetType($type) |
||
| 142 | { |
||
| 143 | |||
| 144 | $this->__asset = Asset::factory($type); |
||
| 145 | |||
| 146 | foreach ($this->__asset as $k => $v) |
||
| 147 | { |
||
| 148 | if ($k != 'properties') |
||
| 149 | { |
||
| 150 | if ($k == 'value_equals') |
||
| 151 | { |
||
| 152 | $this->__value_equals = $v; |
||
| 153 | } |
||
| 154 | else if ($k == 'status_equals') |
||
| 155 | { |
||
| 156 | $this->__status_equals = $v; |
||
| 157 | } |
||
| 158 | else if ($k == 'start_date') |
||
| 159 | { |
||
| 160 | $this->__start_date = $v; |
||
| 161 | } |
||
| 162 | else if ($k == 'end_date') |
||
| 163 | { |
||
| 164 | $this->__end_date = $v; |
||
| 165 | } |
||
| 166 | else |
||
| 167 | { |
||
| 168 | $this->$k = (isset($this->$k)) ? $this->$k : $v; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | $this->type = $type; |
||
| 173 | |||
| 174 | $this->module = Helpers::getModule($type); |
||
| 175 | |||
| 176 | $this->setInternals(); |
||
| 177 | |||
| 178 | return $this; |
||
| 179 | } |
||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * Sets the internal mapping |
||
| 184 | * @todo - pehaps this should become protected ? |
||
| 185 | */ |
||
| 186 | public function setInternals() |
||
| 187 | { |
||
| 188 | |||
| 189 | foreach ($this->__asset->properties as $p => $v) |
||
| 190 | { |
||
| 191 | |||
| 192 | if (!isset($this->__asset_properties[$p])) |
||
| 193 | { |
||
| 194 | $this->__asset_properties[$p]['temp'] = self::getAsset($this->__asset->properties[$p]['type']); |
||
| 195 | $this->__asset_properties[$p]['temp']->name = (isset($this->__asset->properties[$p]['name'])) ? $this->__asset->properties[$p]['name'] : null; |
||
| 196 | $this->__asset_properties[$p]['temp']->key = $p; |
||
| 197 | $this->__asset_properties[$p]['temp']->private = (isset($this->__asset->properties[$p]['private'])) ? $this->__asset->properties[$p]['private'] : false; // accessibility |
||
| 198 | $this->__asset_properties[$p]['temp']->value = (isset($this->__asset->properties[$p]['value'])) ? $this->__asset->properties[$p]['value'] : null; // the actual value of the property |
||
| 199 | $this->__asset_properties[$p]['temp']->options = (isset($this->__asset->properties[$p]['options'])) ? $this->__asset->properties[$p]['options'] : null; // additional options specific to the property |
||
| 200 | $this->__asset_properties[$p]['temp']->callback = (isset($this->__asset->properties[$p]['callback'])) ? $this->__asset->properties[$p]['callback'] : null; // additional options specific to the property |
||
| 201 | $this->__asset_properties[$p]['temp']->meta = (isset($this->__asset->properties[$p]['meta'])) ? $this->__asset->properties[$p]['meta'] : null; // additional options specific to the property |
||
| 202 | $this->__asset_properties[$p]['temp']->help = (isset($this->__asset->properties[$p]['help'])) ? $this->__asset->properties[$p]['help'] : null; // additional options specific to the property |
||
| 203 | $this->__asset_properties[$p]['temp']->required = (isset($this->__asset->properties[$p]['required'])) ? $this->__asset->properties[$p]['required'] : true; // is the property required |
||
| 204 | $this->__asset_properties[$p]['temp']->limit = (isset($this->__asset->properties[$p]['limit'])) ? $this->__asset->properties[$p]['limit'] : null; // additional options specific to the property |
||
| 205 | $this->__asset_properties[$p]['temp']->warning = (isset($this->__asset->properties[$p]['warning'])) ? $this->__asset->properties[$p]['warning'] : null; // additional options specific to the property |
||
| 206 | $this->__asset_properties[$p]['temp']->max_instances = (isset($this->__asset->properties[$p]['max_instances'])) ? $this->__asset->properties[$p]['max_instances'] : 0; // there can be only one |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | // fitst check whether we have any datastore set |
||
| 211 | if ($this->ownDatastore) |
||
| 212 | { |
||
| 213 | |||
| 214 | // if it has a datastore, its easy - map the buggers |
||
| 215 | $this->key = self::getKey($this->type); |
||
| 216 | |||
| 217 | foreach ($this->ownDatastore as $idx => $each) |
||
| 218 | { |
||
| 219 | // create a map to our property bean |
||
| 220 | if ($each->namespace == 'property') |
||
| 221 | { |
||
| 222 | $this->__asset_properties[$each->key] = array('temp' => $each); |
||
| 223 | @$this->ownDatastore[$idx]->required = (isset($this->__asset->properties[$each->key]['required'])) ? $this->__asset->properties[$each->key]['required'] : true; |
||
| 224 | @$this->ownDatastore[$idx]->limit = (isset($this->__asset->properties[$each->key]['limit'])) ? $this->__asset->properties[$each->key]['limit'] : null; |
||
| 225 | @$this->ownDatastore[$idx]->warning = (isset($this->__asset->properties[$each->key]['warning'])) ? $this->__asset->properties[$each->key]['warning'] : null; |
||
| 226 | @$this->ownDatastore[$idx]->help = (isset($this->__asset->properties[$each->key]['help'])) ? $this->__asset->properties[$each->key]['help'] : null; |
||
| 227 | @$this->ownDatastore[$idx]->max_instances = (isset($this->__asset->properties[$each->key]['max_instances'])) ? $this->__asset->properties[$each->key]['max_instances'] : 0; |
||
| 228 | @$this->ownDatastore[$idx]->options = (isset($this->__asset->properties[$each->key]['options'])) ? $this->__asset->properties[$each->key]['options'] : null; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | //echo '<pre>$this->ownDatastore: '; print_r($this->ownDatastore); echo '</pre>'; die(); |
||
| 232 | } |
||
| 233 | else |
||
| 234 | { |
||
| 235 | // nope - net to create some datastore beans to represent our properties |
||
| 236 | foreach ($this->__asset->properties as $prop => $data) |
||
| 237 | { |
||
| 238 | |||
| 239 | // we need to create a temporary new bean to store new data |
||
| 240 | $this->__asset_properties[$prop]['temp'] = self::getAsset($this->__asset->properties[$prop]['type']); |
||
| 241 | $this->__asset_properties[$prop]['temp']->name = (isset($this->__asset->properties[$prop]['name'])) ? $this->__asset->properties[$prop]['name'] : null; |
||
| 242 | $this->__asset_properties[$prop]['temp']->key = $prop; |
||
| 243 | $this->__asset_properties[$prop]['temp']->private = (isset($this->__asset->properties[$prop]['private'])) ? $this->__asset->properties[$prop]['private'] : false; // accessibility |
||
| 244 | $this->__asset_properties[$prop]['temp']->value = (isset($this->__asset->properties[$prop]['value'])) ? $this->__asset->properties[$prop]['value'] : null; // the actual value of the property |
||
| 245 | $this->__asset_properties[$prop]['temp']->options = (isset($this->__asset->properties[$prop]['options'])) ? $this->__asset->properties[$prop]['options'] : null; // additional options specific to the property |
||
| 246 | $this->__asset_properties[$prop]['temp']->callback = (isset($this->__asset->properties[$prop]['callback'])) ? $this->__asset->properties[$prop]['callback'] : null; // additional options specific to the property |
||
| 247 | $this->__asset_properties[$prop]['temp']->meta = (isset($this->__asset->properties[$prop]['meta'])) ? $this->__asset->properties[$prop]['meta'] : null; // additional options specific to the property |
||
| 248 | $this->__asset_properties[$prop]['temp']->help = (isset($this->__asset->properties[$prop]['help'])) ? $this->__asset->properties[$prop]['help'] : null; // additional options specific to the property |
||
| 249 | $this->__asset_properties[$prop]['temp']->required = (isset($this->__asset->properties[$prop]['required'])) ? $this->__asset->properties[$prop]['required'] : true; // is the property required |
||
| 250 | $this->__asset_properties[$prop]['temp']->limit = (isset($this->__asset->properties[$prop]['limit'])) ? $this->__asset->properties[$prop]['limit'] : null; // additional options specific to the property |
||
| 251 | $this->__asset_properties[$prop]['temp']->warning = (isset($this->__asset->properties[$prop]['warning'])) ? $this->__asset->properties[$prop]['warning'] : null; // additional options specific to the property |
||
| 252 | $this->__asset_properties[$prop]['temp']->max_instances = (isset($this->__asset->properties[$prop]['max_instances'])) ? $this->__asset->properties[$prop]['max_instances'] : null; // there can be only one |
||
| 253 | |||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * Gets the Asset Key |
||
| 262 | * @param string $string |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public static function getKey($string) |
||
| 266 | { |
||
| 267 | $new = Helpers::splitByCaps($string); |
||
| 268 | $explode = explode(' ', $new); |
||
| 269 | |||
| 270 | if (isset($explode[0]) && $explode[0] == 'Ams') |
||
| 271 | { |
||
| 272 | // drop the ams portion |
||
| 273 | array_shift($explode); |
||
| 274 | } |
||
| 275 | // drop the last part too |
||
| 276 | $last = array_pop($explode); |
||
| 277 | //lowercase the first part |
||
| 278 | if (isset($explode[0])) |
||
| 279 | { |
||
| 280 | $explode[0] = strtolower($explode[0]); |
||
| 281 | } |
||
| 282 | |||
| 283 | return (count($explode) > 1) ? implode('', $explode) : $explode[0]; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Setter / getter of asset property value, |
||
| 288 | * pass 1 param to get the value, |
||
| 289 | * pass 2 params to set the value |
||
| 290 | * @return mixed |
||
| 291 | * @throws Exception |
||
| 292 | */ |
||
| 293 | public function prop() |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * shortcut to write to the asset's value property |
||
| 385 | * @return mixed |
||
| 386 | */ |
||
| 387 | public function val() |
||
| 388 | { |
||
| 389 | $num_args = func_num_args(); |
||
| 390 | |||
| 391 | switch ($num_args) |
||
| 392 | { |
||
| 393 | case 0: |
||
| 394 | // return all props as beans |
||
| 395 | return $this->value; |
||
| 396 | break; |
||
| 397 | |||
| 398 | default: |
||
| 399 | $val = func_get_arg(0); |
||
| 400 | $this->value = $val; |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | public function __toString() |
||
| 405 | { |
||
| 406 | |||
| 407 | return (string) $this->val(); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Stores our asset |
||
| 412 | * @param DF_Datastore $asset |
||
| 413 | * @param Int $pid Parent Datastore id |
||
| 414 | */ |
||
| 415 | protected function __store($asset, $pid = null) |
||
| 416 | { |
||
| 417 | |||
| 418 | $class = new ReflectionClass($asset->__asset); |
||
| 419 | $props = $class->getProperties(ReflectionProperty::IS_PUBLIC); |
||
| 420 | $record = $asset->__model; |
||
| 421 | $record->datastore_id = $pid; |
||
| 422 | |||
| 423 | foreach ($props as $prop) |
||
| 424 | { |
||
| 425 | if (!in_array($prop->name, array('status_equals', 'properties','help','value_equals'))) |
||
| 426 | { |
||
| 427 | $record->{$prop->name} = isset($asset->{$prop->name}) ? $asset->{$prop->name} : NULL; |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | if ($asset->__status_equals) |
||
| 432 | { |
||
| 433 | $record->status = isset($record->{$asset->__status_equals})? $record->{$asset->__status_equals} : $this->prop($this->__status_equals); |
||
| 434 | } |
||
| 435 | if ($asset->__start_date) |
||
| 436 | { |
||
| 437 | $record->start_date = isset($record->{$asset->__start_date})? $record->{$asset->__start_date} : $this->prop($this->__start_date); |
||
| 438 | } |
||
| 439 | if ($asset->__end_date) |
||
| 440 | { |
||
| 441 | $record->end_date = isset($record->{$asset->__end_date})? $record->{$asset->__end_date} : $this->prop($this->__end_date); |
||
| 442 | } |
||
| 443 | |||
| 444 | $record->options = json_encode($record->options); |
||
| 445 | $record->meta = json_encode($record->meta); |
||
| 446 | |||
| 447 | $record->save(); |
||
| 448 | |||
| 449 | if ($asset->__asset_properties) |
||
| 450 | { |
||
| 451 | foreach ($asset->__asset_properties as $prop) |
||
| 452 | { |
||
| 453 | $this->__store($prop['temp'], $record->id); |
||
| 454 | } |
||
| 455 | } |
||
| 456 | return $record->id; |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Stores the asset |
||
| 461 | * @return int Datastore id |
||
| 462 | */ |
||
| 463 | public function store($parent = null) |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Echos out the asset markup |
||
| 471 | * @param string $prop |
||
| 472 | * @param string $template |
||
| 473 | */ |
||
| 474 | public function render($prop, $template = false) |
||
| 475 | { |
||
| 476 | if (isset($this->__asset_properties[$prop]['temp'])) |
||
| 477 | { |
||
| 478 | return $this->__asset_properties[$prop]['temp']->getMarkup($template); |
||
| 479 | } |
||
| 480 | else |
||
| 481 | { |
||
| 482 | if (isset($this->__asset_properties[$prop])) |
||
| 483 | { |
||
| 484 | return $this->ownDatastore[$this->__asset_properties[$prop]]->getMarkup($template); |
||
| 485 | } |
||
| 486 | } |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Gets the current Datastore markup |
||
| 491 | * @param string $template |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | public function getMarkup($template = false) |
||
| 495 | { |
||
| 496 | |||
| 497 | $vars = $this->export(); |
||
| 498 | $vars['_unique_id'] = uniqid(); |
||
| 499 | |||
| 500 | // we also want to know what asset this property belongs to |
||
| 501 | $output = Helpers::callStatic($this->type, 'render', array($vars, $template)); |
||
| 502 | if (!$output) |
||
| 503 | { |
||
| 504 | return null; |
||
| 505 | } |
||
| 506 | return $output; |
||
| 507 | } |
||
| 508 | |||
| 509 | |||
| 510 | /** |
||
| 511 | * exports current Datastore to an array |
||
| 512 | * @return array |
||
| 513 | */ |
||
| 514 | public function export() |
||
| 515 | { |
||
| 516 | |||
| 517 | $class = new ReflectionClass($this->__asset); |
||
| 518 | $props = $class->getProperties(ReflectionProperty::IS_PUBLIC); |
||
| 519 | |||
| 520 | |||
| 521 | |||
| 522 | foreach ($props as $prop) |
||
| 523 | { |
||
| 524 | |||
| 525 | $data[$prop->name] = isset($this->{$prop->name}) ? $this->{$prop->name} : NULL; |
||
| 526 | } |
||
| 527 | |||
| 528 | if ($this->__status_equals) |
||
| 529 | { |
||
| 530 | |||
| 531 | $data['status'] = isset($data[$this->__status_equals])? $data[$this->__status_equals] : $this->prop($this->__status_equals); |
||
| 532 | } |
||
| 533 | if ($this->__start_date) |
||
| 534 | { |
||
| 535 | $data['start_date'] = $data[$this->__start_date]; |
||
| 536 | } |
||
| 537 | if ($this->__end_date) |
||
| 538 | { |
||
| 539 | $data['end_date'] = $data[$this->__end_date]; |
||
| 540 | } |
||
| 541 | |||
| 542 | $data['id'] = $this->id; |
||
| 543 | |||
| 544 | return $data; |
||
| 545 | } |
||
| 546 | |||
| 547 | |||
| 548 | /** |
||
| 549 | * Gets form element for a propery |
||
| 550 | * @param string $prop |
||
| 551 | * @param string $template |
||
| 552 | */ |
||
| 553 | public function form($prop, $template = false) |
||
| 554 | { |
||
| 555 | if (isset($this->__asset_properties[$prop]['temp'])) |
||
| 556 | { |
||
| 557 | echo $this->__asset_properties[$prop]['temp']->getForm($template, $prop); |
||
| 558 | } |
||
| 559 | else |
||
| 560 | { |
||
| 561 | if (isset($this->__asset_properties[$prop])) |
||
| 562 | { |
||
| 563 | echo $this->ownDatastore[$this->__asset_properties[$prop]]->getForm($template, $prop); |
||
| 564 | } |
||
| 565 | } |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Gets property form markup |
||
| 570 | * @param string $prop |
||
| 571 | * @param string $template |
||
| 572 | * @param array $_prop |
||
| 573 | * @return type |
||
| 574 | */ |
||
| 575 | public function returnForm($prop, $template = false, $_prop) |
||
| 576 | { |
||
| 577 | |||
| 578 | if (isset($this->__asset_properties[$prop]['temp'])) |
||
| 579 | { |
||
| 580 | return $this->__asset_properties[$prop]['temp']->getForm($template, $_prop); |
||
| 581 | } |
||
| 582 | else |
||
| 583 | { |
||
| 584 | if (isset($this->__asset_properties[$prop])) |
||
| 585 | { |
||
| 586 | |||
| 587 | return $this->ownDatastore[$this->__asset_properties[$prop]]->getForm($template, $_prop); |
||
| 588 | } |
||
| 589 | } |
||
| 590 | } |
||
| 591 | |||
| 592 | public function getFieldValues(){ |
||
| 593 | $data = []; |
||
| 594 | foreach(array_keys($this->__asset->properties) as $key){ |
||
| 595 | $data[$key] = $this->prop($key); |
||
| 596 | } |
||
| 597 | return $data; |
||
| 598 | } |
||
| 599 | |||
| 600 | |||
| 601 | /** |
||
| 602 | * Gets the form |
||
| 603 | * @param string $template |
||
| 604 | * @param array $_prop |
||
| 605 | * @return string |
||
| 606 | */ |
||
| 607 | public function getForm($template = false, $_prop = array()) |
||
| 608 | { |
||
| 609 | |||
| 610 | $output = null; |
||
| 611 | if ($this->namespace == 'asset') |
||
| 612 | { |
||
| 613 | foreach ($this->__asset_properties as $prop => $val) |
||
| 614 | { |
||
| 615 | $output .= $this->returnForm($prop, false, $_prop); |
||
| 616 | } |
||
| 617 | } |
||
| 618 | else |
||
| 619 | { |
||
| 620 | return $this->propGetForm($template, $_prop); |
||
| 621 | } |
||
| 622 | |||
| 623 | if (!$output) |
||
| 624 | { |
||
| 625 | return null; |
||
| 626 | } |
||
| 627 | return $output; |
||
| 628 | } |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Gets property form |
||
| 632 | * @param string $template |
||
| 633 | * @param array $_prop |
||
| 634 | * @return string |
||
| 635 | */ |
||
| 636 | public function propGetForm($template = false, $_prop = array()) |
||
| 637 | { |
||
| 638 | $vars = $this->export(); |
||
| 639 | |||
| 640 | if (isset($_prop['unique_id'])) |
||
| 641 | { |
||
| 642 | $vars['_unique_id'] = $_prop['unique_id']; |
||
| 643 | } |
||
| 644 | else |
||
| 645 | { |
||
| 646 | $vars['_unique_id'] = md5(uniqid(rand(), true)); //UUID ?? |
||
| 647 | } |
||
| 648 | |||
| 649 | // we also want to know what asset this property belongs to |
||
| 650 | $output = Helpers::callStatic($this->type, 'form', array($vars, $template)); |
||
| 651 | if (!$output) |
||
| 652 | { |
||
| 653 | return null; |
||
| 654 | } |
||
| 655 | return $output; |
||
| 656 | } |
||
| 657 | |||
| 658 | public function propGetValue($prop){ |
||
| 659 | dd($prop); |
||
| 660 | } |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Injects a form into an asset |
||
| 664 | * Children asset instances |
||
| 665 | * @param string $uid |
||
| 666 | * @return string |
||
| 667 | */ |
||
| 668 | public function injectForm($uid) |
||
| 669 | { |
||
| 670 | |||
| 671 | $output = null; |
||
| 672 | if ($uid) |
||
| 673 | { |
||
| 674 | $unique_id = md5($uid); |
||
| 675 | } |
||
| 676 | else |
||
| 677 | { |
||
| 678 | $unique_id = md5(uniqid(rand(), true)); |
||
| 679 | } |
||
| 680 | |||
| 681 | |||
| 682 | foreach ($this->__asset_properties as $prop => $each) |
||
| 683 | { |
||
| 684 | if (isset($this->__asset_properties[$prop]['temp'])) |
||
| 685 | { |
||
| 686 | $xvars = $this->__asset_properties[$prop]['temp']->export(); |
||
| 687 | $xvars['unique_id'] = $unique_id; |
||
| 688 | |||
| 689 | $output .= Helpers::callStatic($this->__asset_properties[$prop]['temp']->type, 'form', array($xvars, $this->type)); |
||
| 690 | } |
||
| 691 | else |
||
| 692 | { |
||
| 693 | if (isset($this->__asset_properties[$prop])) |
||
| 694 | { |
||
| 695 | $xvars = $this->ownDatastore[$this->__asset_properties[$prop]]->export(); |
||
| 696 | $xvars['unique_id'] = $unique_id; |
||
| 697 | |||
| 698 | $output .= Helpers::callStatic($this->ownDatastore[$this->__asset_properties[$prop]]->type, 'form', array($xvars, $this->type)); |
||
| 699 | } |
||
| 700 | } |
||
| 701 | } |
||
| 702 | |||
| 703 | // we also want to know what asset this property belongs to |
||
| 704 | $output .= '<input type="hidden" class="asset-injector-input" data-type="' . $this->type . '" id="' . $unique_id . '" name="assetInjectionform[' . $this->type . '][' . $unique_id . '][id]" value="' . $this->id . '" />'; |
||
| 705 | if (!$output) |
||
| 706 | { |
||
| 707 | return null; |
||
| 708 | } |
||
| 709 | return $output; |
||
| 710 | } |
||
| 711 | |||
| 712 | public function hasMetadataForm(){ |
||
| 713 | if ($this->namespace == 'asset' && !$this->is_child){ |
||
| 714 | return ($this->meta_description !== 'off' || $this->meta_keywords !== 'off'); |
||
| 715 | } |
||
| 716 | return false; |
||
| 717 | } |
||
| 718 | |||
| 719 | /** |
||
| 720 | * gets the form necessary for building the meta data |
||
| 721 | * @return string |
||
| 722 | */ |
||
| 723 | public function getMetadataForm() |
||
| 724 | { |
||
| 725 | $output = null; |
||
| 726 | // must be a parent asset |
||
| 727 | if ($this->namespace == 'asset' && !$this->is_child) |
||
| 728 | { |
||
| 729 | if ($this->meta_description !== 'off') |
||
| 730 | { |
||
| 731 | $data = array( |
||
| 732 | 'name' => 'Meta-Description', |
||
| 733 | 'key' => 'meta_description', |
||
| 734 | 'help' => 'You can associate a Meta-Description with this content. No more than 155 characters is considered optimal', |
||
| 735 | 'value' => $this->meta_description |
||
| 736 | ); |
||
| 737 | |||
| 738 | $output .= Helpers::callStatic(ASSET::METATEXT, 'form', array($data, 'metatext')); |
||
| 739 | } |
||
| 740 | |||
| 741 | if ($this->meta_keywords !== 'off' /* && CONFIG->get('settings.allow_meta_keywords' ) */) |
||
| 742 | { |
||
| 743 | $data = array( |
||
| 744 | 'name' => 'Meta-Keywords', |
||
| 745 | 'key' => 'meta_keywords', |
||
| 746 | 'help' => 'You can associate Meta-Keywords with this content, with each keyword or key-phrase being seperated by a comma. <br/><b>Note :</b> There is evidence that suggests Keywords can hurt your SEO Ranking', |
||
| 747 | 'value' => $this->meta_keywords |
||
| 748 | ); |
||
| 749 | |||
| 750 | $output .= Helpers::callStatic(ASSET::METATEXT, 'form', array($data, 'metatext')); |
||
| 751 | } |
||
| 752 | } |
||
| 753 | if (!$output) |
||
| 754 | { |
||
| 755 | return null; |
||
| 756 | } |
||
| 757 | return $output; |
||
| 758 | } |
||
| 759 | |||
| 760 | public function hasDeveloperForm(){ |
||
| 761 | if ($this->namespace == 'asset' && !$this->is_child){ |
||
| 762 | return ($this->page_js !== 'off' || $this->page_css !== 'off'); |
||
| 763 | } |
||
| 764 | return false; |
||
| 765 | } |
||
| 766 | |||
| 767 | /** |
||
| 768 | * gets the form necessary for building the styling and behaviour data |
||
| 769 | * @return string |
||
| 770 | */ |
||
| 771 | public function getDeveloperForm() |
||
| 772 | { |
||
| 773 | $output = null; |
||
| 774 | // must be a parent asset |
||
| 775 | if ($this->namespace == 'asset' && !$this->is_child) |
||
| 776 | { |
||
| 777 | if ($this->page_css !== 'off') |
||
| 778 | { |
||
| 779 | $data = array( |
||
| 780 | 'name' => 'Custom CSS', |
||
| 781 | 'key' => 'page_css', |
||
| 782 | 'help' => 'Custom css rules for this page display', // 'You can associate a Meta-Description with this content. No more than 155 characters is considered optimal', |
||
| 783 | 'value' => $this->page_css |
||
| 784 | ); |
||
| 785 | |||
| 786 | $output .= Helpers::callStatic(ASSET::METATEXT, 'form', array($data, 'metatext')); |
||
| 787 | } |
||
| 788 | |||
| 789 | if ($this->page_js !== 'off' ) |
||
| 790 | { |
||
| 791 | $data = array( |
||
| 792 | 'name' => 'Custom JS', |
||
| 793 | 'key' => 'page_js', |
||
| 794 | 'help' => 'custom javascript for this page display', // 'You can associate Meta-Keywords with this content, with each keyword or key-phrase being seperated by a comma. <br/><b>Note :</b> There is evidence that suggests Keywords can hurt your SEO Ranking', |
||
| 795 | 'value' => $this->page_js |
||
| 796 | ); |
||
| 797 | |||
| 798 | $output .= Helpers::callStatic(ASSET::METATEXT, 'form', array($data, 'metatext')); |
||
| 799 | } |
||
| 800 | } |
||
| 801 | if (!$output) |
||
| 802 | { |
||
| 803 | return null; |
||
| 804 | } |
||
| 805 | return $output; |
||
| 806 | } |
||
| 807 | |||
| 808 | |||
| 809 | /** |
||
| 810 | * Validates the current form |
||
| 811 | * @return boolean if the form is valid or not |
||
| 812 | */ |
||
| 813 | public function validate($request = null) |
||
| 814 | { |
||
| 815 | |||
| 816 | $config = array(); |
||
| 817 | $messages = array(); |
||
| 818 | foreach ($this->__asset->properties as $key => $property) |
||
| 819 | { |
||
| 820 | if($property['type'] !== \Phpsa\Datastore\Ams\AutoCallBackAdderAsset::class && (empty($property['required']) || $property['required'] === true) ) |
||
| 821 | { |
||
| 822 | $rules = empty($property['validation_rules']) ? 'required' : $property['validation_rules']; |
||
| 823 | if(!empty($property['validation_messages'])){ |
||
| 824 | if(is_string($property['validation_messages'])){ |
||
| 825 | $messages[$key . '.required'] = $property['validation_messages']; |
||
| 826 | } |
||
| 827 | } |
||
| 828 | $config[$key] = $rules; |
||
| 829 | } |
||
| 830 | |||
| 831 | } |
||
| 832 | |||
| 833 | if ($config) |
||
| 834 | { |
||
| 835 | $validator = Validator::make($request, $config, $messages); |
||
| 836 | |||
| 837 | if($validator->fails()){ |
||
| 838 | throw new ValidationException($validator); |
||
| 839 | } |
||
| 840 | } |
||
| 841 | return true; |
||
| 842 | } |
||
| 843 | |||
| 844 | /** |
||
| 845 | * gets current datastore id |
||
| 846 | * @return int |
||
| 847 | */ |
||
| 848 | public function getId() |
||
| 849 | { |
||
| 850 | return $this->id; |
||
| 851 | } |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Gets the current datastore children |
||
| 855 | * @return array |
||
| 856 | */ |
||
| 857 | public function children() |
||
| 858 | { |
||
| 859 | $return = false; |
||
| 860 | |||
| 861 | if ($this->ownDatastore) |
||
| 862 | { |
||
| 863 | foreach ($this->ownDatastore as $child => $child_bean) |
||
| 864 | { |
||
| 865 | if ($child_bean->namespace == 'asset') |
||
| 866 | { |
||
| 867 | $return[] = $child_bean; |
||
| 868 | } |
||
| 869 | } |
||
| 870 | } |
||
| 871 | return $return; |
||
| 872 | } |
||
| 873 | |||
| 874 | /** |
||
| 875 | * does this datastore have this property? |
||
| 876 | * @param string $prop |
||
| 877 | * @return boolean |
||
| 878 | */ |
||
| 879 | public function propExists($prop) |
||
| 880 | { |
||
| 881 | if (isset($this->__asset_properties[$prop])) |
||
| 882 | { |
||
| 883 | return true; |
||
| 884 | } |
||
| 885 | return false; |
||
| 886 | } |
||
| 887 | |||
| 888 | |||
| 889 | /** |
||
| 890 | * Populates the current Datastore with data passedin array |
||
| 891 | * @param array $post_data |
||
| 892 | * @return $this |
||
| 893 | */ |
||
| 894 | public function populateAsset($post_data = array()) |
||
| 895 | { |
||
| 896 | |||
| 897 | foreach ($post_data as $k => $v) |
||
| 898 | { |
||
| 899 | switch ($k) |
||
| 900 | { |
||
| 901 | case 'meta_description': |
||
| 902 | case 'meta_keywords': |
||
| 903 | $this->$k = strip_tags($v); |
||
| 904 | break; |
||
| 905 | |||
| 906 | case 'page_css': |
||
| 907 | case 'page_js': |
||
| 908 | $this->$k = $v; |
||
| 909 | break; |
||
| 910 | |||
| 911 | case '_meta_': |
||
| 912 | foreach ($v as $pr => $pv) |
||
| 913 | { |
||
| 914 | if ($this->propExists($pr)) |
||
| 915 | { |
||
| 916 | if (isset($this->__asset_properties[$pr])) |
||
| 917 | { |
||
| 918 | // check whether this is new or existibng |
||
| 919 | if (isset($this->__asset_properties[$pr]['temp'])) |
||
| 920 | { |
||
| 921 | $this->__asset_properties[$pr]['temp']->meta = $pv; |
||
| 922 | } |
||
| 923 | else |
||
| 924 | { |
||
| 925 | $this->ownDatastore[$this->__asset_properties[$pr]]->meta = $pv; |
||
| 926 | } |
||
| 927 | } |
||
| 928 | } |
||
| 929 | } |
||
| 930 | |||
| 931 | default: |
||
| 932 | // if the property exists, populate the props |
||
| 933 | if ($this->propExists($k)) |
||
| 934 | { |
||
| 935 | $this->prop($k, $v); |
||
| 936 | } |
||
| 937 | } |
||
| 938 | } |
||
| 939 | |||
| 940 | return $this; |
||
| 941 | } |
||
| 942 | |||
| 943 | /** |
||
| 944 | * gets the avaialble status options |
||
| 945 | * |
||
| 946 | * @return void |
||
| 947 | */ |
||
| 948 | public function statusOptions(){ |
||
| 949 | return ($this->__status_equals) ? $this->__asset_properties[$this->__status_equals]['temp']->options : false; |
||
| 950 | } |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Gets the readable version of the status |
||
| 954 | * |
||
| 955 | * @return void |
||
| 956 | */ |
||
| 957 | public function getStatusValue(){ |
||
| 958 | $options = $this->statusOptions(); |
||
| 959 | return ($options && Helpers::isAssocArray($options) && isset($options[$this->status])) ? $options[$this->status] : $this->status; |
||
| 960 | } |
||
| 961 | |||
| 962 | /** |
||
| 963 | * Get / set the current status value |
||
| 964 | * |
||
| 965 | * @return void |
||
| 966 | */ |
||
| 967 | public function status() |
||
| 968 | { |
||
| 969 | $num_args = func_num_args(); |
||
| 970 | |||
| 971 | switch ($num_args) |
||
| 972 | { |
||
| 973 | case 0: |
||
| 974 | // return all props as beans |
||
| 975 | return $this->status(); |
||
| 976 | break; |
||
| 977 | |||
| 978 | default: |
||
| 979 | $val = func_get_arg(0); |
||
| 980 | $this->status = $val; |
||
| 981 | } |
||
| 982 | } |
||
| 983 | |||
| 984 | /** |
||
| 985 | * Checks to see if the current asset is actaully active |
||
| 986 | * |
||
| 987 | * @return bool |
||
| 988 | */ |
||
| 989 | public function statusIsActive(){ |
||
| 1002 | } |
||
| 1003 | |||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * gets the url for the image. |
||
| 1007 | * @deprecated ??? |
||
| 1008 | * @todo - perhaps can be used based on record or moved better place ? |
||
| 1009 | * @param [type] $image_id |
||
| 1010 | * |
||
| 1011 | * @return void |
||
| 1012 | */ |
||
| 1013 | public static function image_url($image_id) |
||
| 1014 | { |
||
| 1015 | return Storage::url('file.jpg'); |
||
| 1016 | |||
| 1017 | // get_instance()->load->model('database/files_model'); |
||
| 1018 | |||
| 1019 | // $image = get_instance()->files_model->get_where(array('id' => $image_id), NULL, 1); |
||
| 1020 | |||
| 1021 | // return ($image) ? $image->url() : 'assets/modules/blog/images/feature-placeholder.png'; |
||
| 1022 | } |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Gets an asset by type and value |
||
| 1026 | * |
||
| 1027 | * @param [type] $type |
||
| 1028 | * @param [type] $value |
||
| 1029 | * @deprecated ??? |
||
| 1030 | * @return void |
||
| 1031 | */ |
||
| 1032 | public static function getAssetByTypeValue($type,$value) |
||
| 1033 | { |
||
| 1034 | $asset = DatastoreModel::where('type' , $type)->where('value', $value)->where('namespace', 'asset')->orderBy('modified', 'desc')->first(); |
||
| 1035 | if($asset){ |
||
| 1036 | return self::getAsset($asset->type, $asset->id); |
||
| 1037 | } |
||
| 1038 | return false; |
||
| 1039 | } |
||
| 1040 | |||
| 1041 | /** |
||
| 1042 | * Gets the url path for the current type |
||
| 1043 | * |
||
| 1044 | * @return void |
||
| 1045 | */ |
||
| 1046 | public function urlPath(){ |
||
| 1048 | } |
||
| 1049 | |||
| 1050 | |||
| 1051 | |||
| 1052 | /** |
||
| 1053 | * Gets our view name |
||
| 1054 | * |
||
| 1055 | * @param string $prefix |
||
| 1056 | * |
||
| 1057 | * @return void |
||
| 1058 | */ |
||
| 1059 | public function getViewName($prefix = 'frontend.ams'){ |
||
| 1060 | return $this->__asset::getAssetView($prefix); |
||
| 1061 | } |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * Magic method to get from teh model if we not included it already |
||
| 1065 | * |
||
| 1066 | * @param [type] $name |
||
| 1067 | * |
||
| 1068 | * @return void |
||
| 1069 | */ |
||
| 1070 | public function __get($name){ |
||
| 1072 | } |
||
| 1073 | |||
| 1074 | public function getTitleField(){ |
||
| 1075 | return $this->__value_equals; |
||
| 1076 | } |
||
| 1077 | |||
| 1078 | } |
||
| 1079 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths