| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Editor; |
| 4 |
|
| 5 |
use Averta\Core\Utility\Arr; |
| 6 |
use Averta\WordPress\Utility\JSON; |
| 7 |
use Depicter\Document\Helper\Helper; |
| 8 |
|
| 9 |
class EditorData { |
| 10 |
|
| 11 |
/** |
| 12 |
* Duplicates and returns a section and corresponding elements with new IDs assigned |
| 13 |
* |
| 14 |
* @param string $sectionId Section ID |
| 15 |
* @param array $editorData EditorData in associative array |
| 16 |
* |
| 17 |
* @return array |
| 18 |
*/ |
| 19 |
public function duplicateAndReturnSectionWithElements( $sectionId = '', $editorData = [] ): array{ |
| 20 |
|
| 21 |
$clonedElements = []; |
| 22 |
$newSectionID = $this->getNextAvailableSectionId( $editorData ); |
| 23 |
$newSection = [ |
| 24 |
$newSectionID => $editorData['sections'][ $sectionId ] |
| 25 |
]; |
| 26 |
$newSection[ $newSectionID ]['id'] = $newSectionID; |
| 27 |
|
| 28 |
foreach( $editorData['sections'][ $sectionId ]['elements'] as $elementID ) { |
| 29 |
$clonedElements[ $elementID ] = $editorData['elements'][ $elementID ]; |
| 30 |
} |
| 31 |
|
| 32 |
$IdListOfNewElements = []; |
| 33 |
$duplicatedElements = []; |
| 34 |
|
| 35 |
$newElementIDNumber = $this->getNextAvailableElementNumber( $editorData ); |
| 36 |
|
| 37 |
foreach( $clonedElements as $element ) { |
| 38 |
$duplicatedElements[ 'element-' . $newElementIDNumber ] = $element; |
| 39 |
|
| 40 |
if ( isset( $duplicatedElements[ 'element-' . $newElementIDNumber ]['section'] ) ) { |
| 41 |
$duplicatedElements[ 'element-' . $newElementIDNumber ]['section'] = $newSectionID; |
| 42 |
} |
| 43 |
if ( isset( $duplicatedElements[ 'element-' . $newElementIDNumber ]['parent'] ) ) { |
| 44 |
$duplicatedElements[ 'element-' . $newElementIDNumber ]['parent'] = $newSectionID; |
| 45 |
} |
| 46 |
if ( isset( $duplicatedElements[ 'element-' . $newElementIDNumber ]['id'] ) ) { |
| 47 |
$duplicatedElements[ 'element-' . $newElementIDNumber ]['id'] = 'element-' . $newElementIDNumber; |
| 48 |
} |
| 49 |
|
| 50 |
$IdListOfNewElements[] = 'element-' . $newElementIDNumber; |
| 51 |
++$newElementIDNumber; |
| 52 |
} |
| 53 |
|
| 54 |
$newSection[ $newSectionID ]['elements'] = $IdListOfNewElements; |
| 55 |
|
| 56 |
return [ |
| 57 |
'sections' => $newSection, |
| 58 |
'elements' => $duplicatedElements |
| 59 |
]; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Duplicates a section and corresponding elements with new IDs assigned in EditorData |
| 64 |
* |
| 65 |
* @param string $sectionId Section ID |
| 66 |
* @param array $editorData EditorData in associative array |
| 67 |
* @param int $numberOfSections |
| 68 |
* |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
public function duplicateSectionWithElements( $sectionId, $editorData = [], $numberOfSections = 1 ): array{ |
| 72 |
if ( !is_null( $sectionId ) && !empty( $numberOfSections ) && $numberOfSections > 1 ) { |
| 73 |
for ( $i = 1; $i < $numberOfSections; $i++ ) { |
| 74 |
|
| 75 |
$duplicates = $this->duplicateAndReturnSectionWithElements( $sectionId, $editorData ); |
| 76 |
|
| 77 |
if( ! empty( $duplicates['sections'] ) ){ |
| 78 |
foreach ( $duplicates['sections'] as $sectionID => $elements ) { |
| 79 |
$editorData['sectionsList'][] = $sectionID; |
| 80 |
} |
| 81 |
$editorData['sections'] = Arr::merge( $duplicates['sections'], $editorData['sections'] ); |
| 82 |
} |
| 83 |
if( ! empty( $duplicates['elements'] ) ){ |
| 84 |
$editorData['elements'] = Arr::merge( $duplicates['elements'], $editorData['elements'] ); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return $editorData; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Get a new unreserved section ID |
| 94 |
* |
| 95 |
* @param array $editorData EditorData in associative array |
| 96 |
* |
| 97 |
* @return string New section ID |
| 98 |
*/ |
| 99 |
protected function getNextAvailableSectionId( $editorData = [] ){ |
| 100 |
|
| 101 |
$sectionIDs = []; |
| 102 |
foreach( $editorData['sections'] as $sectionID => $elementsID ) { |
| 103 |
$sectionIDs[] = str_replace( 'section-', '', $sectionID ); |
| 104 |
} |
| 105 |
|
| 106 |
$sectionMaxID = max( $sectionIDs ) + 1; |
| 107 |
return 'section-' . $sectionMaxID; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get a new unreserved element ID |
| 112 |
* |
| 113 |
* @param array $editorData EditorData in associative array |
| 114 |
* |
| 115 |
* @return string New element ID |
| 116 |
*/ |
| 117 |
protected function getNextAvailableElementId( $editorData = [] ){ |
| 118 |
return 'element-' . $this->getNextAvailableElementNumber( $editorData ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get a new unreserved element index number |
| 123 |
* |
| 124 |
* @param array $editorData EditorData in associative array |
| 125 |
* |
| 126 |
* @return string New element ID |
| 127 |
*/ |
| 128 |
protected function getNextAvailableElementNumber( $editorData = [] ){ |
| 129 |
$elementsID = []; |
| 130 |
foreach( $editorData['elements'] as $elementID => $element ) { |
| 131 |
$elementsID[] = str_replace( 'element-','', $elementID ); |
| 132 |
} |
| 133 |
return max( $elementsID ) + 1; |
| 134 |
} |
| 135 |
} |
| 136 |
|