| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Admin\Form\Template; |
| 4 |
|
| 5 |
use FilesystemIterator; |
| 6 |
|
| 7 |
final class TemplateProvider |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Undocumented function |
| 11 |
* |
| 12 |
* @return all |
| 13 |
*/ |
| 14 |
public function getAllTemplates() |
| 15 |
{ |
| 16 |
return $this->allTemplates(); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Undocumented function |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
protected function allTemplates() |
| 25 |
{ |
| 26 |
$dirs = new FilesystemIterator(__DIR__); |
| 27 |
foreach ($dirs as $dirInfo) { |
| 28 |
if ($dirInfo->isFile()) { |
| 29 |
$integartionBaseName = basename($dirInfo); |
| 30 |
if ('Template.php' === substr($integartionBaseName, -strlen('Template.php'))) { |
| 31 |
$className = substr($integartionBaseName, 0, strpos($integartionBaseName, '.php')); |
| 32 |
$className = __NAMESPACE__ . '\\' . $className; |
| 33 |
$templateDetail = new $className(); |
| 34 |
$Templates[] = [ |
| 35 |
'title' => $templateDetail->getTitle(), |
| 36 |
'description' => $templateDetail->getDescription(), |
| 37 |
'status' => $templateDetail->getStatus(), |
| 38 |
'thumbnail' => $templateDetail->getThumbnail(), |
| 39 |
'category' => $templateDetail->getCategory() |
| 40 |
]; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
return json_encode($Templates); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Undocumented function |
| 49 |
* |
| 50 |
* @param String $name |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
protected function setTemplate($name = 'Contact Form', $newFormId) |
| 54 |
{ |
| 55 |
$name = is_null($name) ? 'Contact Form' : $name; |
| 56 |
$name = str_replace(' ', '', $name); |
| 57 |
$file = false !== strpos($name, 'Template') ? $name . '.php' : $name . 'Template.php'; |
| 58 |
if (file_exists(__DIR__ . '/' . $file)) { |
| 59 |
$className = __NAMESPACE__ . '\\' . $name . 'Template'; |
| 60 |
$templateDetail = new $className(); |
| 61 |
return [ |
| 62 |
'fields' => json_decode($templateDetail->getFields($newFormId)), |
| 63 |
'layout' => json_decode($templateDetail->getLayout($newFormId)), |
| 64 |
'form_name' => $templateDetail->getTitle() |
| 65 |
]; |
| 66 |
} else { |
| 67 |
return false; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* This function helps to get TEMPLATE |
| 73 |
* |
| 74 |
* @return bool setTemplate() |
| 75 |
*/ |
| 76 |
public function getTemplate($name, $newFormId) |
| 77 |
{ |
| 78 |
return $this->setTemplate($name, $newFormId); |
| 79 |
} |
| 80 |
} |
| 81 |
|