@@ -3,14 +3,14 @@ class Dcim_Api_Location extends Dcim_Api_Abstract
33 {
44 const OBJECT_TYPE = 'location ' ;
55 const REPORT_NAMES = array (
6+ 'root ' => 'CW - TOOLS-CLI - Location - Root ' ,
67 'label ' => 'CW - TOOLS-CLI - Location1 ' ,
78 'location ' => 'CW - TOOLS-CLI - Location2 ' ,
89 'subLocation ' => 'CW - TOOLS-CLI - Location3 ' ,
910 );
1011
11- const ROOT_SITE = 'Cloudwatt ' ;
12-
1312 static protected $ _rootLocationId = null ;
13+ static protected $ _rootLocationIds = null ;
1414
1515 protected $ _parentLocationId ;
1616 protected $ _parentLocationApi ;
@@ -47,21 +47,30 @@ public function getParentLocationId()
4747 {
4848 if ($ this ->_parentLocationId === null )
4949 {
50+ $ this ->_parentLocationId = false ;
51+
5052 $ path = $ this ->getPath ();
5153 $ path = explode (', ' , $ path );
5254
53- $ locationId = $ this ->getRootLocationId ();
55+ $ selfClassName = static ::class;
56+ $ Dcim_Api_Location = new $ selfClassName ();
57+ $ locationId = $ Dcim_Api_Location ->getSubLocationId ($ path [0 ]);
5458
55- for ( $ i = 1 ; $ i < count ( $ path ); $ i ++ )
59+ if ( $ locationId !== false )
5660 {
57- $ locationId = $ this ->_DCIM ->getLocationIdByParentLocationIdLocationLabel ($ locationId , $ path [$ i ], false );
61+ for ($ i =1 ; $ i <count ($ path ); $ i ++)
62+ {
63+ $ locationId = $ this ->_DCIM ->getLocationIdByParentLocationIdLocationLabel ($ locationId , $ path [$ i ], false );
5864
59- if ($ locationId === false ) {
60- break ;
65+ if ($ locationId === false ) {
66+ break ;
67+ }
6168 }
62- }
6369
64- $ this ->_parentLocationId = ($ i === count ($ path )) ? ($ locationId ) : (false );
70+ if ($ i === count ($ path )) {
71+ $ this ->_parentLocationId = $ locationId ;
72+ }
73+ }
6574 }
6675
6776 return $ this ->_parentLocationId ;
@@ -105,7 +114,7 @@ public function getSubLocationIds()
105114 return $ this ->_DCIM ->getSubLocationIds ($ this ->getLocationId (), false );
106115 }
107116 else {
108- return false ;
117+ return $ this -> getRootLocationIds () ;
109118 }
110119 }
111120
@@ -115,7 +124,9 @@ public function getSubLocationId($locationLabel)
115124 return $ this ->_DCIM ->getLocationIdByParentLocationIdLocationLabel ($ this ->getLocationId (), $ locationLabel , false );
116125 }
117126 else {
118- return false ;
127+ $ results = self ::$ _DCIM ->getReportResults (self ::REPORT_NAMES ['root ' ]);
128+ $ result = $ this ->_getSubObjects ($ results , 'name ' , $ locationLabel );
129+ return (Tools::is ('array ' , $ result ) && count ($ result ) === 1 ) ? ($ result [0 ]['entity_id ' ]) : (false );
119130 }
120131 }
121132
@@ -155,10 +166,9 @@ public function __call($method, $parameters = null)
155166 {
156167 switch ($ method )
157168 {
158- /*case 'getRootLocationId':
159- return self::getRootLocationId();*/
160- default :
169+ default : {
161170 throw new Exception ('Method ' .$ method .' does not exist ' , E_USER_ERROR );
171+ }
162172 }
163173 }
164174
@@ -177,14 +187,43 @@ public function __get($name)
177187
178188 public static function getRootLocationId ()
179189 {
180- if (self ::$ _rootLocationId === null ) {
181- $ result = self ::$ _DCIM ->getSiteId (self ::ROOT_SITE );
182- self ::$ _rootLocationId = (self ::$ _DCIM ->isValidReturn ($ result )) ? ($ result ) : (false );
190+ if (self ::$ _rootLocationId === null )
191+ {
192+ $ results = self ::$ _DCIM ->getReportResults (self ::REPORT_NAMES ['root ' ]);
193+
194+ if (Tools::is ('array ' , $ results ) && count ($ results ) === 1 ) {
195+ self ::$ _rootLocationId = $ results [0 ]['entity_id ' ];
196+ }
197+ else {
198+ throw new Exception ("Unable to get root location ID " , E_USER_ERROR );
199+ }
183200 }
184201
185202 return self ::$ _rootLocationId ;
186203 }
187204
205+ public static function getRootLocationIds ()
206+ {
207+ if (self ::$ _rootLocationIds === null )
208+ {
209+ $ results = self ::$ _DCIM ->getReportResults (self ::REPORT_NAMES ['root ' ]);
210+
211+ if (Tools::is ('array&&count>0 ' , $ results ))
212+ {
213+ array_walk ($ results , function (&$ item ) {
214+ $ item = $ item ['entity_id ' ];
215+ });
216+
217+ self ::$ _rootLocationIds = $ results ;
218+ }
219+ else {
220+ throw new Exception ("Unable to retreive root location IDs " , E_USER_ERROR );
221+ }
222+ }
223+
224+ return self ::$ _rootLocationIds ;
225+ }
226+
188227 public static function searchLocations ($ locationLabel , $ locationId = null , $ recursion = false )
189228 {
190229 $ args = array ('label ' => $ locationLabel );
0 commit comments