Jump to content

abdus

Members
  • Posts

    743
  • Joined

  • Last visited

  • Days Won

    42

Everything posted by abdus

  1. I was reiterating the suggestion from PHP The Right Way, that I found out just yesterday. Which got me curious, and did my own tests. RAM usage doesn't change, but on CPU time it has some effect. // 8000 pages with title and body fields $pp = $pages('template=basic, parent=1384'); $out = ''; foreach($pp as $p) { $t = $p->title . microtime(); $b = $p->body . mt_rand(0, 1e5); $out .= $t . $b; } echo strlen($out); // 18384.08ms, 25.00 MB // 8000 pages with title and body fields $pp = $pages('template=basic, parent=1384'); $out = ''; foreach($pp as $p) { $out .= $p->title . microtime(); $out .= $p->body . mt_rand(0, 1e5); } echo strlen($out); // 17617.05ms, 25.00MB $out = ''; foreach($pages('template=basic, parent=1384') as $p) { $out .= $p->title . microtime(); $out .= $p->body . mt_rand(0, 1e5); } echo strlen($out); // 17927.9ms, 25.00MB Verdict: Don't believe everything you read on the internet. Do your own tests.
  2. Why not use closures? function print_foo() use($foo) { // to be able to change $foo use get it by reference &$foo instead print_r($foo); } One distinction is that with closures $foo will have the value at the moment of function creation, whereas global $foo will have the value of $foo at the time of execution. Often this doesn't matter, but you should be aware. Also with closures you can reference variables from parent scope. To access even higher scopes, you'll have to use globals. https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and
  3. WireCache can help you a lot when rendering the full list. You can use it to cache a strings, arrays, WireArrays, or PageArrays and retrieve back when you need it https://processwire.com/api/ref/wire-cache/ <?php namespace ProcessWire; $options = $cache->get('unit-options'); if (!$options) { $options = $pages('template=unit-option'); $cache->save('unit-options', $options, WireCache::expireDaily); } // generate your markup // or cache your markup and use that instead For the filtered list that you're building with GET parameter, if the parameter doesnt vary wildly and can only take a small number of values/combinations, than you can cache those results as well.
  4. Ah, I think you're looking for this: https://processwire.com/api/modules/select-options-fieldtype/#manipulating-options-on-a-page-from-the-api But it wont be as simple as $page->progress_status += 1, because FieldtypeSelect doesn't work like that.
  5. Hmm. To save/increment the status you can use: $page->of(false); $page->progress_status = $page->progress_status + 1; $page->save();
  6. Where's the form? Backend? Frontend? The hook I've written works (not tested) on the backend. Achieving it on the frontend is a bit more convoluted. If you go a bit more into detail, I can help you more, though.
  7. If you mean what I think you mean, then you can hook before InputfieldSelect::renderValue and change option labels depending on a field value. Haven't tried this, but it's a good start. $this->addHookBefore('InputfieldSelect::renderValue', function (HookEvent $e) { /** @var $select InputfieldSelect */ /** @var $page Page */ $select = $e->object; $page = $select->hasPage(); $options = $select->getOptions(); $options['myOption'] = $options['myOption'] . " -- " . $page->myStatusField; $select->set('options', $options); });
  8. Some points: I think you missed the part where you're using $locations, otherwise why are you getting all unit-options and their locations only to scrap all that and fetch it again? $unitOptions = $pages->find("template=unit-option"); // never used $unitResults = $pages->find("template=unit-option, sort=unit_name_ref"); If you're dealing with large number of pages, avoid excessive assignment of large values to variables, it'll increase memory usage, this may fill up the ram and server may start using swap partitions, which is always slower. Instead of $_GET, there's $input->get and for direct sanitization $input->get->text() (and ->selectorValue if you're going to use it in selectors). <?php namespace ProcessWire; /** @var $pages Pages */ /** @var $input WireInput */ // $myLocation = ( !empty($_GET['location']) ) ? $sanitizer->text($_GET['location']) : null; $myLocation = $input->get->selectorValue('location'); $unitResults = null; if ($myLocation) { $unitResults = $pages("template=unit-option, unit_location=$myLocation, sort=unit_name_ref"); } else { $unitOptions = $pages->find("template=unit-option") ->explode('unit_location'); // filter empty values and duplicates $unitOptions = array_unique(array_filter($unitOptions)); $locations = join("|", $unitOptions); $unitResults = $pages("template=unit-option, unit_location=$locations, sort=unit_name_ref"); } Other than these points, I'm not sure why you'd get slow results for several hundred pages. If you give a more complete picture of your page structure, we'd be able to help you better
  9. try this one. I forgot to add ->type, when you add it both methods should work.
  10. I like it in the way it's more functional, and easy to move around, but you have to admit, it looks quite old. It's a bit too colorful for my taste. I like having a clean interface
  11. This seems to work $implements = wireClassImplements($field->type); $isMulti = in_array('FieldtypeLanguageInterface', $implements); // OR $isMulti = $field->type instanceof FieldtypeLanguageInterface; Adapting to your code: $data = []; foreach ($page->template->fieldgroup as $field) { // check if field is multi language if ($field->type instanceof FieldtypeLanguageInterface) { continue; } $data[] = $field; } return $data;
  12. @SarbjitGrewal The code looks to be from an ASP template. This is a forum for a PHP based CMS called ProcessWire. Are you sure you're at the right place? Edit: Now it's from a Genesis based WordPress theme
  13. Cloudinary.com seems to offer everything you need and some more. You can get it to fetch images from your server on the fly and serve the right size image to visitors device without uploading images beforehand. Free tier is quite generous, too. https://cloudinary.com/features
  14. You can use localStorage to save and restore scroll position. Something to start with: (not tested) function saveScroll(href) { // save current scroll position for this page localStorage.setItem('scroll__' + href, window.scrollY); } function restoreScroll(href) { // restore previous scroll or scroll to top let scroll = localStorage.getItem('scroll__' + href); $('html, body').animate({ scrollTop: scroll ? scroll : 0 }, 10); } window.addEventListener('beforeunload', function (e) { // remove scroll position localStorage.removeItem('scroll__' + location.pathname); }); $(document).on('click', 'a:not(.external)', function (e) { saveScroll(this.href); history.pushState(null, null, this.href); replacePage(this.href); e.preventDefault(); }); $(window).bind('popstate', function () { replacePage(location.pathname); restoreScroll(location.pathname); }); https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
  15. From @ryan in this blog post
  16. A generous use of WireCache can help with generating overviews for a large number trees. <?php namespace ProcessWire; /** @var $cache WireCache */ foreach ($trees as $tree) { $overview = $cache->getFor($tree, "tree-overview"); if (!$overview) { $overview = wireRenderFile('parts/tree-overview'); $cache->saveFor($tree, 'tree-overview', WireCache::expireWeekly); } echo $overview; }
  17. It's usually plug and play, I dont remeber having any problems with the upgrade. Unless you're using deprecated features, you should be ok http://php.net/manual/en/migration70.php http://php.net/manual/en/migration70.deprecated.php Here are some guides on Tuts+ and DigitalOcean https://code.tutsplus.com/tutorials/upgrading-your-linux-server-to-php-7--cms-27583 https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-ubuntu-14-04
  18. These might help you https://processwire-recipes.com/recipes/activate-all-languages/ https://processwire.com/talk/topic/4383-how-to-set-language-active-via-api/
  19. Yes. Certainly so. http://www.zend.com/en/resources/php7_infographic
  20. Based on this forum post, I wanted to find a more elegant solution for changing a field's settings per template. Hooking the page editor after it generates the form works and may still be needed for more complex modifications. But using field & template context, it's easier to modify the field settings and it greatly reduces the need for creating an almost identical field just to adjust a few things. In my blog post, I've written on how to extend contextual options to allow any fieldtype / inputfield settings to be changed depending on the template. Hope you find it useful, and if you have any questions or comments, feel free to post them here. https://abdus.co/blog/doing-more-with-fewer-fields-in-processwire/
  21. That was my initial experience as well. It served me well in the absence of xDebug (still use Tracy quite often, though), but now that I got it running again (by manually removing __debugInfo methods()) I don't use it as much. Console feature is immensely useful, you'll love it.
  22. You can manually include any file you want. You dont have to use $config->appendTemplateFile. <?php namespace ProcessWire; // home template ?> <div pw-id="content"> this is content </div> <?php include_once 'layouts/my-layout.php'; Only requirement is that the file you include needs to have <!DOCTYPE declaration and <html> tag in it. <?php namespace ProcessWire; // my-layout.php ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <main id="content"></main> </body> </html> From the documentation in WireMarkupRegions.php This takes anything output before the opening `<!DOCTYPE` and connects it to the right places * within the `<html>` that comes after it. /** * Identify and populate markup regions in given HTML * * To use this, you must set `$config->useMarkupRegions = true;` in your /site/config.php file. * In the future it may be enabled by default for any templates with text/html content-type. * * This takes anything output before the opening `<!DOCTYPE` and connects it to the right places * within the `<html>` that comes after it. For instance, if there's a `<div id='content'>` in the * document, then a #content element output prior to the doctype will replace it during page render. * This enables one to use delayed output as if it’s direct output. It also makes every HTML element * in the output with an “id” attribute a region that can be populated from any template file. It’s * a good pairing with a `$config->appendTemplateFile` that contains the main markup and region * definitions, though can be used with or without it. * * Beyond replacement of elements, append, prepend, insert before, insert after, and remove are also * supported via “pw-” prefix attributes that you can add. The attributes do not appear in the final output * markup. When performing replacements or modifications to elements, PW will merge the attributes * so that attributes present in the final output are present, plus any that were added by the markup * regions. See the examples for more details. * * Examples * ======== * Below are some examples. Note that “main” is used as an example “id” attribute of an element that * appears in the main document markup, and the examples below focus on manipulating it. The examples * assume there is a `<div id=main>` in the _main.php file (appendTemplateFile), and the lines in the * examples would be output from a template file, which manipulates what would ultimately be output * when the page is rendered. * * In the examples, a “pw-id” or “data-pw-id” attribute may be used instead of an “id” attribute, when * or if preferred. In addition, any “pw-” attribute may be specified as a “data-pw-” attribute if you * prefer it. * ~~~~~~ * Replacing and removing elements * * <div id='main'>This replaces the #main div and merges any attributes</div> * <div pw-replace='main'>This does the same as above</div> * <div id='main' pw-replace>This does the same as above</div> * <div pw-remove='main'>This removes the #main div</div> * <div id='main' pw-remove>This removes the #main div (same as above)</div> * * Prepending and appending elements * * <div id='main' class='pw-prepend'><p>This prepends #main with this p tag</p></div> * <p pw-prepend='main'>This does the same as above</p> * <div id='main' pw-append><p>This appends #main with this p tag</p></div> * <p pw-append='main'>Removes the #main div</p> * * Modifying attributes on an existing element * * <div id='main' class='bar' pw-prepend><p>This prepends #main and adds "bar" class to main</p></div> * <div id='main' class='foo' pw-append><p>This appends #main and adds a "foo" class to #main</p></div> * <div id='main' title='hello' pw-append>Appends #main with this text + adds title attribute to #main</div> * <div id='main' class='-baz' pw-append>Appends #main with this text + removes class “baz” from #main</div> * * Inserting new elements * * <h2 pw-before='main'>This adds an h2 headline with this text before #main</h2> * <footer pw-after='main'><p>This adds a footer element with this text after #main</p></footer> * <div pw-append='main' class='foo'>This appends a div.foo to #main with this text</div> * <div pw-prepend='main' class='bar'>This prepends a div.bar to #main with this text</div> * * ~~~~~~ * * @param string $htmlDocument Document to populate regions to * @param string|array $htmlRegions Markup containing regions (or regions array from a find call) * @param array $options Options to modify behavior: * - `useClassActions` (bool): Allow "pw-*" actions to be specified in class names? Per original/legacy spec. (default=false) * @return int Number of updates made to $htmlDocument * */ public function populate(&$htmlDocument, $htmlRegions, array $options = array()) { ... }
  23. Let me introduce you: https://processwire.com/blog/posts/introducing-tracy-debugger/
  24. You can implement it anyway you like, I was just introducing a few api methods that I use quite often. I like being DRY with my code but with templates, I lean towards using template files and wireRenderFile() function over basic functions if the markup does not include too much logic https://processwire.com/blog/posts/processwire-2.5.2/#new-wirerenderfile-and-wireincludefile-functions Skim over api methods once or twice to see what's available to you, you'll appreciate PW once more. So much good stuff is buried in the docs (and blog posts)
  25. Latest. v3.0.75
×
×
  • Create New...