| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentForm\App\Services\Transfer\TransferService; |
| 7 |
use FluentForm\Framework\Request\File; |
| 8 |
|
| 9 |
class Form { |
| 10 |
protected $plugin; |
| 11 |
protected $file; |
| 12 |
protected $settings = []; |
| 13 |
|
| 14 |
public function __construct( $plugin, $file, $settings = [] ) { |
| 15 |
$this->plugin = $plugin; |
| 16 |
$this->file = $file; |
| 17 |
$this->settings = $settings; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* @throws Exception |
| 22 |
*/ |
| 23 |
public function run() { |
| 24 |
if ( ! file_exists( $this->file ) ) { |
| 25 |
throw new Exception( __( 'Form JSON does not exists.', 'templately' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
$data = $this->import(); |
| 29 |
|
| 30 |
if ( ! $data ) { |
| 31 |
throw new Exception( __( 'Cannot be imported.', 'templately' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
return $data; |
| 35 |
} |
| 36 |
|
| 37 |
public function import() { |
| 38 |
try { |
| 39 |
$results = null; |
| 40 |
switch ( $this->plugin ) { |
| 41 |
case 'fluent-forms' && is_plugin_active( 'fluentform/fluentform.php' ) && class_exists( File::class ) && class_exists( TransferService::class ): |
| 42 |
$fileObject = new File( $this->file, '' ); |
| 43 |
$importer = new TransferService(); |
| 44 |
$inserted = $importer->importForms( $fileObject ); |
| 45 |
$results = key( $inserted['inserted_forms'] ); |
| 46 |
break; |
| 47 |
default: |
| 48 |
break; |
| 49 |
} |
| 50 |
|
| 51 |
return $results; |
| 52 |
|
| 53 |
} catch ( Exception $e ) { |
| 54 |
return null; |
| 55 |
} |
| 56 |
} |
| 57 |
} |