Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/freeform_next/Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function fields()
if (!empty($_POST)) {
try {
$model = $this->getFieldController()->save();
FreeformHelper::get('validate', $model);
FreeformHelper::validate($model);

$view->addVariable('success', true);
} catch (Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion src/freeform_next/Controllers/FieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function index(): RedirectView|CpView
$template = [
'table' => $table->viewData(),
'cp_page_title' => lang('Fields'),
'form_right_links' => FreeformHelper::get('right_links', $this),
'form_right_links' => FreeformHelper::getRightLinks($this),
'footer' => [
'submit_lang' => lang('submit'),
'type' => 'bulk_action_form',
Expand Down
2 changes: 1 addition & 1 deletion src/freeform_next/Controllers/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function index(): CpView
];

if ($canManageForms) {
$template['form_right_links'] = FreeformHelper::get('right_links', $this);
$template['form_right_links'] = FreeformHelper::getRightLinks($this);
}

$template['footer'] = ['submit_lang' => lang('submit'), 'type' => 'bulk_action_form'];
Expand Down
2 changes: 1 addition & 1 deletion src/freeform_next/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private function spamProtectionAction(): CpView
*/
private function permissionsAction(): CpView
{
$version = FreeformHelper::get('version');
$version = FreeformHelper::getVersion();

$permissionsModel = $this->getPermissionsModel();

Expand Down
6 changes: 3 additions & 3 deletions src/freeform_next/Controllers/StatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public function index(): CpView
];
}

$tableData = FreeformHelper::get('column_count', $tableData);
$columns = FreeformHelper::get('columns', $columns);
$tableData = FreeformHelper::getColumnCount($tableData);
$columns = FreeformHelper::getColumns($columns);

$table->setColumns($columns);
$table->setData($tableData);
Expand All @@ -84,7 +84,7 @@ public function index(): CpView
$template = [
'table' => $table->viewData(),
'cp_page_title' => lang('Statuses'),
'form_right_links' => FreeformHelper::get('right_links', $this),
'form_right_links' => FreeformHelper::getRightLinks($this),
'footer' => [
'submit_lang' => lang('submit'),
'type' => 'bulk_action_form',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function copy($target, ?string $prefix = null, null|callable|array $calla
}

if (is_callable($callable)) {
call_user_func_array($callable, [$newFilePath, $prefix]);
$callable($newFilePath, $prefix);
}
}
}
2 changes: 1 addition & 1 deletion src/freeform_next/Library/Composer/Components/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ public function setAttributes(?array $attributes = null)
*/
public function populateFromSubmission(SubmissionModel|int|string|null $token = null)
{
if (null === $token || FreeformHelper::get('version') !== FREEFORM_PRO) {
if (null === $token || FreeformHelper::getVersion() !== FREEFORM_PRO) {
return $this;
}

Expand Down
5 changes: 2 additions & 3 deletions src/freeform_next/Library/Helpers/ExtensionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ class ExtensionHelper
*
* @return bool
*/
public static function call($hookName, mixed $arg1 = null): bool
public static function call(string $hookName, mixed ...$args): bool
{
$args = func_get_args();
$extClass = ee()->extensions;

call_user_func_array([$extClass, 'call'], $args);
$extClass->call($hookName, ...$args);

return !$extClass->end_script;
}
Expand Down
261 changes: 132 additions & 129 deletions src/freeform_next/Library/Helpers/FreeformHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,155 +14,158 @@

class FreeformHelper
{
/**
* @param string $name
*
* @return mixed
*/
public static function get($name)
public static function getVersion(): string
{
$args = func_get_args();
if (file_exists(PATH_THIRD . '/freeform_next/Library/Pro')) {
return FREEFORM_PRO;
}

$return = null;
if (file_exists(PATH_THIRD . '/freeform_next/ft.freeform_next.php')) {
return FREEFORM_LITE;
}

if ($name === 'version') {
if (file_exists(PATH_THIRD . '/freeform_next/Library/Pro')) {
$return = FREEFORM_PRO;
} elseif (file_exists(PATH_THIRD . '/freeform_next/ft.freeform_next.php')) {
$return = FREEFORM_LITE;
} else {
$return = FREEFORM_EXPRESS;
}
} elseif ($name === 'validate') {
$version = FreeformHelper::get('version');

if ($version === FREEFORM_EXPRESS) {
$item = $args[1];

if ($item instanceof FormModel) {
$count = (int) ee()->db
->select('COUNT(*) as total')
->get('freeform_next_forms')
->row()
->total;

if (!$item->id && $count > 0) {
throw new Exception('Form limit reached');
}
} elseif ($item instanceof FieldModel) {
$count = (int) ee()->db
->select('COUNT(*) as total')
->get('freeform_next_fields')
->row()
->total;

if (!$item->id && $count >= 15) {
throw new Exception('Maximum limit of 15 fields reached.');
}
}
}
} elseif ($name === 'props') {
$version = FreeformHelper::get('version');

$return = $version === FREEFORM_EXPRESS;
} elseif ($name === 'right_links') {
$version = FreeformHelper::get('version');
$item = $args[1];
$link = $title = '';
$showLink = false;

if ($item instanceof FormController) {
$count = count(FormRepository::getInstance()->getAllForms());

$showLink = $version !== FREEFORM_EXPRESS || $count === 0;
$link = 'forms/new';
$title = 'New Form';
} elseif ($item instanceof FieldController) {
$count = count(FieldRepository::getInstance()->getAllFields());

$showLink = $version !== FREEFORM_EXPRESS || $count < 15;
$link = 'fields/new';
$title = 'New Field';
} elseif ($item instanceof StatusController) {
$showLink = $version !== FREEFORM_EXPRESS;
$link = 'settings/statuses/new';
$title = 'New Status';
}
return FREEFORM_EXPRESS;
}

if ($showLink) {
$return = [
[
'title' => lang($title),
'link' => UrlHelper::getLink($link),
],
];
} else {
$return = [];
}
} elseif ($name === 'name') {
$version = FreeformHelper::get('version');

if ($version === FREEFORM_PRO) {
$return = 'Freeform';
} elseif ($version === FREEFORM_LITE) {
$return = 'Freeform Lite';
} else {
$return = 'Freeform Express';
public static function validate(FormModel|FieldModel $item): void
{
if (self::getVersion() !== FREEFORM_EXPRESS) {
return;
}

if ($item instanceof FormModel) {
$count = (int) ee()->db
->select('COUNT(*) as total')
->get('freeform_next_forms')
->row()
->total;

if (!$item->id && $count > 0) {
throw new Exception('Form limit reached');
}
} elseif ($name === 'columns') {
$version = FreeformHelper::get('version');
$columns = $args[1];

if ($version === FREEFORM_EXPRESS) {
$columns = array_slice($columns, 0, (is_countable($columns) ? count($columns) : 0) - 2, true);
return;
}

if ($item instanceof FieldModel) {
$count = (int) ee()->db
->select('COUNT(*) as total')
->get('freeform_next_fields')
->row()
->total;

if (!$item->id && $count >= 15) {
throw new Exception('Maximum limit of 15 fields reached.');
}
}
}

$return = $columns;
} elseif ($name === 'column_count') {
$version = FreeformHelper::get('version');
$columns = $args[1];
public static function isExpressEdition(): bool
{
return self::getVersion() === FREEFORM_EXPRESS;
}

if ($version === FREEFORM_EXPRESS) {
$newColumns = [];
public static function getRightLinks(FormController|FieldController|StatusController $controller): array
{
$version = self::getVersion();
$link = '';
$title = '';
$showLink = false;

if ($controller instanceof FormController) {
$count = count(FormRepository::getInstance()->getAllForms());

$showLink = $version !== FREEFORM_EXPRESS || $count === 0;
$link = 'forms/new';
$title = 'New Form';
} elseif ($controller instanceof FieldController) {
$count = count(FieldRepository::getInstance()->getAllFields());

$showLink = $version !== FREEFORM_EXPRESS || $count < 15;
$link = 'fields/new';
$title = 'New Field';
} elseif ($controller instanceof StatusController) {
$showLink = $version !== FREEFORM_EXPRESS;
$link = 'settings/statuses/new';
$title = 'New Status';
}

foreach ($columns as $column) {
$data = array_slice($column, 0, (is_countable($column) ? count($column) : 0) - 2, true);
$data[1]['content'] = strip_tags($data[1]['content'], '<span>');
if (!$showLink) {
return [];
}

$newColumns[] = $data;
}
return [
[
'title' => lang($title),
'link' => UrlHelper::getLink($link),
],
];
}

$return = $newColumns;
} else {
$return = $columns;
}
} elseif ($name === 'navigation') {
$version = FreeformHelper::get('version');
public static function getEditionName(): string
{
$version = self::getVersion();

/** @var NavigationLink $item */
$item = $args[1];
if ($version === FREEFORM_PRO) {
return 'Freeform';
}

$link = '';
$showLink = false;
if ($version === FREEFORM_LITE) {
return 'Freeform Lite';
}

if ($item->getMethod() === 'fields') {
$count = count(FieldRepository::getInstance()->getAllFields());
return 'Freeform Express';
}

$link = 'fields/new';
$showLink = $version !== FREEFORM_EXPRESS || ($count < 15);
} elseif ($item->getMethod() === 'forms') {
$count = count(FormRepository::getInstance()->getAllForms());
public static function getColumns(array $columns): array
{
if (self::getVersion() !== FREEFORM_EXPRESS) {
return $columns;
}

$link = 'forms/new';
$showLink = $version !== FREEFORM_EXPRESS || ($count < 1);
}
return array_slice($columns, 0, count($columns) - 2, true);
}

if ($showLink) {
$item->setButtonLink(new NavigationLink('New', $link));
}
public static function getColumnCount(array $columns): array
{
if (self::getVersion() !== FREEFORM_EXPRESS) {
return $columns;
}

return $return;
$newColumns = [];

foreach ($columns as $column) {
$data = array_slice($column, 0, count($column) - 2, true);
$data[1]['content'] = strip_tags($data[1]['content'], '<span>');

$newColumns[] = $data;
}

return $newColumns;
}

public static function getNavigation(NavigationLink $item): void
{
$version = self::getVersion();

$link = '';
$showLink = false;

if ($item->getMethod() === 'fields') {
$count = count(FieldRepository::getInstance()->getAllFields());

$link = 'fields/new';
$showLink = $version !== FREEFORM_EXPRESS || $count < 15;
} elseif ($item->getMethod() === 'forms') {
$count = count(FormRepository::getInstance()->getAllForms());

$link = 'forms/new';
$showLink = $version !== FREEFORM_EXPRESS || $count < 1;
}

if ($showLink) {
$item->setButtonLink(new NavigationLink('New', $link));
}
}

public static function isFreeformAtLeast(string $minVersion): bool
Expand Down
2 changes: 1 addition & 1 deletion src/freeform_next/Model/FieldModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ private function getTimestampableDate(): string
*/
public function onBeforeSave(): void
{
FreeformHelper::get('validate', $this);
FreeformHelper::validate($this);
}

/**
Expand Down
Loading