/**
  * показывает форму для изменения сайта или его создания
  *
  * @param $id - идентификатор редактируемого сайта, если null - создание сайта
  * @return ничего не возвращает
  */
 public function index($id = null)
 {
     $current_site_info = null;
     $categories_tree = $this->category_model->get_html_tree();
     $id_channel = $this->input->post('id_channel');
     if (!empty($id)) {
         $siteModel = new Sppc_SiteModel();
         $this->_site = $siteModel->findObjectById($id);
         $mode = "modify";
         // Set Site layout
         try {
             if (is_null($this->_site)) {
                 throw new Exception('New site');
             }
             $layoutModel = new Sppc_Site_LayoutModel();
             $layout = $layoutModel->findBySite($this->_site);
             if (is_null($layout)) {
                 throw new Exception('Site without layout');
             }
             $this->_add_java_script_inline("var siteLayoutJson='{$layout->toString()}';", 'siteLayoutJson');
         } catch (Exception $e) {
             $this->_add_java_script_inline("var siteLayoutJson='';", 'siteLayoutJson');
         }
     } else {
         $this->_add_java_script_inline("var siteLayoutJson='';", 'siteLayoutJson');
     }
     if (is_null($this->_site) || $this->user_id != $this->_site->getIdEntityPublisher()) {
         $this->_site = null;
         $mode = "create";
     }
     $form = array("id" => is_null($this->_site) ? '' : $this->_site->getId(), "name" => "site_form", "view" => $this->views_paths['site_form'], "vars" => array('CATEGORIES_TREE' => $categories_tree, 'IMAGE' => $this->site->get_thumb($this->site_id) . "?ac=" . rand(100, 9999999), 'IMAGEPATH' => ltrim($this->config->item('path_to_images'), './')), "fields" => array("domain" => array("display_name" => __("Site URL"), "id_field_type" => "string", "form_field_type" => "text", "validation_rules" => "trim|required|hostname", "max" => 150), "title" => array("display_name" => "Title", "id_field_type" => "string", "form_field_type" => "text", "validation_rules" => "trim|required", "max" => 100), "description" => array("display_name" => __("Description"), "id_field_type" => "string", "form_field_type" => "textarea", "validation_rules" => "trim|required", "max" => 300), "category" => array("display_name" => __("Category"), "id_field_type" => "int", "form_field_type" => "hidden", "validation_rules" => "required"), 'thumb_id' => array('id_field_type' => 'string', 'form_field_type' => 'hidden', 'default' => 'DEFAULT'), 'layout_json' => array('id_field_type' => 'string', 'form_field_type' => 'text')));
     if ($this->input->post('redirect_after_save')) {
         $form['redirect'] = $this->input->post('redirect_after_save');
     }
     switch ($mode) {
         case 'create':
             $form['vars']['FORM_TITLE'] = "Create site";
             $form['vars']['CURRENT_SITE_NAME'] = '';
             $form['vars']['APPLY_BTN_CAPTION'] = "Create";
             if (!array_key_exists('redirect', $form)) {
                 $form['redirect'] = "/" . $this->role . "/edit_site/create_complete";
             }
             if ($id_channel) {
                 $this->load->model('channel');
                 $channel_info = $this->channel->get_info($id_channel);
                 $form['vars']['FORM_TITLE'] = sprintf(__("Create site and add channel %s to this site"), '<span class="green i">&bdquo;' . $channel_info->name . '&ldquo;</span>');
                 $form['fields']['id_channel'] = array("display_name" => "{@ID channel@}", "id_field_type" => "string", "form_field_type" => "hidden", "default" => $id_channel);
             }
             break;
         case 'modify':
             $form['vars']['FORM_TITLE'] = __("Edit site") . ':';
             $form['vars']['CURRENT_SITE_NAME'] = '&bdquo;' . type_to_str($this->_site->getName(), 'encode') . ' (' . $this->_site->getUrl() . ')&ldquo;';
             $form['vars']['APPLY_BTN_CAPTION'] = "Save";
             if (!array_key_exists('redirect', $form)) {
                 $form['redirect'] = "/" . $this->role . "/edit_site/edit_complete/" . type_to_str($this->_site->getId(), 'textcode');
             }
             break;
     }
     $this->_add_java_script('ajaxupload.3.9');
     $this->_set_content($this->form->get_form_content($mode, $form, $this->input, $this));
     $this->_display();
 }
 public function index($site_code = NULL)
 {
     $error_flag = false;
     $error_message = '';
     if (is_null($site_code)) {
         $error_flag = true;
         $error_message = __('Site is not found');
     }
     if (!$error_flag) {
         $id_site = type_cast($site_code, 'textcode');
         $this->load->model('site');
         $site_info = $this->site->get_list(array('fields' => 'sites.name, sites.url, SUM(stat_sites.impressions) as impressions, ' . 'UNIX_TIMESTAMP(sites.creation_date) as creation_date, site_categories.id_category as id_category, ' . 'sites.description', 'site_id_filter' => array($id_site), 'joinCategories' => true));
         try {
             if (is_null($site_info)) {
                 throw new Exception('Site was not found');
             }
             $siteModel = new Sppc_SiteModel();
             $site = $siteModel->findObjectById($id_site);
             if (is_null($site)) {
                 throw new Exception('Site was not found');
             }
             try {
                 $siteLayoutModel = new Sppc_Site_LayoutModel();
                 $siteLayout = $siteLayoutModel->findBySite($site);
                 if (is_null($siteLayout)) {
                     throw new Exception('layout is not found');
                 }
                 $jsonLayout = $siteLayout->toString();
                 //die($jsonLayout);
                 //$this->_add_java_script_inline( "var siteLayoutJson='{$jsonLayout}';" );
                 $this->content['JSCODE'] = "siteLayoutJson='{$jsonLayout}';";
             } catch (Exception $e) {
                 //$this->_add_java_script_inline( "var siteLayoutJson='';" );
                 $this->content['JSCODE'] = "siteLayoutJson='';";
             }
         } catch (Exception $e) {
             $error_flag = true;
             $error_message = __('Site is not found');
         }
     }
     if ($error_flag) {
         $data = array('MESSAGE' => __($error_message), 'REDIRECT' => $this->site_url . $this->index_page . $this->role . '/site_directory');
         $content = $this->parser->parse('common/errorbox.html', $data, FALSE);
         $this->_set_content($content);
         $this->_display();
         return;
     }
     $site_info = $site_info[0];
     $this->load->model('category_model', 'category');
     $category_list = $this->category->get_list_by_site($id_site);
     $this->load->model('channel');
     $channels_list = $this->channel->get_list(array('fields' => 'channels.id_channel as channel_code, channels.name as channel_name', 'show_deleted_in_site_channels' => false, 'hide_wo_programs' => true, 'site_id_filter' => $id_site));
     if (is_null($channels_list)) {
         $channels_list = array();
     }
     foreach ($channels_list as &$channel) {
         $channel['id_channel'] = $channel['channel_code'];
         $channel['channel_code'] = type_to_str($channel['channel_code'], 'textcode');
     }
     $channels_list = array_merge($channels_list, $this->plugins->run('get_channels', $this));
     $rank_script = "http://xslt.alexa.com/site_stats/js/s/a?url={$site_info['url']}";
     $vars = array('SITE_VIEWS_COUNT' => type_to_str($site_info['impressions'], 'impressions'), 'SITE_CREATION_DATE' => type_to_str($site_info['creation_date'], 'date'), 'SITE_CATEGORY' => implode(', ', $category_list), 'SITE_DESCRIPTION' => nl2br(type_to_str($site_info['description'], 'encode')), 'SITE_DESTINATION_URL' => $site_info['url'], 'SITE_TITLE' => type_to_str($site_info['name'], 'encode'), 'SITE_CODE' => $site_code, 'SITE_CHANNELS_LIST' => json_encode($channels_list), 'IMAGE' => $this->site->get_thumb($id_site), 'SITE_ALEXA_RANK' => $rank_script, 'ROLE' => $this->role);
     $this->_set_content($this->parser->parse("common/site_directory/site_information/template.html", $vars, TRUE));
     $this->_display();
 }
 public function _load($idSite)
 {
     $siteModel = new Sppc_SiteModel();
     $site = $siteModel->findObjectById($idSite);
     $siteLayoutModel = new Sppc_Site_LayoutModel();
     $siteLayout = $siteLayoutModel->findBySite($site);
     return array('layout_json' => $siteLayout->toString());
 }