/**
  * Создание сайта
  *
  * @param array $fields параметры создаваемого сайта
  * @return string описание ошибки ('' при успешном создании)
  */
 public function _create($fields)
 {
     $siteModel = new Sppc_SiteModel();
     $site = $siteModel->createRow();
     /* @var Sppc_Site $site */
     $categoryModel = new Sppc_CategoryModel();
     $category = $categoryModel->find(explode(',', $fields['category']));
     $site->setUrl(trim($fields['domain']));
     $site->setName(trim($fields['title']));
     $site->setCreationDate(Zend_Date::now());
     $site->setDescription(trim($fields['description']));
     $site->setCategories($category);
     $site->setIdEntityPublisher($this->user_id);
     $siteStatus = 'active';
     if ($this->role != 'admin') {
         $siteConfirmationRequired = (bool) $this->global_variables->get('SitesOwnershipConfirmationRequired', 0, '0');
         $siteAdminApproveRequired = (bool) $this->global_variables->get('ApproveSites', 0, '0');
         if ($siteConfirmationRequired) {
             $siteStatus = 'unapproved';
         } else {
             if ($siteAdminApproveRequired) {
                 $siteStatus = 'pending';
             }
         }
     }
     $site->setStatus($siteStatus);
     if (array_key_exists('id_channel', $fields)) {
         $channelModel = new Sppc_ChannelModel();
         $channel = $channelModel->findObjectById($fields['id_channel']);
         if (!is_null($channel)) {
             $site->addChannel($channel);
         }
     }
     try {
         $site->save();
     } catch (Exception $e) {
         return $e->getMessage();
     }
     $siteLayoutModel = new Sppc_Site_LayoutModel();
     $siteLayoutModel->updateFromJson($site, trim($fields['layout_json']));
     if (strtolower($fields['thumb_id']) != 'default') {
         $from = $this->config->item('path_to_images') . 'thumbs/' . $fields['thumb_id'] . '.jpeg';
         $to = $this->config->item('path_to_images') . 'thumbs/' . $site->getId() . '.jpeg';
         if (file_exists($to)) {
             unlink($to);
         }
         rename($from, $to);
     }
     if ($this->role != 'admin' && $siteConfirmationRequired) {
         redirect($this->role . '/edit_site/create_complete/' . $site->getId());
         exit;
     }
     return '';
 }
 /**
  * Update site layout
  *
  * @param Sppc_Db_Table_Row_Abstract $site
  * @param string $json
  * @return Sppc_Site_Layout
  */
 public function updateFromJson(Sppc_Db_Table_Row_Abstract $site, $json)
 {
     $jsonLayout = json_decode($json);
     if (is_null($jsonLayout) || $jsonLayout === false) {
         throw new Sppc_Exception('Decode json layout error: ' . $json);
     }
     $siteLayout = $this->findBySite($site);
     if (is_null($siteLayout)) {
         $siteLayout = $this->createRow();
     }
     $siteLayout->setIdSite($site->getId());
     if (isset($jsonLayout->width)) {
         $siteLayout->setWidth($jsonLayout->width);
     }
     if (isset($jsonLayout->height)) {
         $siteLayout->setHeight($jsonLayout->height);
     }
     if (isset($jsonLayout->sizesW) && is_array($jsonLayout->sizesW)) {
         for ($i = 0; $i < 3 && $i < count($jsonLayout->sizesW); $i++) {
             $siteLayout->{'setSizeW' . ($i + 1)}($jsonLayout->sizesW[$i]);
         }
     }
     if (isset($jsonLayout->sizesH) && is_array($jsonLayout->sizesH)) {
         for ($i = 0; $i < 3 && $i < count($jsonLayout->sizesH); $i++) {
             $siteLayout->{'setSizeH' . ($i + 1)}($jsonLayout->sizesH[$i]);
         }
     }
     $siteLayout->save();
     $siteLayoutZoneModel = new Sppc_Site_Layout_ZoneModel();
     $siteLayoutZoneModel->delete(array('id_site_layout=?' => $siteLayout->getId()));
     if (isset($jsonLayout->zones) && is_array($jsonLayout->zones)) {
         // parse and save new zones:
         foreach ($jsonLayout->zones as $key => $jsonZone) {
             $zone = $siteLayoutZoneModel->createRow();
             $zone->setIdSiteLayout($siteLayout->getId());
             if (!isset($jsonZone->id)) {
                 $zone->setIdJsonZone($siteLayout->getId() . '_' . $key);
             } else {
                 $zone->setIdJsonZone($jsonZone->id);
             }
             if (isset($jsonZone->colspan)) {
                 $zone->setColspan($jsonZone->colspan);
             }
             if (isset($jsonZone->rowspan)) {
                 $zone->setRowspan($jsonZone->rowspan);
             }
             $zone->setOrder($key);
             $zone->save();
         }
     }
     // save layout channels:
     $channelModel = new Sppc_ChannelModel();
     $siteLayoutChannelModel = new Sppc_Site_Layout_ChannelModel();
     $siteLayoutChannelModel->delete(array('id_site_layout=?' => $siteLayout->getId()));
     if (isset($jsonLayout->channels) && is_array($jsonLayout->channels)) {
         foreach ($jsonLayout->channels as $key => $jsonChannel) {
             if (!isset($jsonChannel->ad_type) || $jsonChannel->ad_type == "search") {
                 continue;
             }
             if (!isset($jsonChannel->id)) {
                 continue;
             }
             $channel = $channelModel->findObjectById($jsonChannel->id);
             if (is_null($channel)) {
                 continue;
             }
             $siteChannel = $siteLayoutChannelModel->createRow();
             $siteChannel->setIdSiteLayout($siteLayout->getId());
             $siteChannel->setIdChannel($channel->getId());
             if (isset($jsonChannel->x)) {
                 $siteChannel->setX($jsonChannel->x);
             }
             if (isset($jsonChannel->y)) {
                 $siteChannel->setY($jsonChannel->y);
             }
             if (isset($jsonChannel->width)) {
                 $siteChannel->setWidth($jsonChannel->width);
             }
             if (isset($jsonChannel->height)) {
                 $siteChannel->setHeight($jsonChannel->height);
             }
             if (isset($jsonChannel->zone)) {
                 $zone = $siteLayoutZoneModel->findByJsonZone($siteLayout, $jsonChannel->zone);
                 if (!is_null($zone)) {
                     if ($zone->getIdSiteLayout() == $siteLayout->getId()) {
                         $siteChannel->setIdSiteLayoutZone($zone->getId());
                     }
                 }
             }
             $siteChannel->save();
         }
     }
     // In-Text
     if (isset($jsonLayout->channels) && is_array($jsonLayout->channels)) {
         // store site search channels:
         try {
             $siteLayoutIntextModel = new Sppc_Site_Layout_IntextModel();
             $siteLayoutIntextModel->delete(array('id_site_layout=?' => $siteLayout->getId()));
             foreach ($jsonLayout->channels as $key => $jsonChannel) {
                 if (!isset($jsonChannel->ad_type) || $jsonChannel->ad_type != 'intext') {
                     continue;
                 }
                 $siteLayoutIntext = $siteLayoutIntextModel->createRow();
                 $siteLayoutIntext->setIdSiteLayout($siteLayout->getId());
                 if (isset($jsonChannel->x)) {
                     $siteLayoutIntext->setX($jsonChannel->x);
                 }
                 if (isset($jsonChannel->y)) {
                     $siteLayoutIntext->setY($jsonChannel->y);
                 }
                 if (isset($jsonChannel->width)) {
                     $siteLayoutIntext->setWidth($jsonChannel->width);
                 }
                 if (isset($jsonChannel->height)) {
                     $siteLayoutIntext->setHeight($jsonChannel->height);
                 }
                 if (isset($siteLayoutIntext->zone)) {
                     $zone = $siteLayoutZoneModel->findByJsonZone($siteLayout, $jsonChannel->zone);
                     if (!is_null($zone)) {
                         if ($zone->getIdSiteLayout() == $siteLayout->getId()) {
                             $siteLayoutIntext->setIdSiteLayoutZone($zone->getId());
                         }
                     }
                 }
                 $siteLayoutIntext->save();
             }
         } catch (Exception $e) {
         }
     }
     return $siteLayout;
 }
 /**
  * Загрузка данных канала
  *
  * @param array $fields параметры создаваемого канала
  * @return string описание ошибки ('' при успешной загрузке)
  */
 public function _load($id)
 {
     try {
         $channelModel = new Sppc_ChannelModel();
         $channel = $channelModel->findObjectById($id);
         if (is_null($channel)) {
             throw new Sppc_Exception('Channel is not found');
         }
         $site = $channel->getParentSite();
         $tagModel = new Sppc_TagModel();
         $tag = $tagModel->findObjectById($id);
         $category = $channel->getCategory();
         if (null === $category) {
             $category = $site->getCategory();
         }
         $data = array('name' => $channel->getName(), 'description' => $channel->getDescription(), 'format' => $channel->getDimension()->getId(), 'ad_type_text' => $channel->isAdTypeAllowed(Sppc_Channel::AD_TYPE_TEXT) ? 'text' : '', 'ad_type_image' => $channel->isAdTypeAllowed(Sppc_Channel::AD_TYPE_IMAGE) ? 'image' : '', 'channel_type' => $channel->getChannelType(), 'ad_settings' => $channel->getAdSettings(), 'tag_code' => $tag->getCode(), 'blank_color' => '#' . $channel->getBlankColor(), 'category' => $category, 'ad_sources_advertisers' => $channel->hasAdSource(Sppc_Channel::AD_SOURCE_ADVERTISERS), 'ad_sources_xml_feeds' => $channel->hasAdSource(Sppc_Channel::AD_SOURCE_XMLFEEDS));
         return $data;
     } catch (Exception $e) {
         return __($e->getMessage());
     }
 }