| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace linkprofit\AmoCRM\services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use linkprofit\AmoCRM\entities\CatalogElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use linkprofit\AmoCRM\entities\CustomField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use linkprofit\AmoCRM\entities\EntityInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use linkprofit\AmoCRM\entities\Value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Class CatalogElementService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @package linkprofit\AmoCRM\services | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | class CatalogElementService extends BaseService | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @var CatalogElement[] | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     protected $entities = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @var int | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     protected $listPage = 1; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     protected $listQuery; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     protected $listParams = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @param EntityInterface|CatalogElement $catalogElement | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 11 |  |     public function add(EntityInterface $catalogElement) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 11 |  |         if ($catalogElement instanceof CatalogElement) { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 11 |  |             $this->entities[] = $catalogElement; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 11 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @param int $page | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |     public function setPage($page) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |         $this->listPage = $page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @param string $query | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |     public function setQuery($query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |         $this->listQuery = $query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      * @param array $params | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |     public function setParams(array $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |         $this->listParams = $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * @param $link | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 7 |  |     protected function composeListLink($link) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 7 |  |         $query = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 7 |  |         $query['PAGEN_1'] = $this->listPage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 7 |  |         if (!empty($this->listQuery)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 4 |  |             $query['term'] = $this->listQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 7 |  |         $query = array_merge($query, $this->listParams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 7 |  |         $link .= '?' . http_build_query($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 7 |  |         return $link; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * @deprecated | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      * @param int $page | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * @param string|null $query | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      * @param array $params | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      * @return array|bool | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 4 |  |     public function lists($page = 1, $query = null, array $params = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 4 |  |         $this->listPage = $page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 4 |  |         $this->listQuery = $query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 4 |  |         $this->listParams = $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 4 |  |         return $this->getList(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |      * @param $array | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |      * @return CatalogElement | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 11 |  |     public function parseArrayToEntity($array) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 11 |  |         $element = new CatalogElement(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 11 |  |         $element->set($array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 11 |  |         if (isset($array['custom_fields'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 7 |  |             foreach ($array['custom_fields'] as $customFieldArray) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 7 |  |                 $customField = new CustomField($customFieldArray['id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 7 |  |                 $customField->set($customFieldArray); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 7 |  |                 if (isset($customFieldArray['values'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 7 |  |                     foreach ($customFieldArray['values'] as $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 7 |  |                         $value = new Value($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 7 |  |                         $customField->addValue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 7 |  |                 $element->addCustomField($customField); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 11 |  |         return $element; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 11 |  |     protected function getLink() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 11 |  |         return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/api/v2/catalog_elements'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 160 |  |  | } |