Exemplo n.º 1
0
 protected function createElementByType($type, $name, Field $field)
 {
     $auto = ['id', 'hidden', 'email', 'password', 'text', 'textarea', 'editor', 'integer', 'decimal'];
     if (in_array($type, $auto)) {
         return $this->{'add' . ucfirst($type)}($name);
     } elseif (in_array($type, ['file', 'pdf'])) {
         $dir = $field->getAbsoluteDir($field->getSetting('pckg.dynamic.field.dir'), $field->getSetting('pckg.dynamic.field.privateUpload'));
         $fullPath = $this->record->{$field->field} ? media($this->record->{$field->field}, null, true, $dir) : null;
         if ($field->getSetting('pckg.dynamic.field.uploadDisabled')) {
             $element = $this->addDiv();
             if ($field->getSetting('pckg.dynamic.field.generateFileUrl')) {
                 $element->addChild('<a class="btn btn-info btn-md" title="Generate ' . $type . '" href="' . $field->getGenerateFileUrlAttribute($this->record) . '"><i class="fa fa-refresh" aria-hidden="true"></i> Generate ' . $type . '</a>');
             }
             if ($this->record->{$field->field}) {
                 $element->addChild('&nbsp;&nbsp;<a class="btn btn-success btn-md" title="Download ' . $type . '" href="' . $fullPath . '"><i class="fa fa-download" aria-hidden="true"></i> Download ' . $this->record->{$field->field} . '</a>');
             }
         } else {
             $element = $this->addFile($name);
             $element->setPrefix('<i class="fa fa-file' . ($type == 'pdf' ? '-pdf' : '') . '-o" aria-hidden="true"></i>');
             if ($fullPath) {
                 $element->setAttribute('data-image', $fullPath);
             }
             $element->setAttribute('data-type', $type);
         }
         return $element;
     } elseif ($type == 'picture') {
         $element = $this->addPicture($name);
         $element->setPrefix('<i class="fa fa-picture-o" aria-hidden="true"></i>');
         $element->setAttribute('data-url', url('dynamic.records.field.upload', ['table' => $this->table, 'field' => $field, 'record' => $this->record]));
         $dir = $field->getAbsoluteDir($field->getSetting('pckg.dynamic.field.dir'));
         $element->setAttribute('data-image', img($this->record->{$field->field}, null, true, $dir));
         $element->setAttribute('data-type', 'picture');
         return $element;
     } elseif ($type == 'datetime') {
         $element = $this->addDatetime($name);
         $element->setPrefix('<i class="fa fa-calendar" aria-hidden="true"></i>');
         return $element;
     } elseif ($type == 'date') {
         $element = $this->addDate($name);
         $element->setPrefix('<i class="fa fa-calendar" aria-hidden="true"></i>');
         return $element;
     } elseif ($type == 'time') {
         $element = $this->addTime($name);
         $element->setPrefix('<i class="fa fa-clock-o" aria-hidden="true"></i>');
         return $element;
     } elseif (in_array($type, ['slug', 'order', 'hash'])) {
         return $this->addText($name);
     } elseif (in_array($type, ['json'])) {
         return $this->addTextarea($name);
     } elseif ($type == 'boolean') {
         return $this->addCheckbox($name);
     } elseif ($type == 'select') {
         if ($this->record && ($relation = $field->getRelationForSelect($this->record, $this->foreignRecord))) {
             $element = $this->addSelect($name);
             /**
              * @T00D00 - add setting for select placeholder for speciffic field
              */
             $element->addOption(null, ' -- select value -- ');
             foreach ($relation as $id => $value) {
                 $element->addOption($id, str_replace(['<br />', '<br/>', '<br>'], ' - ', $value));
             }
             return $element;
         } else {
             return $this->{'addText'}($name);
         }
     } else {
         dd('Unknown dynamic form type: ' . $type);
     }
 }