| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace NerdsAndCompany\Schematic\Services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Craft\Craft; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Craft\BaseApplicationComponent as BaseApplication; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Schematic Sources Service. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Sync Craft Setups. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author    Nerds & Company | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @copyright Copyright (c) 2015-2017, Nerds & Company | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @license   MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @see      http://www.nerds.company | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class Sources extends BaseApplication | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $hookedSources = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     * Get sources based on the indexFrom attribute and return them with the indexTo attribute. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     * @param string       $fieldType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     * @param string|array $sources | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     * @param string       $indexFrom | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     * @param string       $indexTo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     * @return array|string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |     */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 24 |  |    public function getMappedSources($fieldType, $sources, $indexFrom, $indexTo) | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |    { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 24 |  |        $mappedSources = $sources; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 24 |  |        if (is_array($sources)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 24 |  |            $mappedSources = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 24 |  |            foreach ($sources as $source) { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 22 |  |                $mappedSources[] = $this->getSource($fieldType, $source, $indexFrom, $indexTo); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 24 |  |            } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 24 |  |        } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 24 |  |        return $mappedSources; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |    } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     * Gets a source by the attribute indexFrom, and returns it with attribute $indexTo. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     * @TODO Break up and simplify this method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     * @param string $fieldType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     * @param string $source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     * @param string $indexFrom | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     * @param string $indexTo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 22 |  |    public function getSource($fieldType, $source, $indexFrom, $indexTo) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |    { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 22 |  |        if (strpos($source, ':') === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |            return $source; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |        } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |        /** @var BaseElementModel $sourceObject */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 20 |  |        $sourceObject = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 20 |  |        list($sourceType, $sourceFrom) = explode(':', $source); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |        switch ($sourceType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 20 |  |           case 'section': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 20 |  |           case 'createEntries': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 20 |  |           case 'deleteEntries': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 20 |  |           case 'deletePeerEntries': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 20 |  |           case 'deletePeerEntryDrafts': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 20 |  |           case 'editEntries': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 20 |  |           case 'editPeerEntries': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 20 |  |           case 'editPeerEntryDrafts': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 20 |  |           case 'publishEntries': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 20 |  |           case 'publishPeerEntries': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 20 |  |           case 'publishPeerEntryDrafts': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |               $service = Craft::app()->sections; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  |               $method = 'getSectionBy'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 2 |  |               break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 18 |  |           case 'group': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 18 |  |           case 'editCategories': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 6 |  |               $service = $fieldType == 'Users' ? Craft::app()->userGroups : Craft::app()->categories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 6 |  |               $method = 'getGroupBy'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 6 |  |               break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 12 |  |           case 'folder': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 12 |  |           case 'createSubfoldersInAssetSource': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 12 |  |           case 'removeFromAssetSource': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 12 |  |           case 'uploadToAssetSource': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 12 |  |           case 'viewAssetSource': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 4 |  |               $service = Craft::app()->schematic_assetSources; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 4 |  |               $method = 'getSourceBy'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 4 |  |               break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 8 |  |           case 'taggroup': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 2 |  |               $service = Craft::app()->tags; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |               $method = 'getTagGroupBy'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  |               break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 6 |  |           case 'field': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 2 |  |               $service = Craft::app()->fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 2 |  |               $method = 'getFieldBy'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |               break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 4 |  |           case 'editGlobalSet': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |               $service = Craft::app()->globals; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  |               $method = 'getSetBy'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |               break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |           case 'editLocale': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 2 |  |               return $source; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |        } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 18 |  |        if (isset($service) && isset($method) && isset($sourceFrom)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 18 |  |            $method = $method.ucfirst($indexFrom); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 18 |  |            $sourceObject = $service->$method($sourceFrom); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 18 |  |        } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 18 |  |        if ($sourceObject && isset($sourceType)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 18 |  |            return $sourceType.':'.$sourceObject->$indexTo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |        } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |        return $this->getHookedSource($source, $indexFrom); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |    } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * See if the source can be found in the hooked sources. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param string $source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param string $indexFrom | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     private function getHookedSource($source, $indexFrom) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $this->loadHookedSources($indexFrom); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         foreach ($this->hookedSources[$indexFrom] as $hookedSources) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             if (array_key_exists($source, $hookedSources)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 return $hookedSources[$source]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     * Load the hooked sources. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |    private function loadHookedSources($indexFrom) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |    { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |        if (!isset($this->hookedSources[$indexFrom])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |            $this->hookedSources[$indexFrom] = Craft::app()->plugins->call('registerSchematicSources'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |        } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |    } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 156 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 157 |  |  |  |