1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
<?php
namespace hji\construction\models;
use hji\common\utils\GFHelper;
use hji\common\interfaces\AbstractSettingsModel;
use hji\construction\ServiceContainer;
class Settings extends AbstractSettingsModel
{
protected $formsFileName = 'new-construction-gravityforms.json';
protected $serviceContainer;
protected $optionGroups = array(
'general' => 'General'
);
function __construct($pluginSlug, $settingsKey = false, ServiceContainer $serviceContainer)
{
parent::__construct($pluginSlug, $settingsKey);
$this->optionGroupsKeys = array_keys($this->optionGroups);
$this->serviceContainer = $serviceContainer;
$this->loadOptions();
}
function setDefaults()
{
$this->installDefaultForms();
}
function installDefaultForms()
{
if (!GFHelper::gravityFormsExists())
{
return new \WP_Error('Error', 'Gravity Forms plugin is required.');
}
if ($formsExist = GFHelper::getFormsByKey('new_construction'))
{
return new \WP_Error('Error', 'New Construction forms already exist. No new forms were installed.');
}
if ($this->importForms())
return $this->setDefaultFormShortcodes();
}
function deleteDefaultForms()
{
if (!GFHelper::gravityFormsExists())
return;
$forms = GFHelper::getFormsListByKey('new_construction');
if (!empty($forms))
{
\GFFormsModel::delete_forms(array_keys($forms));
}
}
function importForms()
{
if (!GFHelper::gravityFormsExists())
return;
$paths = $this->serviceContainer->getPaths();
$file = $paths->getPath('forms') . '/' . $this->formsFileName;
if (file_exists($file))
{
return GFHelper::importGravityForm($file);
}
}
function setDefaultFormShortcodes()
{
$communityForm = GFHelper::getFormsByKey('new_construction:community_inquiry');
$propertyForm = GFHelper::getFormsByKey('new_construction:property_inquiry');
$options = $this->getOptions('general');
if ($formId = $this->_getFormId($communityForm))
{
$options['community-inquiry-form'] = '[gravityform id="' . $formId .'" ajax="true"]';
}
if ($formId = $this->_getFormId($propertyForm))
{
$options['property-inquiry-form'] = '[gravityform id="' . $formId .'" ajax="true"]';
}
return $this->setOptions($options, 'general');
}
private function _getFormId($forms)
{
if (!empty($forms))
{
$form = array_pop($forms);
return (isset($form['id'])) ? $form['id'] : null;
}
}
}