示例#1
0
文件: cepg.php 项目: Hawkart/megatv
 /**
  * Import Epg file 
  */
 public function import()
 {
     $arProgCropIds = array();
     //prog id list to crop image
     $arScheduleIdsNotDelete = array();
     $arProgTimeDelete = array();
     //Расписание, которое нужно удалить
     $arCategories = self::importCategory();
     $arGanres = self::importGanre();
     $arTopics = self::importTopic();
     $arCountries = self::importCountry();
     //$arProductions = self::importProduction();
     $arRoles = self::importRole();
     $arPeople = self::importPeople($arRoles);
     $arBaseChannels = $this->base_channels;
     $arChannels = $this->channels;
     //Delete cropped images
     //\CDev::deleteOldFiles($_SERVER["DOCUMENT_ROOT"]. self::$cut_dir, 0);
     //Delete schedule before prev day
     ScheduleTable::deleteOld();
     /**
      * Get prog's list
      */
     $arProgs = array();
     $result = ProgTable::getList(array('filter' => array("!UF_EPG_ID" => false), 'select' => array("UF_EPG_ID", "UF_EPG_SUB_ID", "ID", "UF_IMG_ID")));
     while ($row = $result->fetch()) {
         $arProgs[$row["UF_EPG_ID"] . $row["UF_EPG_SUB_ID"]] = $row;
     }
     /**
      * Get schedule's list
      */
     $arSchedules = array();
     $result = ScheduleTable::getList(array('filter' => array(), 'select' => array("UF_EPG_ID", "ID")));
     while ($row = $result->fetch()) {
         $arSchedules[$row["UF_EPG_ID"]] = $row["ID"];
     }
     foreach ($this->href_channels as $channel_epg_id => $xml_urls) {
         $arChannel = $arChannels[$channel_epg_id];
         $active = intval($arBaseChannels[$arChannel["UF_BASE_EPG_ID"]]["UF_ACTIVE"]);
         if (intval($arChannel["ID"]) <= 0 || empty($arChannel["ID"]) || $active != 1 || empty($channel_epg_id)) {
             continue;
         }
         foreach ($xml_urls as $xml_url) {
             $xml = simplexml_load_file($xml_url);
             //$attr = $xml->channel->attributes();
             //$channel_epg_id = (string)$attr["id"];
             /**
              * Update Progs
              */
             foreach ($xml->programme as $arProg) {
                 $_arProg = $arProg;
                 $json = json_encode($arProg);
                 $arProg = json_decode($json, TRUE);
                 $arFields = array("UF_ACTIVE" => 1, "UF_TITLE" => trim((string) $arProg["title"]), "UF_DESC" => (string) $_arProg->desc, "UF_YEAR_LIMIT" => (int) $arProg["rating"]["value"], "UF_YEAR" => (string) $arProg["year"]);
                 if (isset($arProg["title"])) {
                     $attr = $_arProg->title->attributes();
                     $arFields["UF_EPG_ID"] = (string) $attr["id"];
                     $prog_epg_id = (string) $attr["id"];
                 }
                 if (isset($arProg["sub-title"]) && !empty($arProg["sub-title"])) {
                     $arFields["UF_SUB_TITLE"] = trim($arProg["sub-title"]);
                     $attr = $_arProg->{'sub-title'}->attributes();
                     $arFields["UF_EPG_SUB_ID"] = (string) $attr["id"];
                     $prog_epg_id .= $arFields["UF_EPG_SUB_ID"];
                 } else {
                     //get sub_epg_id from full id if no sub-title
                     $full_epg_id = (string) $arProg["@attributes"]["id"];
                     $pos = strripos($full_epg_id, $prog_epg_id) + strlen($prog_epg_id);
                     if ($pos != strlen($full_epg_id)) {
                         $epg_sub_id = substr($full_epg_id, $pos);
                         if (!empty($epg_sub_id)) {
                             $arFields["UF_EPG_SUB_ID"] = $epg_sub_id;
                             $prog_epg_id .= $arFields["UF_EPG_SUB_ID"];
                         }
                     }
                 }
                 if (!array_key_exists($prog_epg_id, $arProgs)) {
                     if (!empty($_arProg->{"desc-sub-title"})) {
                         $arFields["UF_SUB_DESC"] = (string) $_arProg->{"desc-sub-title"};
                     }
                     if (!empty($_arProg->{"episode-number"})) {
                         $arFields["UF_SERIA"] = (string) $_arProg->{"episode-number"};
                     }
                     if (!empty($_arProg->season)) {
                         $arFields["UF_SEASON"] = (string) $_arProg->season;
                     }
                     if (!is_array($arProg["category"])) {
                         $arProg["category"] = array($arProg["category"]);
                     }
                     $arFields["UF_CATEGORY"] = implode(", ", $arProg["category"]);
                     if (!is_array($arProg["topic"])) {
                         $arProg["topic"] = array($arProg["topic"]);
                     }
                     $arFields["UF_TOPIC"] = implode(", ", $arProg["topic"]);
                     if (!is_array($arProg["ganre"])) {
                         $arProg["ganre"] = array($arProg["ganre"]);
                     }
                     $arFields["UF_GANRE"] = implode(", ", $arProg["ganre"]);
                     if (!is_array($arProg["country"])) {
                         $arProg["country"] = array($arProg["country"]);
                     }
                     $arFields["UF_COUNTRY"] = implode(", ", $arProg["country"]);
                     if (!is_array($arProg["credits"]["director"])) {
                         $arProg["credits"]["director"] = array($arProg["credits"]["director"]);
                     }
                     $arFields["UF_DIRECTOR"] = implode(", ", $arProg["credits"]["director"]);
                     if (!is_array($arProg["credits"]["actor"])) {
                         $arProg["credits"]["actor"] = array($arProg["credits"]["actor"]);
                     }
                     $arFields["UF_ACTOR"] = implode(", ", $arProg["credits"]["actor"]);
                     if (!is_array($arProg["credits"]["presenter"])) {
                         $arProg["credits"]["presenter"] = array($arProg["credits"]["presenter"]);
                     }
                     $arFields["UF_PRESENTER"] = implode(", ", $arProg["credits"]["presenter"]);
                     /**
                      * Bugfix: Problem with sub-title id.
                      * If no sub-title id, but it's serial, update previous prog
                      */
                     if (array_key_exists($arFields["UF_EPG_ID"], $arProgs) && $prog_epg_id != $arFields["UF_EPG_ID"]) {
                         $arProgs[$prog_epg_id] = $arProgs[$arFields["UF_EPG_ID"]];
                         unset($arProgs[$arFields["UF_EPG_ID"]]);
                         $prog_id = $arProgs[$prog_epg_id]["ID"];
                         ProgTable::Update($prog_id, $arFields);
                     } else {
                         $result = ProgTable::add($arFields);
                         if ($result->isSuccess()) {
                             $prog_id = $result->getId();
                             $arProgs[$prog_epg_id] = array("ID" => $prog_id, "UF_IMG_ID" => 0);
                         } else {
                             $errors = $result->getErrorMessages();
                         }
                     }
                 } else {
                     $prog_id = $arProgs[$prog_epg_id]["ID"];
                 }
                 $image_id = intval($arProgs[$prog_epg_id]["UF_IMG_ID"]);
                 //--------------Add original image----------------
                 if ($image_id == 0) {
                     $arFields = array();
                     $icons = array();
                     if (!is_array($arProg["icon"])) {
                         $arProg["icon"] = array($arProg["icon"]);
                     }
                     $ar_src = self::addImage($arProg["icon"]);
                     if ($ar_src) {
                         $result = ImageTable::getList(array('filter' => array("=UF_EXTERNAL_ID" => md5($ar_src["origin_path"])), 'select' => array("ID")));
                         if ($row = $result->fetch()) {
                             $arFields["UF_IMG_ID"] = (int) $row["ID"];
                         } else {
                             $resultImg = ImageTable::add(array("UF_PATH" => $ar_src["server_path"], "UF_EXTERNAL_ID" => md5($ar_src["origin_path"]), "UF_WIDTH" => intval($ar_src["width"]), "UF_HEIGHT" => intval($ar_src["height"])));
                             if ($resultImg->isSuccess()) {
                                 $arFields["UF_IMG_ID"] = (int) $resultImg->getId();
                             }
                         }
                     }
                     ProgTable::Update($prog_id, $arFields);
                 }
                 //------------------------------------------------
                 /**
                  * Adding schedules
                  */
                 $schedule_epg_id = trim((string) $arProg["@attributes"]["id"]);
                 $dateStart = new \Bitrix\Main\Type\DateTime(date("Y-m-d H:i:s", strtotime($arProg["@attributes"]["start"])), 'Y-m-d H:i:s');
                 $dateEnd = new \Bitrix\Main\Type\DateTime(date("Y-m-d H:i:s", strtotime($arProg["@attributes"]["stop"])), 'Y-m-d H:i:s');
                 $date = new \Bitrix\Main\Type\Date(date("Y-m-d", strtotime($arProg["date"])), 'Y-m-d');
                 if (!array_key_exists($schedule_epg_id, $arSchedules)) {
                     $title = (string) $arProg["title"];
                     if (!empty($arProg["sub-title"])) {
                         $title .= " | " . trim($arProg["sub-title"]);
                     }
                     $arFields = array("UF_ACTIVE" => 1, "UF_DATE_START" => $dateStart, "UF_DATE_END" => $dateEnd, "UF_DATE" => $date, "UF_CHANNEL_ID" => (int) $arChannel["ID"], "UF_PROG_ID" => (int) $prog_id, "UF_EPG_ID" => $schedule_epg_id, "UF_CODE" => $title . " - " . $arProg["@attributes"]["start"], "UF_DATETIME_CREATE" => new \Bitrix\Main\Type\Date(date("Y-m-d H:i:s"), 'Y-m-d H:i:s'));
                     $result = ScheduleTable::add($arFields);
                     if ($result->isSuccess()) {
                         $schedule_id = $result->getId();
                         $arSchedules[$schedule_epg_id] = $schedule_id;
                     } else {
                         $errors = $result->getErrorMessages();
                     }
                 } else {
                     $schedule_id = (int) $arSchedules[$schedule_epg_id];
                     $arFields = array("UF_DATE_START" => $dateStart, "UF_DATE_END" => $dateEnd, "UF_DATE" => $date, "UF_DATETIME_EDIT" => new \Bitrix\Main\Type\Date(date("Y-m-d H:i:s"), 'Y-m-d H:i:s'));
                     ScheduleTable::Update($schedule_id, $arFields);
                 }
                 $arScheduleIdsNotDelete[] = $schedule_id;
                 $arProgCropIds[] = $prog_id;
             }
         }
     }
     /**
      * Delete not exist schedule
      */
     if (!empty($arScheduleIdsNotDelete)) {
         $result = ScheduleTable::getList(array('filter' => array(">=UF_DATE" => new \Bitrix\Main\Type\Date(date("Y-m-d"), 'Y-m-d')), 'select' => array("ID")));
         while ($row = $result->fetch()) {
             if (!in_array($row["ID"], $arScheduleIdsNotDelete)) {
                 ScheduleTable::delete($row["ID"]);
             }
         }
     }
     //Make cut images for imported progs
     self::addCropFiles($arProgCropIds);
     ProgTable::generateCodes();
     unset($arScheduleIdsNotDelete);
     unset($arProgCropIds);
     self::guessSerials();
 }