Jump to content

creating new page in combination with a file upload


blackeye1987
 Share

Recommended Posts

hi,

so far i am learnin a lot of new stuff using Processwire and i like the way it works.

but i got to a point where i am confused

i got a dynamic form (made with jquery)

which has :

company name (string)

description (string)

categories (checkboxes)

image (file)

after sending the post i have inside my $_POST my important data for the Page which is fine so far

my template or the Company has the standart images field from PW

while creating the page for the new company i have no clue how to get my image into the page

so my question is when should i use WireUpload and how ?

my guess so far was :

creating page

get created page

upload image

edit page so the page has the file

and then save the page again

this seems a bit odd aswell so i just decided to ask you guys

thanks for the help already and have a good day

Link to comment
Share on other sites

Hi Blackeye. Welcome to PW and the forums

Creating pages with upload:

As for creating a page and adding images, you are on your way...

  • call the method for creating a new page - new Page();
  • get its template
  • get its parent
  • add a title
  • save
  • add image
  • save

all via the API...Make sure to sanitize all your form values..

see the examples on this page: http://processwire.com/api/variables/pages/

wireUpload: See these

http://processwire.com/talk/topic/1809-image-upload-via-wireupload/

http://processwire.com/talk/topic/3105-create-pages-with-file-upload-field-via-api/

http://processwire.com/talk/topic/197-import-images-from-file-system-using-the-pw-api/

There's numerous other examples in the forums  :). See also these snippets: https://gist.github.com/somatonic

  • Like 2
Link to comment
Share on other sites

Hey so Thanks for the Answer and the Links quiet helped me.

i couldn't find any solution how i want to get my result so i splitted the whole thing a bit up.

so my next problem is that.

if i want to add an picture to a coupon where the "new" image field is only allowed to have 1 picture with :

$thisPage->image->add($filename)

the field will have after the next upoad 2 pictures (this doesn't seem to be right )

so what i did was

putting this in front of the add:

$thisPage->image->remove($thisPage->image->first());

it works now but seems a bit odd to handle is there any direct way ? or am i doing something wrong ?

for all those who will have similar problems so far here is my  code:

//if i upload a file then files will be filled so it will be a diffrent form then when i post my data with the "new" coupons

if(!empty($_FILES)){
        $thisPage = $pages->get($_POST['couponid']);
        $thisPage->setOutputFormatting(false);
        $u = new WireUpload('image');
        $u->setMaxFiles(1);
        $u->setOverwrite(true);
        $u->setDestinationPath($thisPage->image->path());
        $u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png'));
        $thisPage->image->remove($thisPage->image->first());
        foreach($u->execute() as $filename) { $thisPage->image->add($filename); }
        $thisPage->save();
    }
 
 
//if its a post without a file it has to be the array with new coupons
    if(!empty($_POST)){
 
//i wanted to be able to make more coupons in just 1 sitting (better use handling)
 
        foreach($_POST['data'] as $newcoupon){
            // first create the coupon page
 
//in PW i have my template coupon where all coupons are in so i get the last id of the children and add 1 for the name and my "own id"
            $last_id = $pages->get(1164)->children()->last()->id;
            $last_id += 1;
            $t = new Page();
            $t->template = $templates->get('coupon');
            $t->c_id = $last_id;
            $t->parent = $pages->get(1164); // 1164 => /coupons/
            $t->title = 'coupon_'.$last_id;
            $t->blz = $user->blz;
            $t->company = $newcoupon['company'];
            $t->what_does = $newcoupon['what_does'];
            $t->from = $newcoupon['from'];
            $t->to = $newcoupon['to'];
 
            foreach($newcoupon['categories'] as $category => $not_in_use){
                $categories .= $category.' ';
            }
 
            $t->category = $categories;
            $t->save();
        }
    }

the made coupons are listed for the admin to add the images afterwards for each coupon

i couldn't find a way to add 1 image to each coupon i make all combined in 1 big post so i splitted it up.

hope everyone who will have the same issue will find his or hers solution here and thanks for the help so far

if anyone has some ideas how to make the code better like to hear it.

so far have a nice day and thanks

Link to comment
Share on other sites

the field will have after the next upoad 2 pictures (this doesn't seem to be right )

so what i did was

putting this in front of the add:

$thisPage->image->remove($thisPage->image->first());

All image and file fields technically support multiple files. In your field settings, when you set it to contain a max of 1 file, you are really telling it to behave as a single file on the front-end of your site (for API convenience). But when it comes to administering files/images from the API side, all image/file fields are treated the same (when output formatting is off, as it would be when performing any kind of page manipulation). What I might suggest is to use this bit of a code instead when replacing your single image:

$thisPage->image->deleteAll();
$thisPage->save();
// now add your new image 
  • 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...