Example #1
0
function processStep4($option)
{
    global $database;
    //Get hidden data
    $separator = mosGetParam($_REQUEST, 'separator', ',');
    $convertUTF8 = mosGetParam($_REQUEST, 'csv_utf8', 0);
    //fault here
    $fileName = mosGetParam($_REQUEST, 'filename', '');
    $columns = mosGetParam($_REQUEST, 'columns', null);
    $fields = mosGetParam($_REQUEST, 'fields', null);
    $globalColumns = mosGetParam($_REQUEST, 'global_columns', null);
    $globalValues = mosGetParam($_REQUEST, 'global_value', null);
    //Get global setting
    $authorId = mosGetParam($_REQUEST, 'author_id', 0);
    $sectionId = mosGetParam($_REQUEST, 'section_id', 0);
    $categoryId = mosGetParam($_REQUEST, 'category_id', 0);
    $published = mosGetParam($_REQUEST, 'published', 0);
    $metaKeys = mosGetParam($_REQUEST, 'meta_keys', '');
    $metaDes = mosGetParam($_REQUEST, 'meta_des', '');
    $createdDate = mosGetParam($_REQUEST, 'created_date', '');
    $publishUpDate = mosGetParam($_REQUEST, 'publish_up_date', '');
    $publishDownDate = mosGetParam($_REQUEST, 'publish_down_date', '');
    $access = mosGetParam($_REQUEST, 'access', 0);
    $state = mosGetParam($_REQUEST, 'state', 0);
    //Import data now, go to final step
    $rowContent = new mosContent($database);
    //Get all data
    $arrContentFieldName = array();
    $arrContentFieldSTT = array();
    $arrJreviewFieldName = array();
    $arrJreviewFieldSTT = array();
    $totalColumn = count($columns);
    //Mark the ignore field lists
    $arrIgnores = array();
    for ($i = 0; $i < $totalColumn; $i++) {
        $column = $columns[$i];
        $field = $fields[$i];
        $arrField = explode(".", $field);
        $tableName = $arrField[0];
        $fieldName = $arrField[1];
        if ($fieldName != 'ignore') {
            if ($tableName == "jos_content") {
                $arrContentFieldName[$i] = $fieldName;
                $arrContentFieldSTT[] = $i;
            } else {
                $arrJreviewFieldName[$i] = $fieldName;
                $arrJreviewFieldSTT[] = $i;
            }
        } else {
            $arrIgnores[] = $i;
        }
    }
    //Set static content vaiable
    $arrGlobalSetting = array();
    $arrGlobalSetting["created_by"] = $authorId;
    $arrGlobalSetting["sectionid"] = $sectionId;
    $arrGlobalSetting["catid"] = $categoryId;
    $arrGlobalSetting["published"] = $published;
    $arrGlobalSetting["metakey"] = $metaKeys;
    $arrGlobalSetting["metadesc"] = $metaDes;
    $arrGlobalSetting["created"] = $createdDate;
    $arrGlobalSetting["publish_up"] = $publishUpDate;
    $arrGlobalSetting["publish_down"] = $publishDownDate;
    $arrGlobalSetting["access"] = $access;
    $arrGlobalSetting["state"] = $state;
    //CustomGlobla Varaible
    $totalGlobalFields = count($globalColumns);
    $arrJreviewSettings = array();
    for ($i = 0; $i < $totalGlobalFields; $i++) {
        $column = $globalColumns[$i];
        $value = $globalValues[$i];
        $arrField = explode(".", $column);
        $field = $arrField[1];
        $arrJreviewSettings[$field] = $value;
    }
    //Attributes
    $params = mosGetParam($_POST, 'params', '');
    if (is_array($params)) {
        $txt = array();
        foreach ($params as $k => $v) {
            if (get_magic_quotes_gpc()) {
                $v = stripslashes($v);
            }
            $txt[] = "{$k}={$v}";
        }
        $attribs = implode("\n", $txt);
    }
    //Read the file here
    //Open File and read the correlative data
    //cell separator, row separator, value enclosure
    $csv = new CSV($separator, "\r\n", '"');
    //parse the string content
    $csv->setContent(file_get_contents(PATH_ROOT . "images/csv_import/{$fileName}"));
    //returns an array with the CSV data
    if ($convertUTF8) {
        $csvArray = utf8_encode_mix($csv->getArray());
    } else {
        $csvArray = $csv->getArray();
    }
    //Read the header
    $line = 1;
    $arrErrorLine = array();
    $arrErrorMessage = array();
    $totalImported = 0;
    $images = mosGetParam($_REQUEST, 'images', '');
    while (false != ($cells = next($csvArray))) {
        $totalCells = count($cells);
        //Check all the cell
        $continue = false;
        for ($k = 0; $k < count($cells); $k++) {
            if ($cells[$k]) {
                $continue = true;
                break;
            }
        }
        if ($continue) {
            if ($line > 0) {
                $success = true;
                $errorLine = 0;
                $errMsg = "";
                $arrContentData = array();
                $arrJreviewData = array();
                for ($i = 0; $i < $totalCells; $i++) {
                    $cell = $cells[$i];
                    if (!in_array($i, $arrIgnores)) {
                        if (in_array($i, $arrContentFieldSTT)) {
                            $contentField = $arrContentFieldName[$i];
                            $arrContentData[$contentField] = convert_characters($cell);
                        } else {
                            $jReviewField = $arrJreviewFieldName[$i];
                            $arrJreviewData[$jReviewField] = convert_characters($cell);
                        }
                    }
                }
                $copyArrGlobalSetting = $arrGlobalSetting;
                // Overwrite global settings with CSV fields - added v1.0.9
                $csvColumns = array('catid', 'sectionid', 'created_by', 'metakey', 'metadesc', 'publish_up', 'publish_down', 'images');
                foreach ($csvColumns as $csvColumn) {
                    if (array_key_exists($csvColumn, $arrContentData) && $arrContentData[$csvColumn] != '') {
                        unset($copyArrGlobalSetting[$csvColumn]);
                    } elseif (array_key_exists($csvColumn, $arrContentData)) {
                        unset($arrContentData[$csvColumn]);
                    }
                }
                $arrContentData = array_merge($arrContentData, $copyArrGlobalSetting);
                $arrJreviewData = array_merge($arrJreviewData, $arrJreviewSettings);
                //Save data to correlative table
                $rowContent = new mosContent($database);
                if (!$rowContent->bind($arrContentData, 'id')) {
                    $success = false;
                    $errMsg .= " " . $rowContent->getError();
                }
                $rowContent->id = 0;
                $rowContent->attribs = $attribs;
                if ($rowContent->images == '') {
                    $rowContent->images = $images;
                }
                if (!$rowContent->store()) {
                    $success = false;
                    $errMsg .= " " . $rowContent->getError();
                }
                if ($success && _JREVIEWS_INSTALLED) {
                    $contentId = $rowContent->id;
                    $arrJreviewData["contentid"] = $contentId;
                    //Build query to insert into content table
                    $err = insertObject("#__jreviews_content", $arrJreviewData);
                    if ($err) {
                        $success = false;
                        $errMsg .= " " . $err;
                        $sql = "Delete From #__content Where id={$rowContent->id}";
                        $database->setQuery($sql);
                        $database->query();
                    }
                }
                if (!$success) {
                    $arrErrorLine[] = $line + 1;
                    $arrErrorMessage[] = $errMsg;
                } else {
                    $totalImported++;
                }
            }
        } else {
            break;
        }
        $line++;
    }
    //Save profiler
    $profileName = mosGetParam($_REQUEST, 'profile_name');
    if ($profileName) {
        $rowProfiler = new mosCSVProfiler($database);
        if (!$rowProfiler->bind($_POST)) {
            echo "<script> alert('" . $rowProfiler->getError() . "'); window.history.go(-1); </script>\n";
            exit;
        }
        $rowProfiler->name = $profileName;
        $rowProfiler->number_run = 1;
        $rowProfiler->last_run = date("Y-m-d");
        $rowProfiler->created = $rowProfiler->last_run;
        if (!$rowProfiler->store()) {
            echo "<script> alert('" . $rowProfiler->getError() . "'); window.history.go(-1); </script>\n";
            exit;
        }
        //Insert data about other fields mapping
        $profilerId = $rowProfiler->id;
        for ($i = 0, $n = count($columns); $i < $n; $i++) {
            $column = $columns[$i];
            $field = $fields[$i];
            $columnNo = $i + 1;
            $sql = "Insert Into \r\n\t\t\t\t #__im_fields(\r\n\t\t\t\t profiler_id,\r\n\t\t\t\t column_no,\r\n\t\t\t\t column_title,\r\n\t\t\t\t `field`)\r\n\t\t\t\t Values(\r\n\t\t\t\t {$profilerId},\r\n\t\t\t\t {$columnNo},\r\n\t\t\t\t '{$column}',\r\n\t\t\t\t '{$field}'\t\t\t\t \r\n\t\t\t\t )\r\n\t\t\t\t";
            $database->setQuery($sql);
            if (!$database->query()) {
                echo "<script> alert('" . $database->getError() . "'); window.history.go(-1); </script>\n";
                exit;
            }
        }
        //Insert into global setting tables
        for ($i = 0, $n = count($globalColumns); $i < $n; $i++) {
            $column = $globalColumns[$i];
            $value = $globalValues[$i];
            $sql = "Insert Into \r\n\t\t\t\t #__im_global(\r\n\t\t\t\t profiler_id,\r\n\t\t\t\t field_name,\r\n\t\t\t\t field_value\r\n\t\t\t\t )\r\n\t\t\t\t Values(\r\n\t\t\t\t {$profilerId},\t\t\t\t \r\n\t\t\t\t '{$column}',\r\n\t\t\t\t '{$value}'\t\t\t\t \r\n\t\t\t\t )\r\n\t\t\t\t";
            $database->setQuery($sql);
            if (!$database->query()) {
                echo "<script> alert('" . $database->getError() . "'); window.history.go(-1); </script>\n";
                exit;
            }
        }
    }
    //Save profile here
    HTML_csv_import::showImportResult($option, $totalImported, $arrErrorLine, $arrErrorMessage, $sectionId, $categoryId);
}