| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * CodeEditor for Craft CMS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Provides a code editor field with Twig & Craft API autocomplete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @link      https://nystudio107.com | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @copyright Copyright (c) 2022 nystudio107 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace nystudio107\codeeditor\autocompletes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Craft; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use craft\base\ElementInterface; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use craft\elements\Entry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use nystudio107\codeeditor\base\ObjectParserAutocomplete; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use nystudio107\codeeditor\models\CompleteItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use nystudio107\codeeditor\types\AutocompleteTypes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use nystudio107\codeeditor\types\CompleteItemKind; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @author    nystudio107 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @package   CodeEditor | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @since     1.0.0 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | class SectionShorthandFieldsAutocomplete extends ObjectParserAutocomplete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     // Constants | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     // ========================================================================= | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public const OPTIONS_DATA_KEY = 'SectionShorthandFields'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @array properties that are defined only using the `@property` docblock annotation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public const MAGIC_GETTER_PROPERTIES = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         Entry::class => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'typeId' => "the entry type’s ID", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             'authorId' => "the entry author’s ID", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             'type' => "the entry type", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             'section' => "the entry’s section", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             'author' => "the entry’s author", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     // Public Properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     // ========================================================================= | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @var string The name of the autocomplete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public $name = 'SectionShorthandFieldsAutocomplete'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @var string The type of the autocomplete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public $type = AutocompleteTypes::TwigExpressionAutocomplete; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @var bool Whether the autocomplete should be parsed with . -delimited nested sub-properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public $hasSubProperties = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public $parseBehaviors = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public $parseMethods = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public $customPropertySortPrefix = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public $propertySortPrefix = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public $methodSortPrefix = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @var ?int The section id. A sectionId of 0 denotes we don't know what this section is, so use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * a generic `Entry` and don't generate any complete items for custom fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     public $sectionId = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     // Public Methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     // ========================================================================= | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @inerhitDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public function init(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $this->sectionId = $this->codeEditorOptions[self::OPTIONS_DATA_KEY] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Core function that generates the autocomplete array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     public function generateCompleteItems(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         if ($this->sectionId !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             // A sectionId of 0 denotes we don't know what this section is, so use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             // a generic `Entry` and don't generate any complete items for custom fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             if ($this->sectionId === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 $element = new Entry(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 $this->parseObject('', $element, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $this->addMagicGetterProperties($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $sections = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             // getSections() is used for Craft 3 & 4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             if (method_exists(Craft::$app, 'getSections')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 $sections = Craft::$app->getSections(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             // getEntries() is used for Craft 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             if (method_exists(Craft::$app, 'getEntries')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 $sections = Craft::$app->getEntries(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             // Find the entry types used in the passed in sectionId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             if ($sections && $section = $sections->getSectionById($this->sectionId)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 $entryTypes = $section->getEntryTypes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 foreach ($entryTypes as $entryType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                     // Add the native fields in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                     if ($entryType->elementType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                         $element = new $entryType->elementType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                         /* @var ElementInterface $element */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                         $this->parseObject('', $element, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                         $this->addMagicGetterProperties($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                     // Add the custom fields in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                     if (version_compare(Craft::$app->getVersion(), '4.0', '>=')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                         $customFields = $entryType->getCustomFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                         $customFields = $entryType->getFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                     foreach ($customFields as $customField) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                         $name = $customField->handle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                         $docs = $customField->instructions ?? ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                         if ($name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                             CompleteItem::create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                                 ->insertText($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                                 ->label($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                                 ->detail(Craft::t('codeeditor', 'Custom Field Shorthand')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                                 ->documentation($docs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                                 ->kind(CompleteItemKind::FieldKind) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                                 ->add($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * Add in magic getter properties that are defined only in the `@property` docblock annotation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @param ElementInterface $element | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * @return void | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 170 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |     protected function addMagicGetterProperties(ElementInterface $element): void | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |         foreach (self::MAGIC_GETTER_PROPERTIES as $key => $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |             if ($key === get_class($element)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |                 foreach ($value as $name => $docs) { | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |                     CompleteItem::create() | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |                         ->insertText($name) | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |                         ->label($name) | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |                         ->detail(Craft::t('codeeditor', 'Custom Field Shorthand')) | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |                         ->documentation($docs) | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |                         ->kind(CompleteItemKind::FieldKind) | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |                         ->add($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 187 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 188 |  |  |  |