/**
  * Create a duplicate. 
  *
  * @param integer $customWriteModuleId
  * @param string $duplicate_name the name of the duplicate, if false,
  *           the name "[MODULE NAME] copy [x] will be given to the duplicate. 
  */
 function Create($customWriteModuleId, $duplicate_name = false)
 {
     global $wpdb;
     // Get module name
     $customModule = RCCWP_CustomWriteModule::Get($customWriteModuleId);
     if (!$duplicate_name) {
         // go ahead and rename, then duplicate
         $duplicate_name = $customModule->name;
         if ($other_blocks = $wpdb->get_results("SELECT duplicate_name FROM " . FLUTTER_TABLE_MODULES_DUPLICATES . " WHERE duplicate_name LIKE '" . preg_replace('/\\scopy\\s[0-9]*/', '', $duplicate_name) . " %' ORDER BY duplicate_id DESC")) {
             $duplicate_name = $other_blocks[0]->duplicate_name;
             $testcase = substr($duplicate_name, -1, 1);
             $duplicate_name[strlen($duplicate_name) - 1] = intval($testcase) + 1;
         } else {
             $duplicate_name .= ' copy 2';
         }
     }
     $wpdb->query("INSERT INTO " . FLUTTER_TABLE_MODULES_DUPLICATES . " (module_id, duplicate_name) VALUES ({$customWriteModuleId}, '{$duplicate_name}')");
     FlutterLayoutBlock::UpdateAllModulesSettings();
     return $wpdb->insert_id;
 }
    /**
     * Create a new module
     *
     * @param string $name module name, the name will be sanitized.
     * @param string $description module description
     * @param boolean $create_folders whether to create a folder for the module containing sample template
     * @return the id of the module or -1 if the module name already exist
     */
    function Create($name, $description, $create_folders = true)
    {
        require_once 'RC_Format.php';
        global $wpdb;
        $special_chars = array(' ', '`', '"', '\'', '\\', '/', " ", "#", "\$", "%", "^", "&", "*", "!", "~", "‘", "\"", "’", "'", "=", "?", "/", "[", "]", "(", ")", "|", "<", ">", ";", "\\", ",");
        $name = str_replace($special_chars, '', $name);
        //Make sure the module doesn't already exist
        $sql = "SELECT * FROM " . RC_CWP_TABLE_MODULES . " WHERE name = '" . $name . "'";
        if ($wpdb->get_row($sql)) {
            return -1;
        }
        $sql = sprintf("INSERT INTO " . RC_CWP_TABLE_MODULES . " (name, description)" . " values" . " (%s, %s)", RC_Format::TextToSql($name), RC_Format::TextToSql($description));
        $wpdb->query($sql);
        FlutterLayoutBlock::UpdateAllModulesSettings();
        $customWriteModuleId = $wpdb->insert_id;
        if ($create_folders == false) {
            return $customWriteModuleId;
        }
        // Create template folder
        $moduleTemplateFolder = FLUTTER_MODULES_DIR . $name;
        if (@mkdir($moduleTemplateFolder, 0777)) {
            @chmod($moduleTemplateFolder, 0777);
            if (mkdir($moduleTemplateFolder . '/templates', 0777)) {
                @chmod($moduleTemplateFolder . '/templates', 0777);
                if (mkdir($moduleTemplateFolder . '/templates/default', 0777)) {
                    @chmod($moduleTemplateFolder . '/templates/default', 0777);
                    RCCWP_CustomWriteModule::CreateTemplateFiles($moduleTemplateFolder . '/templates/default/small', $name);
                }
            }
            //Create basic configuration file
            $configurationFileContent = <<<EOF
\t<?xml version="1.0"?>
\t<canvas>
\t\t<function>
\t\t\t<author>
\t\t\tFlutter
\t\t\t</author>
\t\t\t<description>
\t\t\tThis is module {$name}.
\t\t\t</description>
\t\t\t<variables>
\t\t\t\t<variable>
\t\t\t\t\t<name>
\t\t\t\t\tthe_text
\t\t\t\t\t</name>
\t\t\t\t\t<description>
\t\t\t\t\tText to display (HTML allowed)
\t\t\t\t\t</description>
\t\t\t\t\t<type>
\t\t\t\t\tTextarea
\t\t\t\t\t</type>
\t\t\t\t\t<default>
\t\t\t\t\t<![CDATA[<p>This is some text generated by {$name} module. </p>]]>
\t\t\t\t\t</default>
\t\t\t\t</variable>
\t\t\t</variables>
\t\t</function>
\t</canvas>
EOF;
            file_put_contents($moduleTemplateFolder . "/configure.xml", $configurationFileContent);
            @chmod($moduleTemplateFolder . "/configure.xml", 0666);
        }
        return $customWriteModuleId;
    }
 /**
  * Import default modules/panels in the theme if it is the first time to 
  * activate the theme and add the theme settings to the database.
  *
  */
 function ImportNewTheme($themeName)
 {
     // -- Add/update theme settings to the database
     FlutterLayoutBlock::UpdateModuleSettings(get_template_directory() . '/configure.xml', -1, '-', get_option('template'));
     FlutterLayoutBlock::UpdateAllModulesSettings();
     // -- Import modules and panels
     if (RCCWP_Application::IsWordpressMu()) {
         if (get_site_option('Flutter_theme_ft_' . $themeName) == '1') {
             return;
         }
         update_site_option('Flutter_theme_ft_' . $themeName, '1');
     } else {
         if (get_option('Flutter_theme_ft_' . $themeName) == '1') {
             return;
         }
         update_option('Flutter_theme_ft_' . $themeName, '1');
     }
     $modulesFolder = get_template_directory() . '/Flutter_modules/';
     $panelsFolder = get_template_directory() . '/Flutter_panels/';
     // Import modules
     if ($handle = @opendir($modulesFolder)) {
         while (false !== ($file = readdir($handle))) {
             $filePath = $modulesFolder . $file;
             RCCWP_CustomWriteModule::Import($filePath);
         }
     }
     // Import panels
     if ($handle = @opendir($panelsFolder)) {
         while (false !== ($file = readdir($handle))) {
             $filePath = $panelsFolder . $file;
             RCCWP_CustomWritePanel::Import($filePath);
         }
     }
 }