Jump to content
MatthewSchenker

Create Pages (with File-Upload Field) via API

Recommended Posts

@videokid: ok that with readability is personal. agread

but could you explain why that did the trick for me?

 
but what trick does it?
 

if($upload_path.$filename !== $upload_path . $filename) echo 'Horst is wrong!';
else echo 'This is nonsense!';
  • Like 2

Share this post


Link to post
Share on other sites

The dot concatenates the right and left variables (strings), no matter when you use spaces or not. 

oftopic:

// 'concatenating numbers'
echo  6 . 6; //echo's the string 66
echo 6.6;    //echo's the float 6.6
  • Like 1

Share this post


Link to post
Share on other sites

I was always under the impression it didn't matter...

I've done some testing and in the end it seems

'This is nonsense! Horst is right'

however, putting back the spaces results in a not deleted file in the temp directory.... without the spaces... woosh temp file gone...  ???

Debug time!

Share this post


Link to post
Share on other sites

Hello Matthew

I saw in your code  under  "HANDLING MULTIPLE FILE-UPLOAD FIELDS IN THE SAME FORM".. I have done this by adding multiple in HTML form. This way there was no need to keep extra fields and run extra executes (Run foreach). Every thing was working fine until I put the file field inside a repeater. Now the new file is replacing older one inside repeater.

I am searching for solution but No luck. Any help is much appreciated.

here is my code:

$p2= pages->get($id);
$pdfs = new WireUpload('f_plan');
    $pdfs->setMaxFiles(8);
    $pdfs->setOverwrite(false);
    $pdfs->setDestinationPath($upload_path);
    $pdfs->setValidExtensions(array('pdf','xps'));

// execute upload and check for errors
    $pdf_files = $pdfs->execute();
    echo count($pdf_files); //count is showing number of files
    if(!count($pdf_files))
    {
    $pdfs->error("Sorry, but you need to add a file!");
    return false;
    }
    
    foreach($pdf_files as $filename)
    {
    $p2->of(false);
    $new_rep = $p2->floor_plan_rep->getNew(); //floor_plan_rep is my repeater with one file field inside named floor_plan.
    $p2->save();
    echo $pathname = $upload_path . $filename; //all the urls are okey
    $new_rep->floor_plan = $pathname;
    $p2->save();
    $p2->of(true);
    unlink($pathname);
    }

Share this post


Link to post
Share on other sites

After a few days of research I found this solution for my above problem.

Complete code is--

<?php

if ( isset($_POST['submit']) ) {

 $upload_path = $config->paths->templates . "images/";
$pdfs = new WireUpload('upload');
    $pdfs->setMaxFiles(8);
    $pdfs->setOverwrite(false);
    $pdfs->setDestinationPath($upload_path);
    $pdfs->setValidExtensions(array('pdf','xps','pptx'));

// execute upload and check for errors
   $pdf_files = $pdfs->execute();

    if(!count($pdf_files))
    {
    $pdfs->error("Sorry, but you need to add a photo!");
    return false;
    }
    $rep_id = array();
         $page->setOutputFormatting(false);
    foreach($pdf_files as $pdf){

        $building = $page->test_rep->getNew();
        $building->save();
        $rep_id[] = $building->id;
        $page->test_rep->add($building);

    }
            $page->save();
            $page->setOutputFormatting(true);



     if(count($rep_id) === count($pdf_files)){
        $count = 0;

        foreach($pdf_files as $pdf){
            $page->setOutputFormatting(false);
            $repeater_id = $rep_id[$count];
            $field_rep = $page->test_rep->get("id=$repeater_id"); // test_rep is table and test_rep_field is field of the table
            $field_rep->of(false);
            $field_rep->test_rep_field = $upload_path.$pdf;
            $field_rep->save();
            $page->save("test_rep");
            $page->setOutputFormatting(true);
            $count++;
        }

     }


  }
?>

<form action="<?php $_SERVER['PHP_SELF'] ;?>" method="post" enctype="multipart/form-data">

<p>Click the "Select Files" button below to upload your photo.</p>
<input type="file" name="upload[]" multiple />
<button type="submit" name="submit">Submit</button>
</form>

I hope this will help someone while uploading files in repeater fields.

  • Like 2

Share this post


Link to post
Share on other sites

How can i check if the user has choosen a valid file?

For example: User choose pdf but allowed is only jpg.

Share this post


Link to post
Share on other sites

How can i check if the user has choosen a valid file?

For example: User choose pdf but allowed is only jpg.

Check or prevent? If prevent:

$u = new WireUpload('files');
$u->setValidExtensions(array('pdf','xps','pptx'));
// etc..

Share this post


Link to post
Share on other sites

I used the code from here and modified it. And i have this:

// New wire upload
$service_upload = new WireUpload('service_file_upload_sw'); // References the name of the field in the HTML form that uploads the photo
$service_upload->setMaxFiles(5);
$service_upload->setOverwrite(false);
$service_upload->setDestinationPath($upload_path);
$service_upload->setValidExtensions(array('jpg', 'jpeg', 'png', 'tif', 'tiff', 'gif'));

When i now choose a pdf file and hit the submit button then there is no wrong file message.

Share this post


Link to post
Share on other sites

Hello Marcel,

in my case I had two level of validation,first via html form pattern,Second the above one. try to get error message with 

$service_upload->error();

Also if it dosent help get file names and extension with core php

$file_ext = $_FILES['my-file']['type'] ;

and check for right file.

 

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By VeiJari
      Hi, this is the first we are trying to make a page that has only one type of user that has access to every page. 
      The other users should only have a given access to specific pages, not to the whole template.
      My structure
      -Field -Organisation -Project -Report I want that the "measurer" role only has access to "project x" and it's children, but no view access to every project, organisation or field. I've tried to do this with https://modules.processwire.com/modules/page-edit-per-user/ but it still needs a view access to the whole tree to see the "project x" page. Or is there something I haven't figured out?
      Maybe I have to make it via the API: a select field in the "organisation" template where the admins could add the users and then I use hook to update the privileges?
      Have you done something like this and how did you accomplish it?
      Any help would be appreciated.
       
    • By cosmicsafari
      Hi all,
      Before I go potentially wasting time trying to achieve the impossible.
      Can anyone confirm if its possible to have a Page Reference field on a modules config page?
      I'm wanting to essentially just output a list of select able pages based on the a given selector (likely by template at this stage), wherein the select is the pages that the module should apply to etc. I was thinking a simple checkbox list would suffice is asmSelect isn't available.
      Essentially have it display the same way a Page Reference field would display on a template, where you can easily select a bunch of them.
      public function getInputfields() { $inputfields = parent::getInputfields(); $f = $this->modules->get('InputfieldPage'); $f->attr('name', 'testSelect'); $f->setAttribute('multiple', 'checkboxes'); $f->setAttribute('findPagesSelector', 'template=development'); $f->label = 'Test'; $inputfields->add($f); return $inputfields; } Figured something akin to the above would work but can't seem to get rid of this warning on the modules config screen though.

    • By 999design
      Hi all,
      Running into an odd error that I can't seem to get my head around.
      We have 2 separately created Formbuilder forms sitting on a single page.
      But we keep experiencing weird results with them, originally we couldn't get one of the forms to ever submit so we ended up disabling CSRF for them which let us get around this issue.
      However it then causes a problem in that with CSRF disabled, one of forms always records 2 entries on submission. Just a straight duplicate within the entries for that form.
      So trying to stop this happening we tried enabling CSRF again and although that does stop the duplicated entry, it ends up giving really weird feedback such as the attached screengrab.
      Hazarding a guess I assume whatever is trigger on submission is firing twice because of the presence of the second form, but I have no idea why this would be the case as they are 2 seperately named forms?
      Any ideas?

    • By Chris Bennett
      Hi all, I am going round and round in circles and would greatly appreciate if anyone can point me in the right direction.
      I am sure I am doing something dumb, or missing something I should know, but don't. Story of my life 😉

      Playing round with a module and my basic problem is I want to upload an image and also use InputfieldMarkup and other Inputfields.
      Going back and forth between trying an api generated page defining Fieldgroup, Template, Fields, Page and the InputfieldWrapper method.

      InputfieldWrapper method works great for all the markup stuff, but I just can't wrap my head around what I need to do to save the image to the database.
      Can generate a Field for it (thanks to the api investigations) but not sure what I need to do to link the Inputfield to that. Tried a lot of stuff from various threads, of varying dates without luck.
      Undoubtedly not helped by me not knowing enough.

      Defining Fieldgroup etc through the api seems nice and clean and works great for the images but I can't wrap my head around how/if I can add/append/hook the InputfieldWrapper/InputfieldMarkup stuff I'd like to include on that template as well. Not even sure if it should be where it is on ___install with the Fieldtype stuff or later on . Not getting Tracy errors, just nothing seems to happen.
      If anyone has any ideas or can point me in the right direction, that would be great because at the moment I am stumbling round in the dark.
       
      public function ___install() { parent::___install(); $page = $this->pages->get('name='.self::PAGE_NAME); if (!$page->id) { // Create fieldgroup, template, fields and page // Create new fieldgroup $fmFieldgroup = new Fieldgroup(); $fmFieldgroup->name = MODULE_NAME.'-fieldgroup'; $fmFieldgroup->add($this->fields->get('title')); // needed title field $fmFieldgroup->save(); // Create new template using the fieldgroup $fmTemplate = new Template(); $fmTemplate->name = MODULE_NAME; $fmTemplate->fieldgroup = $fmFieldgroup; $fmTemplate->noSettings = 1; $fmTemplate->noChildren = 1; $fmTemplate->allowNewPages = 0; $fmTemplate->tabContent = MODULE_NAME; $fmTemplate->noChangeTemplate = 1; $fmTemplate->setIcon(ICON); $fmTemplate->save(); // Favicon source $fmField = new Field(); $fmField->type = $this->modules->get("FieldtypeImage"); $fmField->name = 'fmFavicon'; $fmField->label = 'Favicon'; $fmField->focusMode = 'off'; $fmField->gridMode = 'grid'; $fmField->extensions = 'svg png'; $fmField->columnWidth = 50; $fmField->collapsed = Inputfield::collapsedNever; $fmField->setIcon(ICON); $fmField->addTag(MODULE_NAME); $fmField->save(); $fmFieldgroup->add($fmField); // Favicon Silhouette source $fmField = new Field(); $fmField->type = $this->modules->get("FieldtypeImage"); $fmField->name = 'fmFaviconSilhouette'; $fmField->label = 'SVG Silhouette'; $fmField->notes = 'When creating a silhouette/mask svg version for Safari Pinned Tabs and Windows Tiles, we recommend setting your viewbox for 0 0 16 16, as this is what Apple requires. In many cases, the easiest way to do this in something like illustrator is a sacrificial rectangle with no fill, and no stroke at 16 x 16. This forces the desired viewbox and can then be discarded easily using something as simple as notepad. Easy is good, especially when you get the result you want without a lot of hassle.'; $fmField->focusMode = 'off'; $fmField->extensions = 'svg'; $fmField->columnWidth = 50; $fmField->collapsed = Inputfield::collapsedNever; $fmField->setIcon(ICON); $fmField->addTag(MODULE_NAME); $fmField->save(); $fmFieldgroup->add($fmField); // Create: Open Settings Tab $tabOpener = new Field(); $tabOpener->type = new FieldtypeFieldsetTabOpen(); $tabOpener->name = 'fmTab1'; $tabOpener->label = "Favicon Settings"; $tabOpener->collapsed = Inputfield::collapsedNever; $tabOpener->addTag(MODULE_NAME); $tabOpener->save(); // Create: Close Settings Tab $tabCloser = new Field(); $tabCloser->type = new FieldtypeFieldsetClose; $tabCloser->name = 'fmTab1' . FieldtypeFieldsetTabOpen::fieldsetCloseIdentifier; $tabCloser->label = "Close open tab"; $tabCloser->addTag(MODULE_NAME); $tabCloser->save(); // Create: Opens wrapper for Favicon Folder Name $filesOpener = new Field(); $filesOpener->type = new FieldtypeFieldsetOpen(); $filesOpener->name = 'fmOpenFolderName'; $filesOpener->label = 'Wrap Folder Name'; $filesOpener->class = 'inline'; $filesOpener->collapsed = Inputfield::collapsedNever; $filesOpener->addTag(MODULE_NAME); $filesOpener->save(); // Create: Close wrapper for Favicon Folder Name $filesCloser = new Field(); $filesCloser->type = new FieldtypeFieldsetClose(); $filesCloser->name = 'fmOpenFolderName' . FieldtypeFieldsetOpen::fieldsetCloseIdentifier; $filesCloser->label = "Close open fieldset"; $filesCloser->addTag(MODULE_NAME); $filesCloser->save(); // Create Favicon Folder Name $fmField = new Field(); $fmField->type = $this->modules->get("FieldtypeText"); $fmField->name = 'folderName'; $fmField->label = 'Favicon Folder:'; $fmField->description = $this->config->urls->files; $fmField->placeholder = 'Destination Folder for your generated favicons, webmanifest and browserconfig'; $fmField->columnWidth = 100; $fmField->collapsed = Inputfield::collapsedNever; $fmField->setIcon('folder'); $fmField->addTag(MODULE_NAME); $fmField->save(); $fmFieldgroup->add($tabOpener); $fmFieldgroup->add($filesOpener); $fmFieldgroup->add($fmField); $fmFieldgroup->add($filesCloser); $fmFieldgroup->add($tabCloser); $fmFieldgroup->save(); /////////////////////////////////////////////////////////////// // Experimental Markup Tests $wrapperFaviconMagic = new InputfieldWrapper(); $wrapperFaviconMagic->attr('id','faviconMagicWrapper'); $wrapperFaviconMagic->attr('title',$this->_('Favicon Magic')); // field show info what $field = $this->modules->get('InputfieldMarkup'); $field->name = 'use'; $field->label = __('How do I use it?'); $field->collapsed = Inputfield::collapsedNever; $field->icon('info'); $field->attr('value', 'Does this even begin to vaguely work?'); $field->columnWidth = 50; $wrapperFaviconMagic->add($field); $fmTemplate->fields->add($wrapperFaviconMagic); $fmTemplate->fields->save(); ///////////////////////////////////////////////////////////// // Create page $page = $this->wire( new Page() ); $page->template = MODULE_NAME; $page->parent = $this->wire('pages')->get('/'); $page->addStatus(Page::statusHidden); $page->title = 'Favicons'; $page->name = self::PAGE_NAME; $page->process = $this; $page->save(); } }  
    • By marcus
      wireshell 1.0.0 is out    
      See Bea's post
       


      -------- Original post -----------
        Now this one could be a rather long post about only an experimental niche tool, but maybe a helpful one for some, so stay with me   Intention Do you guys know "Artisan" (Laravel) or "Drush" (Drupal)? If not: These are command line companions for said systems, and very useful for running certain (e.g. maintenance, installation) task quickly - without having to use the Admin Interface, first and foremost when dealing with local ProcessWire installations. And since it has a powerful API and an easy way of being bootstrapped into CLIs like this, I think such a tool has a certain potential in the PW universe.    It's totally not the first approach of this kind. But: this one should be easily extendable - and is based on PHP (specifically: the Console component of the Symfony Framework). Every command is tidily wrapped in its own class, dependencies are clearly visible, and so on.   ( Here was the outdated documentation. Please visit wireshell.pw for the current one )
×
×
  • Create New...