/**
  * _get_sub_type function.
  * 
  * @access private
  * @param string $type (default: '')
  * @param mixed $allow_edit (default: FALSE)
  * @return void
  */
 function _get_sub_type($type = '', $allow_edit = FALSE)
 {
     $str = '';
     $this->vars['product_search'] = $this->ajax_url . $this->EE->cp->fetch_action_id('Brilliant_retail_mcp', 'product_search');
     // File upload options for new or download products only
     if ($type == '' || $type == 4) {
         // Its downloadable or new! Lets try to get some uploaded files
         // if they are available we need to get a list of possible
         $this->vars["has_import"] = FALSE;
         $this->vars["imports"] = array();
         if ($this->_config["store"][$this->site_id]["downloads_use_local"]) {
             $fl = read_dir_files($this->_config["media_dir"] . 'import');
             if (count($fl) > 0) {
                 $this->vars["has_import"] = TRUE;
                 foreach ($fl as $f) {
                     if (!is_dir($this->_config["media_dir"] . 'import/' . $f)) {
                         $this->vars["imports"][] = $f;
                     }
                 }
             }
         }
         // Do we allow S3 browsing of files?
         $this->vars["has_s3"] = FALSE;
         $this->vars["s3_buckets"] = array();
         $this->vars["s3_get_files"] = '';
         if ($this->_config["store"][$this->site_id]["downloads_use_s3"]) {
             $this->vars["has_s3"] = TRUE;
             // Get the buckets
             $this->EE->load->library('aws');
             $this->EE->aws->AWSAccessKeyId = $this->_config["store"][$this->site_id]["downlaods_s3_access_key"];
             $this->EE->aws->AWSSecretKey = $this->_config["store"][$this->site_id]["downlaods_s3_secret_key"];
             $this->vars["s3_buckets"] = $this->EE->aws->listBuckets();
             $this->vars["s3_get_files"] = $this->EE->functions->fetch_site_index() . QUERY_MARKER . 'ACT=' . $this->EE->cp->fetch_action_id('Brilliant_retail_mcp', 's3_get_files');
         }
     }
     // New products
     if ($type != '') {
         $file = array(2 => 'bundle', 3 => 'configurable', 4 => 'downloadable', 7 => 'donation');
         $str = '';
         // Show all options for edit.
         if ($allow_edit == TRUE) {
             foreach ($file as $f) {
                 $str .= $this->_view('product/sub_types/' . $f, $this->vars);
             }
         } else {
             // Only show the type if it exists
             if (isset($file[$type])) {
                 $str .= $this->_view('product/sub_types/' . $file[$type], $this->vars);
             }
         }
         $str .= "\t<script type=\"text/javascript\">\n\t\t\t\t\t\t\t\t\t\$(function(){\n\t\t\t\t\t\t\t\t\t\t\$('#sub_type_" . $type . "').show();\n\t\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t\t</script>";
     } else {
         $a["bundle"] = $this->_view('product/sub_types/bundle', $this->vars);
         $a["configurable"] = $this->_view('product/sub_types/configurable', $this->vars);
         $a["downloadable"] = $this->_view('product/sub_types/downloadable', $this->vars);
         $a["donation"] = $this->_view('product/sub_types/donation', $this->vars);
         $str = $a["bundle"] . $a["configurable"] . $a["downloadable"] . $a["donation"];
     }
     $str .= "\t<script type=\"text/javascript\">\n\t\t\t\t\t\t\t\t\$(function(){\n\t\t\t\t\t\t\t\t\t\$('#type_id').bind('change',function(){\n\t\t\t\t\t\t\t\t\t\tvar a = \$(this).val();\n\t\t\t\t\t\t\t\t\t\t\$('.subtypes').hide();\n\t\t\t\t\t\t\t\t\t\t\$('#sub_type_'+a).show();\n\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t// First we set reset all requireds to 1\n\t\t\t\t\t\t\t\t\t\t\t\$('.sub_type_req').val(1);\n\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t// Then we set the selected required to empty. \n\t\t\t\t\t\t\t\t\t\t\t\$('#sub_type_req_'+a).val('');\n\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t</script>";
     return $str;
 }
 function _init_snippets()
 {
     $path = PATH_THIRD . 'brilliant_retail/core/snippets';
     $this->EE->load->helper('file');
     $files = read_dir_files($path);
     $this->EE->load->model('snippet_model');
     $list_snippets = $this->EE->snippet_model->get_snippets_by_site_id($this->site_id);
     $snippents = array();
     foreach ($files as $f) {
         // skip hidden files
         if (substr($f, 0, 1) == '.') {
             continue;
         }
         // proceed
         $nm = substr($f, 0, -5);
         if (isset($list_snippets[$nm])) {
             $snippents[$nm] = $list_snippets[$nm];
         } else {
             $this->EE->load->helper('file');
             $s = read_file(rtrim($path, "/") . "/" . $f);
             $data = array('site_id' => $this->EE->config->item('site_id'), 'snippet_name' => $nm, 'snippet_contents' => $s);
             $data["snippet_id"] = $this->EE->snippet_model->create_snippet($data);
             $list_snippets[$nm] = $data;
         }
     }
     // Send all templates to the view
     $this->vars["snippets"] = $list_snippets;
 }
function read_system_files($type)
{
    // List Core Files
    $dir = PATH_THIRD . 'brilliant_retail/core/' . $type;
    $files = read_dir_files($dir);
    // List Local Files
    $local_dir = PATH_THIRD . '_local/brilliant_retail/' . $type;
    $local = read_dir_files($local_dir);
    // Merge
    foreach ($files as $f) {
        if (substr($f, 0, strlen($type) + 1) == $type . '.') {
            // Whats the module name based on the
            // file naming convention
            $rem = array($type . '.', '.php');
            $nm = strtolower(str_replace($rem, '', $f));
            $list[$f]['path'] = $dir . '/' . $f;
            $list[$f]['code'] = $nm;
            $list[$f]['type'] = 'core';
        }
    }
    foreach ($local as $loc) {
        if (substr($loc, 0, strlen($type) + 1) == $type . '.') {
            if (isset($files[$loc])) {
                unset($files[$loc]);
            }
            $rem = array($type . '.', '.php');
            $nm = strtolower(str_replace($rem, '', $loc));
            $list[$loc]['path'] = $local_dir . '/' . $loc;
            $list[$loc]['code'] = $nm;
            $list[$loc]['type'] = 'local';
        }
    }
    sort($list);
    return $list;
}