コード例 #1
0
/**
 * import the csv firstname file
 *
 * @param string $targetPath filepath
 * @param int    $start      start from
 * @param int    $count      step of import
 *
 * @return void
 */
function importFile($targetPath, $start, $count)
{
    $fp = fopen($targetPath, 'r');
    //0 = first line
    if ($start == 0) {
        $start++;
    }
    $line_nb = 0;
    while ($line = fgetcsv($fp, null, ";")) {
        if ($line_nb >= $start && $line_nb < $start + $count) {
            $found = false;
            $fn = CMbString::removeDiacritics(trim($line[0]));
            $sex = trim($line[1]);
            $language = CMbString::removeDiacritics(trim($line[2]));
            if ($sex == "m,f" || $sex == "f,m") {
                $sex = "u";
            }
            $firstname = new CFirstNameAssociativeSex();
            $firstname->firstname = $fn;
            $firstname->language = $language;
            $firstname->loadMatchingObjectEsc();
            if ($firstname->_id) {
                // found
                $found = true;
                if ($sex != $firstname->sex) {
                    $firstname->sex = "u";
                }
            } else {
                // not found
                $firstname->sex = $sex;
            }
            // store & message
            if ($msg = $firstname->store()) {
                CAppUI::stepAjax($msg, UI_MSG_WARNING);
            } else {
                if ($found == true) {
                    CAppUI::stepAjax("prénom <strong>{$fn}</strong>, mis à jour <strong>[{$firstname->sex}]</strong>");
                } else {
                    CAppUI::stepAjax("prénom <strong>{$fn}</strong>, ajouté <strong>[{$firstname->sex}]</strong>");
                }
            }
        }
        $line_nb++;
    }
    return;
}