Jump to content

sjohnson

Members
  • Posts

    5
  • Joined

  • Last visited

Community Answers

  1. sjohnson's post in Odd CSV Import issue that ends up with fatal php error (FieldtypePage bug?)) was marked as the answer   
    Hi All,
    I've figured out that this error seems to only happen when you are using the "Custom PHP Selection Code" on your Page Input Fields
    It seems that PW uses it, but not completely, thereby causing unset errors.
    I've done a bunch of rework and now my code works without issue, so in the hopes that this can help others, here's my revised code.
    FYI -- This bug still exists, I'm just forcing my code to use the same selections as in the "Custom PHP Selection Code", thereby avoiding the unset errors.  Then again, maybe this is how it's supposed to work and I should have been duplicating the selection code the whole time
    Also note that I use a folder structure like so:
    RootSystem 1SkillsSkill 1 RolesRole 1 System 2Skills Roles Which allows me to have the same Skills, Roles, etc. names in different systems with different attributes, and still be able to link them properly.
    I do this with code like "$myParent->parent->child('name=skills')->id" which allows me to join page fields within the same System together.
    // If no filename exit if ($wire->page->getUnformatted('section_slogan') == '') throw new Wire404Exception(); // File Pointer $myfile = "http://" . $config->httpHost . $config->urls->templates . "loaders/" . $wire->page->getUnformatted('section_slogan'); $content = "<p>Using Loader File '{$myfile}'</p><hr/>"; // Read The Data File if (($handle = fopen("$myfile", "r")) !== FALSE) { // Get the Setup Info if (($data = fgetcsv($handle, 0, ",")) !== FALSE) { $myTemplate = $templates->get("$data[0]"); $myParent = $wire->pages->get("path='$data[1]'"); if (($myTemplate->id == 'NullPage') || ($myParent->id == 'NullPage')){ throw new Wire404Exception(); } } else { throw new Wire404Exception(); } // Setup the parent $errorLog = ""; $newItem = new Page(); $newItem->template = $myTemplate; // Tell User what's going on: $content .= "<p>Adding New Pages based on the '{$newItem->template->name}' template to parent '{$myParent->title}' path: '{$myParent->path}'</p><hr/>"; // List out the fields in the template $num = count($newItem->fields); $content .= "<div class='row'>"; $content .= "<div class='col-sm-6'>"; $content .= "<p> $num fields in template</p><dl class='dl-horizontal'>"; foreach($newItem->fields as $itemfd) { $content .= "<dt>[" . $itemfd->name . "]</dt><dd><b>Type:</b>  " . $itemfd->type . "</dd>"; } $content .= "</dl>"; // Read header record and match-up fields to template $fieldmatchup = ''; if (($data = fgetcsv($handle, 0, ",")) !== FALSE) { $num = count($data); for ($c=0; $c < $num; $c++) { foreach($newItem->fields as $itemfd) { if($data[$c] == $itemfd->name) { $fieldmatchup[$itemfd->name] = $c; } } } } $content .= "</div>"; $content .= "<div class='col-sm-6'>"; // Show the File's Field Array $content .= "<p>Display the File's Field Array</p><dl class='dl-horizontal'>"; foreach($fieldmatchup as $item_key => $item_value) { $content .= "<dt>[" . $item_key . "]</dt><dd>" . $item_value . "</dd>"; } $content .= "</dl>"; $content .= "</div>"; $content .= "</div>"; $content .= "<hr/><h4>Reading File</h4><hr/>"; // Now add the data $row = 0; $newItem = ''; $content .= "<div class='row'>"; while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { $content .= "<div class='col-sm-6'><hr/><dl class='dl-horizontal'>"; $num = count($data)-1; $row++; $mySel = "parent=" . $myParent->id . ", name=" . $sanitizer->pageName($data[$fieldmatchup['title']]) . ""; $dupPage = $wire->pages->get($mySel); if($dupPage->id == 'NullPage') { $content .= "<dt>New Page:</dt><dd>[" . $mySel . "] -- Creating<br/> </dd>"; // Setup blank WireArray based on a template $newItem = new Page(); $newItem->template = $myTemplate; $newItem->title = $data[$fieldmatchup['title']]; $newItem->parent = $myParent; $newItem->save(); } else { $content .= "<dt>Duplicate Page:</dt><dd>[" . $mySel . "] -- Updating<br/> </dd>"; $newItem = $dupPage; } $content .= "<dt>Record [{$row}]:</dt><dd>Field List<br/> </dd>"; $newItem->of(false); $errorList = ""; foreach($newItem->fields as $itemfd) { $key = $itemfd->name; $value = $data[$fieldmatchup[$key]]; if ($key != 'title') { if ($value == ""){ $content .= "<dt>- Field:</dt><dd>[{$key}] = [{$value}] -- Clearing</dd>"; $newItem->set($key, $value); } else { switch ($itemfd->type) { case 'FieldtypePage': $pageFilters = ""; switch ($key) { case 'pc_role_category': $pageFilters = ", has_parent={$myParent->parent->child('name=categories')->id}"; break; case 'sa_skill': $pageFilters = ", has_parent={$myParent->parent->child('name=skills')->id}"; break; case 'career_skills': $pageFilters = ", has_parent={$myParent->parent->child('name=skills')->id}"; break; case 'associated_book': $pageFilters = ", has_parent={$myParent->parent->child('name=books')->id}"; break; default: $pageFilters = ""; } $pageList = explode('|', $value); unset($pageIDs); $pageItem = ""; if (count($pageList) > 1) { $cnt = 0; foreach($pageList as $arrItem) { $cnt++; $value2 = $sanitizer->pageName($arrItem); $pageItem = wire('pages')->get("name={$value2}{$pageFilters}"); if ($pageItem->id != 'NullPage') { $content .= "<dt>- Page Ref:</dt><dd><b>{$itemfd->label}</b> [{$key}][{$cnt}]: {$arrItem} -- Added</dd>\n"; $pageIDs[] = "{$pageItem->id}"; } else { $content .= "<dt>- Page Ref:</dt><dd><b>{$itemfd->label}</b> [{$key}][{$cnt}]: {$arrItem} -- Not Found</dd>\n"; $errorList .= "Page Ref: <b>{$itemfd->label}</b> [{$key}]: {$arrItem} -- Not Found<br/>";; } } } else { $value2 = $sanitizer->pageName($value); $pageItem = wire('pages')->get("name={$value2}{$pageFilters}"); if ($pageItem->id != 'NullPage') { $content .= "<dt>- Page Ref:</dt><dd><b>{$itemfd->label}</b> [{$key}]: {$value} -- Added</dd>\n"; $pageIDs[] = "{$pageItem->id}"; } else { $content .= "<dt>- Page Ref:</dt><dd><b>{$itemfd->label}</b> [{$key}]: {$value} -- Not Found</dd>\n"; $errorList .= "Page Ref: <b>{$itemfd->label}</b> [{$key}]: {$value} -- Not Found<br/>"; } } if (isset($pageIDs)) { $newItemVal = implode('|', $pageIDs); $newItem->set($key, $newItemVal); } break; default: $content .= "<dt>- Field:</dt><dd>[{$key}] = [{$value}]</dd>"; $newItem->set($key, $value); } } } } if ($errorList <> "") { $errorLog .= $errorList; $errorList = "<hr/><b>Loading Errors</b><hr/><p>" . $errorList . "</p>"; $tempBody = $newItem->getUnformatted('body'); if ($tempBody !== Null) { $tempBody = $tempBody . $errorList; $newItem->set('body', $tempBody); } } $newItem->save(); $content .= "<dt> </dt><dd> </dd>"; $content .= "<dt>-- Saved:</dt><dd><a target='_blank' href='" . $newItem->editUrl . "'>" . $newItem->title . " [" . $newItem->id . "]</a></dd>"; $content .= "</dl>"; $content .= "<hr/></div>"; } $content .= "</div>"; fclose($handle); } $content .= "<hr/><b>{$row} records added/updated</b>"; if ( $errorLog !== "" ) $content .= "<hr/><b>Error Log</b><br/>{$errorLog}"; Hope this helps someone else!
×
×
  • Create New...