Microweber\Utils\Import::batch_process PHP Method

batch_process() public method

public batch_process ( $content_items = false )
    public function batch_process($content_items = false)
    {
        // \DB::connection()->enableQueryLog();
        //        \DB::enableQueryLog();
        //        \Event::listen('illuminate.query', function($sql)
        //        {
        //            \Log::error($sql);
        //        });
        $chunks_folder = $this->get_import_location() . '_process_import' . DS;
        $index_file = $chunks_folder . 'index.php';
        if (!is_dir($chunks_folder)) {
            mkdir_recursive($chunks_folder);
        }
        $total = 0;
        $remaining = 0;
        $batch_file = false;
        // $save_log_info = $this->log();
        if (is_array($content_items) and isset($content_items['process'])) {
            $content_items = false;
        } elseif (is_array($content_items) and isset($content_items['cancel'])) {
            rmdir_recursive($chunks_folder);
            return;
        }
        if (!is_array($content_items) or empty($content_items)) {
            $content_items = array();
            if (is_file($index_file)) {
                $total = file_get_contents($index_file);
            }
            if ($total == 0) {
                $total = 0;
                $dir = $chunks_folder;
                if ($handle = opendir($dir)) {
                    while (($file = readdir($handle)) !== false) {
                        if (!in_array($file, array('.', '..')) && !is_dir($dir . $file) and strstr($file, 'import_chunk_')) {
                            ++$total;
                        }
                    }
                }
                $save_log_info['total'] = $total;
                //  $this->log($save_log_info);
                file_put_contents($index_file, $total);
            }
            $i = 0;
            $dir = $chunks_folder;
            $rem_counter = 0;
            $process_xml_files = array();
            $chunk_size = $this->batch_size;
            if ($handle = opendir($dir)) {
                while (($file = readdir($handle)) !== false) {
                    if (!in_array($file, array('.', '..')) && !is_dir($dir . $file) and strstr($file, 'import_chunk_')) {
                        //if (!is_array($content_items)) {
                        if ($i < $chunk_size) {
                            $batch_file = $chunks_folder . $file;
                            $batch_file_content = file_get_contents($batch_file);
                            if (strstr($file, 'import_chunk_xml')) {
                                // for ($x=0; $x<=10; $x++){
                                $content_from_xml = $this->parse_content_from_xml_string($batch_file_content);
                                if (is_array($content_from_xml)) {
                                    foreach ($content_from_xml as $content_from_x) {
                                        $content_items[] = $content_from_x;
                                    }
                                    // $rem_counter--;
                                }
                                //}
                            } else {
                                $content_items_from_file = @unserialize($batch_file_content);
                                if (!empty($content_items_from_file)) {
                                    foreach ($content_items_from_file as $content_from_x) {
                                        $content_items[] = $content_from_x;
                                    }
                                }
                            }
                            if ($batch_file != false and is_file($batch_file)) {
                                @unlink($batch_file);
                            }
                        }
                        ++$i;
                    }
                }
                $remaining = $i;
                $save_log_info['remaining'] = $remaining;
                //  $this->log($save_log_info);
            }
        } else {
            $total = count($content_items);
            $save_log_info['total'] = $total;
            //  $this->log($save_log_info);
        }
        // dd($content_items);
        if ($content_items != false and is_array($content_items)) {
            if (!empty($content_items)) {
                $parent = get_content('one=true&subtype=dynamic&is_deleted=0&is_active=1');
                if ($parent == false) {
                    $parent = get_content('one=true&content_type=page&is_deleted=0&is_active=1');
                }
                if ($parent == false) {
                    $parent = 0;
                }
                $content_items = $this->map_array($content_items);
                $parent_id = $parent['id'];
                $restored_items = array();
                foreach ($content_items as $content) {
                    if (isset($content['title']) and $content['title'] != false) {
                        if (!isset($content['parent'])) {
                            $content['parent'] = $parent_id;
                        }
                        $restored_items[] = $this->save_content_item($content);
                    }
                }
                //   $query = \DB::getQueryLog();
                //  \Log::info('$query----'.print_r(($query), true));
                cache_clear('categories');
                cache_clear('content');
                $remaining = $remaining - 1;
                if ($remaining <= 0) {
                    file_put_contents($index_file, '0');
                }
                if ($total < $remaining) {
                    $total = 0;
                    $dir = $chunks_folder;
                    if ($handle = opendir($dir)) {
                        while (($file = readdir($handle)) !== false) {
                            if (!in_array($file, array('.', '..')) && !is_dir($dir . $file) and strstr($file, 'import_chunk_')) {
                                ++$total;
                            }
                        }
                    }
                    file_put_contents($index_file, $total);
                }
                $save_log_info['remaining'] = $remaining;
                $save_log_info['total'] = $total;
                // $this->log($save_log_info);
                $ret = array('success' => count($restored_items) . ' items restored', 'total' => $total, 'remaining' => $remaining);
                if ($total > 0 and $remaining > 0) {
                    $val2 = $total;
                    $val1 = $remaining;
                    $res = round($val1 / $val2 * 100);
                    $remaining_perc = 100 - $res;
                    $ret['percent'] = $remaining_perc;
                }
                return $ret;
            }
        }
        return false;
    }