Jump to content

Creating pages from directories and images


JoshoB
 Share

Recommended Posts

For a client, I have to convert 4900 pictures to pages in ProcessWire. The pictures are organized into directories, and the structure has to preserved. So for the directories I want to create pages with the template "pictures", while the images will have the template "picture". There's already quite a few images that have been uploaded by hand, so these new pictures will be made children of a new page "a2" (name/url).

Now, I've tried the below code (the contents of a new page with a template "conversion" that should be run inside ProcessWire), but I'm getting an error about an unknown selector. No doubt there's a problem in the function with calling $pages, but I can't see what it is. Could someone have a look, or post a better solution to what I'm trying to do? Thanks!

Edit: updated function, which still doesn't work.

function listFolderFiles($dir, $root)
{
    $pages = wire('pages');
    
    $ffs = scandir($dir);
    echo '<ol>';
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            echo '<li>'.$ff;
            
            // This is a directory
            if(is_dir($dir.'/'.$ff))
            {
                // Set title and template
                $title = $ff;
                $template = 'pictures';

                // Set parent
                $parent = explode("/", $dir);
                $parent = end($parent);
                if(strlen($parent) > 30) $parent = 'a2';
                $parent = trim($parent);

                // Set selector
                $selector = 'name=' . $parent . ', template=pictures, id>16521, include=all';

                // Create page
                $pictureList           = new Page();
                $pictureList->template = $pages->get($template);
                $pictureList->parent   = $pages->get($selector);
                $pictureList->title    = $title;
                $pictureList->save();

                // Output
                echo ' [DIR - title = ' . $title . ' and template = ' . $template . '; parent = ' . $parent . ']';
                listFolderFiles($dir.'/'.$ff);
            }
            
            // This is a picture
            else
            {
                // Remove extension from filename for title
                $filebroken = explode( '.', $ff);
                $extension = array_pop($filebroken);
                $title = implode('.', $filebroken);
                $title = str_replace("_", "-", $title);

                // Set parent
                $parent = explode("/", $dir);
                $parent = end($parent);
                $parent = trim($parent);

                // Set template
                $template = 'picture';

                // Set selector
                $selector = 'name=' . $parent . ', template=pictures, id>16521, include=all';

                // Create page
                $picture           = new Page();
                $picture->template = $pages->get($template);
                $picture->parent   = $pages->get($selector);
                $picture->title    = $title;
                $picture->save();

                // Upload picture
                $picture->images->add($ff);
                $picture->save();

                // Output
                echo ' [FILE - title = ' . $title . ' and template = ' . $template . '; parent = ' . $parent . ']';
            }
            echo '</li>';
        }
    }
    echo '</ol>';
}

// Create output
echo '<pre>' . "\n";
echo '==================' . "\n";
echo 'Picture conversion' . "\n";
echo '==================' . "\n\n";

// Set directory
$dir = "D:\Websites\example.org\www\photos";

// Parent (root) page 
$root = $pages->get("name=a2, template=pictures, include=all"); 
echo 'Parent is located <a href="' . $root->url . '">here</a>.' . "\n\n"; 

// Go through directories and files
listFolderFiles($dir, $root);

// Close output
echo '</pre>' . "\n";
Link to comment
Share on other sites

something on this topic to create childpages on a upload image field....in a kind of album->images tree structure:

https://processwire.com/talk/topic/8698-creating-page-for-every-image-uploaded/page-2#entry84891

it's a little module from adrian (and some cosmetics and tests from me) to get a simple and fast solution to use a normal image field as a kind of upload + create shildpages function...take a look if the code helps in your case.

regards mr-fan

Link to comment
Share on other sites

Thanks! I already looked at that, but the problem -- unless I'm missing something -- is how to get the directory structure to keep the same. Also, unless I'm mistaken, the solution you posted would require me to drag and drop 4900 pictures into the upload field? I don't think that would work.

Link to comment
Share on other sites

oh overread the amounth....;) sorry for shure that is no option.

but the exact error would be good. Have you Bootstraped PW or where are you running this code admin.php or template???

regards mr-fan

Link to comment
Share on other sites

I run it as a template. (Edit: I've updated the function to create a new page as parent of a recently created page, based on ID being larger than the otherwise youngest page. Doesn't make a difference, but hope springs eternal!)

The error I'm getting is as follows:

Error: Exception: Unknown Selector operator: '' -- was your selector value properly escaped? (in D:\Websites\example.org\www\wire\core\Selectors.php line 281)

#0 D:\Websites\example.org\www\wire\core\Selectors.php(318): Selectors->create('pictures', '', '')
#1 D:\Websites\example.org\www\wire\core\Selectors.php(115): Selectors->extractString('pictures')
#2 D:\Websites\example.org\www\wire\core\Selectors.php(104): Selectors->setSelectorString('pictures')
#3 D:\Websites\example.org\www\wire\core\Pages.php(203): Selectors->__construct('pictures')
#4 [internal function]: Pages->___find('pictures', Array)
#5 D:\Websites\example.org\www\wire\core\Wire.php(387): call_user_func_array(Array, Array)
#6 D:\Websites\example.org\www\wire\core\Wire.php(325): Wire->runHooks('find', Array)
#7 D:\Websites\example.org\www\wire\core\Pages.php(310): Wire->__call('find', Array)
#8 D:\Websites\example.org\www\wire\core\Pages.php(310): Pages->find('pictures', Array)
#9 D:\Websites\example.org\www\wire\core\Pages.php(325): Pages->findOne('pictures

This error message was shown because you are logged in as a Superuser. Error has been logged.

Link to comment
Share on other sites

You're right. That was the problem. Thanks!

I am now left with figuring out how to get the images uploaded. I cannot, for the life of me, figure out what kind of URL/path the system wants...

This sorry excuse of code is what I'm using now to figure out the path to the picture, and it doesn't work:

// Get the file name for upload
$fullfilename = str_replace("/", "\\", $dir) . '\\' . $ff;
$fullfilename = dirname($fullfilename) . '\\' . $ff;

It's something simple. I've done it before, but I'm stuck now. (This is on a test server, XAMPP, running on Windows.)

Link to comment
Share on other sites

it should just need the path to the file
.

$ff should be the server path
 // Upload picture
                $picture->images->add($ff);
                $picture->save('images');
Link to comment
Share on other sites

Thanks, I know. But for some reason it's not working. I'm unable to come up with a decent path. Any hints what it should look like?

Edit: this is the error I get, with line 85 = $picture->images->add($fullfilename);

Error: 	Call to a member function add() on null (line 85 of D:\Websites\example.org\www\site\templates\conversion.php) 

And this is the complete, revised code that is not working:

function listFolderFiles($dir, $root)
{
    $pages = wire('pages');
	
    $ffs = scandir($dir);
    echo '<ol>';
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            echo '<li>'.$ff;
			
			// This is a directory
            if(is_dir($dir.'/'.$ff)) 
			{
				// Set title and template
				$title = $ff; 
				$template = "pictures"; 

				// Set parent
				$parent = explode("/", $dir);
				$parent = end($parent); 
				$parent = trim($parent); 

				// Set selector
				$selector = "name=$parent, template=pictures, id>16521, include=all";

				// Create page
				$pictureList           = new Page();
				$pictureList->template = $template;
				$pictureList->parent   = $pages->get($selector);
				$pictureList->title    = $title;
				$pictureList->save();

				// Output
				echo ' [DIR - title = ' . $title . ' and template = ' . $template . '; parent = ' . $parent . ']'; 
				listFolderFiles($dir.'/'.$ff, $root);
			}
			
			// This is a picture
			else
			{
				// Remove extension from filename for title
				$filebroken = explode( '.', $ff);
				$extension = array_pop($filebroken);
				$title = implode('.', $filebroken);
				$title = str_replace("_", "-", $title); 

				// Set parent
				$parent = explode("/", $dir);
				$parent = end($parent); 
				$parent = trim($parent); 

				// Set template
				$template = "picture";

				// Set selector
				$selector = "name=$parent, template=pictures, id>16521, include=all"; 
				
				// Get the file name with relative path
				$fullfilename = $dir.'/'.$ff;
				$fullfilename = str_replace("/", "\\", $fullfilename); 
				// $config->paths->root . '/photos/' . $ff; 
				//$fullfilename = str_replace("/", "\\", $dir) . '\\' . $ff; 
				//$fullfilename = dirname($fullfilename) . '\\' . $ff; 
				
				// Download link for debugging purposes
				echo ' <a href="' . $fullfilename . '">download</a>';

				// Create page
				$picture           = new Page();
				$picture->template = $template;
				$picture->parent   = $pages->get($selector);
				$picture->title    = $title;
				$picture->save();

				// Upload picture			
				$picture->images->add($fullfilename);
				$picture->save();

				// Output
				echo ' [FILE - title = ' . $title . ' and template = ' . $template . '; parent = ' . $parent . ']'; 
			}
            echo '</li>';
        }
    }
    echo '</ol>';
}

// Create output
echo '<pre>' . "\n";
echo '==================' . "\n";
echo 'Picture conversion' . "\n";
echo '==================' . "\n\n";

// Set directory
$dir = $config->paths->assets . 'a2'; 

// Parent (root) page 
$root = $pages->get("name=a2, template=pictures, id>16521, include=all"); 
echo 'Parent is located <a href="' . $root->url . '">here</a>.' . "\n\n"; 

// Go through directories and files
listFolderFiles($dir, $root);

// Close output
echo '</pre>' . "\n";
Link to comment
Share on other sites

Some more clarifications: as far as I know, you need a path to upload an image (URLs don't work anyway when trying to upload), and I've tried various forms of those (full Windows paths, since I'm using XAMPP on my computer instead of the live website). I've already shifted all the photos to /site/assets/ and used $config->path to have the system located the photos, but that's not working. I keep getting the error that ProcessWire cannot add NULL (whatever I try), so the path isn't working.

Any suggestions would be great to get me moving. Thanks.

Edit: creating the PictureList pages works, by the way, and the system does create the first regular Picture Page, too, but then the fatal error (NULL as file) causes the system to die.

Link to comment
Share on other sites

Some more clarifications: as far as I know, you need a path to upload an image (URLs don't work anyway when trying to upload)

Actually, that is not true - you can use a URL to an image - even from an external website.

The Error: Call to a member function add() on null error suggests to me that the picture template may not have a field called "images". Have you checked this?

If you are adding from a local path it needs to be the full server path to the image, starting at the very root of the directory structure - it could be something like /var/www/myimagesfolder/myimage.jpg

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...