Jump to content

Trying to save pages from root folder / Permission Problem


Orkun
 Share

Recommended Posts

Hi Guys

I am trying to save Pages with the API from the root Folder in a file named refreshIndex.php. The code looks like this:

$root = "/path/to/root";
include($root . "index.php");

//$doctors = wire('pages')->find("template=doctors");
$jobs = wire('pages')->find("template=jobs");
//$news = wire('pages')->find("template=news");
//$specialities = wire('pages')->find("template=specialities-clinics");
//$events = wire('pages')->find("template=signup-form-formbuilder");
//$dbpages = wire('pages')->find("template=doctors|specialities-clinics|news|signup-form-formbuilder|jobs");

//$allpages = wire('pages')->get(27200)->find("");
   
foreach ($jobs as $stpage) {
	$stpage->save();
}

Now at the moment I am trying to save Job Pages. They are 2 job pages right now. It saves 1 of them and at the 2 one I get an Error like this:

Error: Uncaught WirePermissionException: Page '/de/jobs/test-job_ge/' is not currently viewable. in /pathtoroot/wire/modules/PageRender.module:319

They are both using the same template with the same permissions respectively they are visible (guest user is viewable). And also the languages of the page are all active inside page settings.

 

Somehow my Hook is responsible for this. The hook is the reason for my Script above. I am trying to update the index field for my site search. The hooks works fine when I am saving the pages from the backend interface, but I can't save all pages from the backend since they are over 1500 pages I need to save.

$this->addHookBefore('Pages::saveReady', $this, 'hookIndexingBefore');

protected function hookIndexingBefore( HookEvent $event ) {
 		$options = array();
        $page = $event->arguments("page");

        // abort when true
        if(!$page->template->hasField("index")) return;
        if($page->isNew() || $page->isTrash()) return;

        // save user lang
        $language = $this->wire("user")->language;    

        // clear index field at the begin
        $page->index = '';

        if($page->is("template=specialities-clinics")){
        	   $options['sender'] = $page->choose_sender_2016->id;
        }
        $options['pagename'] = $page->name;

        foreach($this->wire("languages") as $lang) {

            $this->wire("user")->language = $lang; // change user lang

            wire('pages')->setOutputFormatting(true);
            $content = $page->render($options);
            wire('pages')->setOutputFormatting(false);

            if($content){
            	$startStr = "<!--### start-indexing-area ###-->";
		        $endStr = "<!--### end-indexing-area ###-->";
		        preg_match_all('/'.$startStr.'(.*)'.$endStr.'/siU', $content, $matches);
		        $newContent = preg_replace("/<div class='breadcrumb.*'>.*<\/div>/siU", '', $matches[1][0]);
		        $newContent = str_replace('<', ' <', $newContent);
		        $newContent = strip_tags($newContent);
		        $newContent = preg_replace("/\s\s+/", " ", $newContent);
            } 

            $page->index .= $newContent;
        }

        $this->wire("user")->language = $language; // restore user language
}

 

Link to comment
Share on other sites

Ok Guys I could make it to work changing:

wire('pages')->setOutputFormatting(true);
$content = $page->render($options);
wire('pages')->setOutputFormatting(false);

like this:

$page->setOutputFormatting(true);
$content = $page->render($options);
$page->setOutputFormatting(false);

 

Link to comment
Share on other sites

EDIT: Ok it doesn't matter anymore. I removed the index field from overview-content-page and fetch-content-page template since I realised that they are just overview pages which can be searched through their title and body field. So no need for an index field.

I have now another really strange problem. When I try to change the template of a page to a specific template (in this case overview-content-page or fetch-content-page) I end up on a blank page with this url (/processwire/page/edit/saveTemplate) and the template doesn't get changed. It's because of my Hook for updating the index field. But I don't know what is actually causing this. Since I have also other templates with the field index in it and there are no problems.

 

Edited by Nukro
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...