98 lines
2.8 KiB
PHP
98 lines
2.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Services;
|
||
|
|
|
||
|
|
use App\Models\CustomPostType;
|
||
|
|
use App\Models\CustomField;
|
||
|
|
use Exception;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Service to handle Content Modeling (CPTs and Custom Fields).
|
||
|
|
*/
|
||
|
|
class ContentModelerService
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Store a newly created custom post type.
|
||
|
|
*
|
||
|
|
* @param array $data Data for the custom post type.
|
||
|
|
* @return CustomPostType The created model instance.
|
||
|
|
*/
|
||
|
|
public function storeCustomPostType(array $data): CustomPostType
|
||
|
|
{
|
||
|
|
return CustomPostType::create($data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the specified custom post type.
|
||
|
|
*
|
||
|
|
* @param CustomPostType $customPostType The CPT model to update.
|
||
|
|
* @param array $data The new data.
|
||
|
|
* @return CustomPostType The updated model instance.
|
||
|
|
*/
|
||
|
|
public function updateCustomPostType(CustomPostType $customPostType, array $data): CustomPostType
|
||
|
|
{
|
||
|
|
$customPostType->update($data);
|
||
|
|
return $customPostType;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Remove the specified custom post type and its associated fields.
|
||
|
|
*
|
||
|
|
* @param CustomPostType $customPostType The CPT model to delete.
|
||
|
|
* @return bool True if successful.
|
||
|
|
*/
|
||
|
|
public function deleteCustomPostType(CustomPostType $customPostType): bool
|
||
|
|
{
|
||
|
|
return (bool) $customPostType->delete();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Store a newly created custom field for a custom post type.
|
||
|
|
*
|
||
|
|
* @param CustomPostType $customPostType The parent CPT.
|
||
|
|
* @param array $data Data for the new field.
|
||
|
|
* @return CustomField The created field instance.
|
||
|
|
*/
|
||
|
|
public function storeCustomField(CustomPostType $customPostType, array $data): CustomField
|
||
|
|
{
|
||
|
|
return $customPostType->fields()->create($data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the specified custom field.
|
||
|
|
*
|
||
|
|
* @param CustomField $customField The field model to update.
|
||
|
|
* @param array $data The new data.
|
||
|
|
* @return CustomField The updated model instance.
|
||
|
|
*/
|
||
|
|
public function updateCustomField(CustomField $customField, array $data): CustomField
|
||
|
|
{
|
||
|
|
$customField->update($data);
|
||
|
|
return $customField;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Remove the specified custom field.
|
||
|
|
*
|
||
|
|
* @param CustomField $customField The field model to delete.
|
||
|
|
* @return bool True if successful.
|
||
|
|
*/
|
||
|
|
public function deleteCustomField(CustomField $customField): bool
|
||
|
|
{
|
||
|
|
return (bool) $customField->delete();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Reorder fields for a custom post type based on provided data.
|
||
|
|
*
|
||
|
|
* @param array $fieldsData Array of field IDs and their new sort orders.
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function reorderFields(array $fieldsData): void
|
||
|
|
{
|
||
|
|
foreach ($fieldsData as $fieldData) {
|
||
|
|
CustomField::where('id', $fieldData['id'])->update(['sort_order' => $fieldData['sort_order']]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|