echo count($selproducts); ?> )</h3> </td> </tr> <?php $count = 1; foreach ($selproducts as $key => $val) { ?> <tr> <td> <div class="ftext"> <?php $Breakfast = new Product(); $BreakfastAttributes = $Breakfast->attributeLabels(); // e($BreakfastAttributes); echo "<h4>Product -" . $count . " </h4>"; foreach ($val as $vkey => $vval) { if (in_array($vkey, array('name', 'desc', 'pic')) && !empty($vval)) { if ($vkey == 'pic') { $url = getSiteUrl() . '/productimg/' . $vval; echo ' <div style="margin-top:10px;"><img width="60%" src=' . $url . '></div>'; } else { echo ' <div style=""><span style="color:#0069A4;">' . $BreakfastAttributes[$vkey] . '</span> : ' . $vval . "</div>"; } } } $count++; ?> <!--</div>-->
public function actionImport() { $model = new ImportForm(); if (isset($_GET['examplefile'])) { $product = new Product(); $importFields = $product->attributeLabels(); $value = new ProductValue(); foreach ($value->getFields() as $field) { $importFields[$field->varname] = $field->title; } if ($_GET['examplefile'] == 'xml') { //header('Content-Disposition: attachment; filename="examplefile.xml"'); } else { header('Content-Disposition: attachment; filename="examplefile.csv"'); $content = implode(';', array_values($importFields)) . "\n"; $content .= implode(';', array_keys($importFields)) . "\n"; if (isset(Yii::app()->getModule('cart')->importCSVCharset) && Yii::app()->getModule('cart')->importCSVCharset != 'UTF-8') { $content = iconv("UTF-8", Yii::app()->getModule('cart')->importCSVCharset, $content); } echo $content; } Yii::app()->end(); } if (isset($_POST['ajax'])) { $session = Yii::app()->session[self::IMPORT_SESSION_NAME]; if ($_POST['line'] < $session['count']) { // Import product //echo '<pre>'; print_r(array($session,$_POST)); die(); $content = explode("\n", file_get_contents($session['filename'])); if (isset($content[$_POST['line']])) { echo CJSON::encode($this->productImport(CJSON::decode($content[$_POST['line']]))); } else { echo CJSON::encode(array('message' => $session['filename'] . ' - Error data line ' . $_POST['line'])); } } else { echo CJSON::encode(array('message' => 'Finished!')); } //sleep(1); Yii::app()->end(); //*/ } if (isset($_POST['ImportForm'])) { $model->attributes = $_POST['ImportForm']; if ($model->validate()) { $model->importFile = CUploadedFile::getInstance($model, 'importFile'); $content = file_get_contents($model->importFile->tempName); if (isset(Yii::app()->getModule('cart')->importCSVCharset) && Yii::app()->getModule('cart')->importCSVCharset != 'UTF-8') { $content = iconv(Yii::app()->getModule('cart')->importCSVCharset, "UTF-8", $content); } $fields = array(); $array = array(); $content = explode("\n", $content); $fields = explode(';', $content[1]); foreach ($content as $i => $line) { $line = trim($line); if ($line && $i > 1) { $line = explode(';', $line); $item = array(); foreach ($fields as $n => $field) { $item[trim($field)] = trim($line[$n]); } if ($item) { $item = CJSON::encode($item); array_push($array, $item); } } } $session = array('line' => 0, 'count' => count($array), 'filename' => Yii::app()->getModule('cart')->importPathPhotos . '/yii-cart-import_' . date('Y-m-d_H-i-s') . '.import'); if ($fh = fopen($session['filename'], 'w+')) { fwrite($fh, implode("\n", $array)); fclose($fh); Yii::app()->session[self::IMPORT_SESSION_NAME] = $session; Yii::app()->user->setFlash('importMessage', CartModule::t('Import {count} products', 'import', array('{count}' => count($array)))); $cs = Yii::app()->getClientScript(); $cs->registerCoreScript('jquery'); $css = ' /* progress bar */ #progressbar { margin: 10px 0; padding:4px; border:1px solid #6FACCF; position:relative; -moz-border-radius: 5px;-webkit-border-radius:5px; } #progressbar #percent { position:absolute; left:0; right:0; text-align:center; color:#0066A4; font-weight:bold; } #progressbar #progress { background: #EFFDFF; border:1px solid #B7D6E7; margin:-1px; -moz-border-radius: 3px;-webkit-border-radius:3px; } #log { position:relative; overflow: auto; max-height: 300px; } #log .logline { position:absolute; padding: 5px; border: 1px solid #fff; -moz-border-radius: 5px;-webkit-border-radius:5px; } #log .logline:hover { border: 1px solid #B7D6E7 } #log .tohide { color:#298DCD;/*#B7D6E7*/ } #log .error {color:#f00;} #log .addnew {color:#298DCD;} #log span.warning {color:#f00;} #log span.note {color:#ff9900;} '; $js = ' $(document).ready(function(){ $("#showlog").click(function(){ $("#log div.logline").attr("style","position:relative;display:none;").show(500); $(this).hide(); $("#hidelog").show(); }); $("#hidelog").click(function(){ $("#log div.logline").hide(500); $(this).hide(); $("#showlog").show(); }); var i = 0; var size = ' . count($array) . '; getData(); function getData() { var AjaxError = false; $.ajax({ async: false, type: \'POST\', data: ({ajax : 1,line: i}), dataType:\'json\', url:\'' . $this->createUrl('product/import') . '\', success: function(data) { if (data!=null) { addLogLine(data.message,data.error,data.warning,data.note,data.new); i++; setProgress(i,size); } else { addLogLine("{url: ' . $this->createUrl('product/import') . ', data: {ajax : 1,line: "+i+"}, return: null}"); addLogLine("' . CartModule::t('Import error!\\nPlease contact your administrator!', 'import') . '"); AjaxError = true; } }, error: function() { addLogLine("{url: ' . $this->createUrl('product/import') . ', data: {ajax : 1,line: "+i+"}}"); addLogLine("' . CartModule::t('Import error!\\nPlease contact your administrator!', 'import') . '"); AjaxError = true; }, }); if (AjaxError) { alert("' . CartModule::t('Import error!\\nPlease contact your administrator!', 'import') . '"); $("#showlog").show(); } else { if (i<size) { ' . (Yii::app()->getModule('cart')->importAjaxSleep ? 'sleep(' . Yii::app()->getModule('cart')->importAjaxSleep . ');' : '') . ' getData(); } else { $("#showlog").show(); } } } function setProgress(n,c) { var p = parseInt(100/c*n); $("#percent").text(p+" %"); //$("#progress").animate({width: p+"%"}); $("#progress").width(p+"%"); } function addLogLine(line,er,wr,nt,nw) { $("#log div.last").removeClass("last").addClass("logline").hide(); $("#log").append("<div class=\\"last "+((nw)?"addnew":"update")+((er)?" error":"")+"\\">"+((wr)?"<span class=\\"warning\\">"+wr+"</span><br/>":"")+((nt)?"<span class=\\"note\\">"+nt+"</span><br/>":"")+line+"</div>"); } function sleep(milliseconds) { var start = new Date().getTime(); for (var i = 0; i < 1e7; i++) { if ((new Date().getTime() - start) > milliseconds){ break; } } } }); '; $cs->registerCss(__CLASS__ . '#form', $css); $cs->registerScript(__CLASS__ . '#form', $js); } else { Yii::app()->user->setFlash('importError', CartModule::t('Failed to create file: {filename}', 'import', array('{filename}' => $session['filename']))); } } } $this->render('importform', array('model' => $model)); }