コード例 #1
0
ファイル: Test.php プロジェクト: kshamiev/testXml
 /**
  * Обработчик ноды одноименной методу (без учета регистра)
  *
  * @param Xml_Object $xml
  * @return bool
  */
 public function offer(Xml_Object $xml)
 {
     // Main
     $params = [];
     $attributes = $xml->Get_Attributes();
     if (!isset($attributes['internal-id'])) {
         return Zero_Logs::Error('error attribute internal-id');
     }
     $params[] = $attributes['internal-id'];
     if (is_null($node = $xml->Get_Node('type')) || !$node->Get_Data()) {
         return Zero_Logs::Error('error tag type');
     }
     $params[] = $node->Get_Data();
     if (is_null($node = $xml->Get_Node('property-type')) || !$node->Get_Data()) {
         return Zero_Logs::Error('error tag property-type');
     }
     $params[] = $node->Get_Data();
     if (is_null($node = $xml->Get_Node('category')) || !$node->Get_Data()) {
         return Zero_Logs::Error('error tag category');
     }
     $params[] = $node->Get_Data();
     if (is_null($node = $xml->Get_Node('url')) || !$node->Get_Data()) {
         return Zero_Logs::Error('error tag url');
     }
     $params[] = $node->Get_Data();
     if (is_null($node = $xml->Get_Node('creation-date')) || !$node->Get_Data() || null == ($date = $this->CheckDate($xml->Get_Data()))) {
         return Zero_Logs::Error('error tag creation-date');
     }
     $params[] = $date->format('Y-m-d H:i:s');
     if (!is_null($node = $xml->Get_Node('last-update-date')) && '' != $node->Get_Data()) {
         if (is_null($date = $this->CheckDate($xml->Get_Data()))) {
             return Zero_Logs::Error('error tag last-update-date');
         }
         $params[] = $date->format('Y-m-d H:i:s');
     } else {
         $params[] = '';
     }
     if (!is_null($node = $xml->Get_Node('expire-date')) && '' != $node->Get_Data()) {
         if (is_null($date = $this->CheckDate($xml->Get_Data()))) {
             return Zero_Logs::Error('error tag expire-date');
         }
         $params[] = $date->format('Y-m-d H:i:s');
     } else {
         $params[] = '';
     }
     if (is_null($node = $xml->Get_Node('payed-adv')) || !$node->Get_Data() || !isset($this->FlagBool[$node->Get_Data()])) {
         return Zero_Logs::Error('error tag payed-adv');
     }
     $params[] = $this->FlagBool[$node->Get_Data()];
     if (is_null($node = $xml->Get_Node('manually-added')) || !$node->Get_Data() || !isset($this->FlagBool[$node->Get_Data()])) {
         return Zero_Logs::Error('error tag manually-added');
     }
     $params[] = $this->FlagBool[$node->Get_Data()];
     // Location
     if (is_null($nodeLocation = $xml->Get_Node('location'))) {
         return Zero_Logs::Error('error tag location');
     }
     if (is_null($node = $nodeLocation->Get_Node('country')) || !$node->Get_Data()) {
         return Zero_Logs::Error('error tag location country');
     }
     $params[] = $node->Get_Data();
     if (is_null($node = $nodeLocation->Get_Node('region'))) {
         $params[] = '';
     } else {
         $params[] = $node->Get_Data();
     }
     if (is_null($node = $nodeLocation->Get_Node('district'))) {
         $params[] = '';
     } else {
         $params[] = $node->Get_Data();
     }
     if (is_null($node = $nodeLocation->Get_Node('locality-name'))) {
         $params[] = '';
     } else {
         $params[] = $node->Get_Data();
     }
     if (is_null($node = $nodeLocation->Get_Node('sub-locality-name'))) {
         $params[] = '';
     } else {
         $params[] = $node->Get_Data();
     }
     if (is_null($node = $nodeLocation->Get_Node('address'))) {
         $params[] = '';
     } else {
         $params[] = $node->Get_Data();
     }
     if (is_null($node = $nodeLocation->Get_Node('direction'))) {
         $params[] = '';
     } else {
         $params[] = $node->Get_Data();
     }
     if (is_null($node = $nodeLocation->Get_Node('distance'))) {
         $params[] = '';
     } else {
         $params[] = $node->Get_Data();
     }
     if (is_null($node = $nodeLocation->Get_Node('latitude'))) {
         $params[] = '';
     } else {
         $params[] = $node->Get_Data();
     }
     if (is_null($node = $nodeLocation->Get_Node('longitude'))) {
         $params[] = '';
     } else {
         $params[] = $node->Get_Data();
     }
     // Metro
     if (is_null($nodeMetro = $nodeLocation->Get_Node('metro'))) {
         $params[] = '';
         $params[] = '';
         $params[] = '';
     } else {
         if (is_null($node = $nodeMetro->Get_Node('name'))) {
             $params[] = '';
         } else {
             $params[] = $node->Get_Data();
         }
         if (is_null($node = $nodeMetro->Get_Node('time-on-transport'))) {
             $params[] = '';
         } else {
             $params[] = $node->Get_Data();
         }
         if (is_null($node = $nodeMetro->Get_Node('time-on-foot'))) {
             $params[] = '';
         } else {
             $params[] = $node->Get_Data();
         }
     }
     if (is_null($node = $nodeLocation->Get_Node('railway-station'))) {
         $params[] = '';
     } else {
         $params[] = $node->Get_Data();
     }
     // Price
     if (is_null($nodePrice = $xml->Get_Node('price'))) {
         return Zero_Logs::Error('error tag price');
     }
     if (is_null($node = $nodePrice->Get_Node('value')) || !$node->Get_Data()) {
         return Zero_Logs::Error('error tag price value');
     }
     $params[] = $node->Get_Data();
     if (is_null($node = $nodePrice->Get_Node('currency')) || !$node->Get_Data()) {
         return Zero_Logs::Error('error tag price currency');
     }
     $params[] = $node->Get_Data();
     if (is_null($node = $nodePrice->Get_Node('period'))) {
         $params[] = '';
     } else {
         $params[] = $node->Get_Data();
     }
     if (is_null($node = $nodePrice->Get_Node('unit'))) {
         $params[] = '';
     } else {
         $params[] = $node->Get_Data();
     }
     // SalesAgent
     $params_sales = [];
     if (is_null($nodeSales = $xml->Get_Node('sales-agent'))) {
         return Zero_Logs::Error('error tag sales-agent');
     }
     if (is_null($node = $nodeSales->Get_Node('name'))) {
         $params_sales[] = '';
     } else {
         $params_sales[] = $node->Get_Data();
     }
     if (is_null($node = $nodeSales->Get_Node('phone')) || !$node->Get_Data()) {
         return Zero_Logs::Error('error tag phone');
     }
     $params_sales[] = $node->Get_Data();
     if (is_null($node = $nodeSales->Get_Node('category'))) {
         $params_sales[] = '';
     } else {
         $params_sales[] = $node->Get_Data();
     }
     if (is_null($node = $nodeSales->Get_Node('organization'))) {
         $params_sales[] = '';
     } else {
         $params_sales[] = $node->Get_Data();
     }
     if (is_null($node = $nodeSales->Get_Node('agency-id'))) {
         $params_sales[] = '';
     } else {
         $params_sales[] = $node->Get_Data();
     }
     if (is_null($node = $nodeSales->Get_Node('url'))) {
         $params_sales[] = '';
     } else {
         $params_sales[] = $node->Get_Data();
     }
     if (is_null($node = $nodeSales->Get_Node('email'))) {
         $params_sales[] = '';
     } else {
         $params_sales[] = $node->Get_Data();
     }
     if (is_null($node = $nodeSales->Get_Node('partner'))) {
         $params_sales[] = '';
     } else {
         $params_sales[] = $node->Get_Data();
     }
     // Sql
     $sql = "INSERT Offers SET\n            internal_id = %d,\n            type = %s,\n            property_type = %s,\n            category = %s,\n            url = %s,\n            creation_date = %s,\n            last_update_date = %s,\n            expire_date = %s,\n            payed_adv = %d,\n            manually_added = %d,\n            country = %s,\n            region = %s,\n            district = %s,\n            locality_name = %s,\n            sub_locality_name = %s,\n            address = %s,\n            direction = %s,\n            distance = %d,\n            latitude = %d,\n            longitude = %d,\n            metroName = %s,\n            time_on_transport = %d,\n            time_on_foot = %d,\n            railway_station = %s,\n            price = %f,\n            currency = %s,\n            period = %s,\n            unit = %s\n        ";
     $sql = Zero_DB::Escape_Format($sql, $params);
     if (!($offer_id = Zero_DB::Ins($sql))) {
         return Zero_Logs::Error('error offer save: ' . $attributes['internal-id']);
     }
     $sql = "INSERT OffersSalesAgent SET\n            name = %s,\n            phone = %s,\n            category = %s,\n            organization = %s,\n            agency_id = %d,\n            url = %s,\n            email = %s,\n            partner = %s,\n            Offers_Id = %d\n        ";
     $params_sales[] = $offer_id;
     $sql = Zero_DB::Escape_Format($sql, $params_sales);
     Zero_DB::Ins($sql);
     // Images
     if (!is_null($nodeImages = $xml->Get_Nodes('image'))) {
         $sqlImages = [];
         $sql = "INSERT INTO `OffersImages`(`Offers_Id`, `image`) VALUES ";
         foreach ($nodeImages as $node) {
             /* @var $node Xml_Object */
             if ('' != ($img = $node->Get_Data())) {
                 $sqlImages[] = '(' . $offer_id . ', ' . Zero_DB::Escape_T($img) . ')';
             }
         }
         if (0 < count($sqlImages)) {
             $sql .= implode(', ', $sqlImages);
         }
         Zero_DB::Ins($sql);
     }
     return Zero_Logs::Info('обработка предложения id: ' . $attributes['internal-id']);
 }