Jump to content

Kay Lohn

Members
  • Posts

    14
  • Joined

  • Last visited

About Kay Lohn

  • Birthday August 28

Profile Information

  • Gender
    Male
  • Location
    Pakistan
  • Interests
    Many!

Recent Profile Visitors

1,851 profile views

Kay Lohn's Achievements

Jr. Member

Jr. Member (3/6)

5

Reputation

2

Community Answers

  1. Hi, Wonderful module, the core should have the ability to put the site offline. Anyways, I had a slight problem with this module. Please refer to this topic/thread. Thanks. https://processwire.com/talk/topic/8822-session-redirect-problem/
  2. UPDATE: Fortunately, found out the reason. The module 'MaintenanceMode' was the culprit. The pages worked as expected once the module was uninstalled. To double check I re-installed the module again and same problem reappeared. On further investigation the redirect in 'MaintenanceMode' module was set to the 'blocked' page which caused the problem which when changed to some other page, the 'blocked' pages started to redirect as expected. UPDATE: Unfortunately, I had to uninstall the module for the reason that the page I set as 'blocked' would redirect to home when accessed directly, something which I do not want to happen.
  3. Hi, I have a checkbox field called 'blocked' on pages using 'my-page-template' which is used to check if the page is blocked for 'guest' users. Whenever those pages are accessed by guests then a simple check in the template file would redirect them to another page. I have a page named 'blocked' using template 'blocked-page' to which the guests are redirected. The 'my-page-template.php' has the code at top. <?php if ($page->blocked == 1 && $user->isGuest()) { $session->redirect($pages->get('/blocked/')->url); } ?> <h1>Welcome</h1> <p>This is my website</p> Now, when a 'blocked' page is accessed the redirect goes straight to homepage. In Chrome/Firefox Developer tools > Network the output/headers are as follows: http://mysite.com/pages/somepage > GET > 301 > Initiator: Other http://mysite.com/pages/somepage > GET > 302 > Initiator: http://mysite.com/pages/somepage http://mysite.com/blocked > GET > 302 > Initiator: http://mysite.com/blocked The page redirects to http://mysite.com/ instead of http://mysite.com/blocked The 'blocked-page' template does not have any redirect code. Content below: <?php echo '<h1>This page is blocked</h2>; ?> What am I missing? Would deeply appreciate some help/ pointers. Thanks.
  4. UPDATE: Fortunately, I have traced the problem. (I recalled having a similar problem on MODx a few years back) ALL of the pages which I created on local XAMPP install and migrated to the live site would throw a 403 Forbidden on save as well as pages which had content that matched the ModSecurity filters/ rules on the server. It does not like the brackets <> I guess. The pages I created on the live site had fields which were to have HTML code but were left empty, hence no error was thrown and the page was saved gracefully. Error log below: [sat Jan 17 13:13:33 2015] [error] [client xxx.xxx.xxx.xxx] ModSecurity: Access denied with code 403 (phase 2). Pattern match "(< ?(?:script|about|applet|activex|chrome).*(?:script|about|applet|activex|chrome) ?>|> ?< ?(img ?src|a ?href) ?= ?(ht|f)tps?:/|" ?> ?<|" ?[a-z]+ ?<.*>|> ?"? ?(>|<)|< ?/?i?frame|\\%env)" ... [uri "/xxxx/page/edit/"] I have a few text fields which are supposed to accept HTML code and output it as is but my host does not like it being saved to the database un-escaped. This issue has been traced and is fixed for now by revisiting the template files PHP code and outputting the data some other way OR manually editing ALL offending entries in the database.....hmph BTW, my host uses HSPHERE/PARALLELS and hides its Apache version
  5. Hi, After migrating a site built on PW to a live server, when trying to edit and save the home page, it throws a 403 Forbidden error. I have tried creating a page under home and editing/saving it, it works fine and so do any other page BUT Home. Some things I have tried/ did: Setting permission on site/assets to 777 (recursive) Setting permission on site/modules to 777 (recursive) Deleting content under site/assets/sessions Removing unused fields from homepage Created a new 'superuser' account, logged in but still the 403 Forbidden *edit* Same 403 Forbidden on Firefox, Chrome and Opera *edit* Changed theme to Reno and Default, still the same *edit* Have installed module MaintenanceMode after migration, uninstalled, re-installed, no effect *edit* Had the '......request seems to be forged' which was fixed by setting permissions to 777 on site/assets (recursive) *edit* The hosting provider does not allow PHP Zip extension, so installed 'MaintenanceMode' manually *edit* Setting Debug to true has no effect, no errors outputted *edit* Using ProcessWire 2.5.2 Any help would be highly appreciated. Thanks.
  6. A simple function to get the grandchildren of a page (0 to 2 levels deep): (The code below is just an example, be-careful, it does not filter results and would return pages you would not want but can easily be extended to include filtering) <?php /* * Function to get 0 to 2 levels deep page children (returns results from 'all' pages) * Home (Depth 0) * |-Level 1 (Depth 1) * |-Level 2 (Depth 2) */ function getChildren($parent, $depth) { if ($depth > 2 || $depth < 0) throw new Exception ('Depth invalid. Should be 0 or <=2.'); $include = 'all'; $parent = wire('pages')->get("{$parent}"); $level1children = $parent->children("parent={$parent}, include={$include}"); $level2children = $parent->find("parent={$level1children}, include={$include}"); switch ($depth){ case '0': return array($parent); break; case '1': return $level1children; break; case '2': return $level2children; break; } } ?> Going overboard (0 to 5 levels deep) <?php /* * Function to get 0 to 5 levels deep page children (returns results from 'all' pages) * Home (Depth 0) * |-Level 1 (Depth 1) * |-Level 2 (Depth 2) * |-Level 3 (Depth 3) * |-Level 4 (Depth 4) * |-Level 5 (Depth 5) */ function getChildren($parent, $depth) { if ($depth > 5 || $depth < 0) throw new Exception ('Depth invalid. Should be 0 or <=5.'); $include = 'all'; $parent = wire('pages')->get("{$parent}"); $level1children = $parent->children("parent={$parent}, include={$include}"); $level2children = $parent->find("parent={$level1children}, include={$include}"); $level3children = $parent->find("parent={$level2children}, include={$include}"); $level4children = $parent->find("parent={$level3children}, include={$include}"); $level5children = $parent->find("parent={$level4children}, include={$include}"); switch ($depth){ case '0': return array($parent); break; case '1': return $level1children; break; case '2': return $level2children; break; case '3': return $level3children; break; case '4': return $level4children; break; case '5': return $level5children; break; } } ?> The function above can easily be extended to filter results. Hope this would prove useful. @Soma: Thank you for your valuable inputs Yes, you are right. The find() function is right there and it can be used directly, but then I'd have to write long long lines each time I need some grandchildren or children or great grandchildren etc. Comparing this: <?php $parent_page = wire('pages')->get('/projects/'); foreach ($pages->find("parent={$parent_page->children}, include=all") as $child) { ?> echo '<li>'.$child->title.'</li>'; <?php }; ?> to this: <?php $gc = getChildren('/projects/', '2'); foreach ($gc as $child) { echo '<li>'.$child->title.'</li>'; } ?> It's obvious how a function in some cases can help ease things up a bit. Building this function to get children etc had proved extremely useful in building my project as I have to deal with pages several levels deep. I hope this help others too. @Ivan: No problem, thanks, I still have your function in my _functions.php file, it never hurts to have two in the arsenal @pwired: Thanks for the 'Like'. Gracias! Regards use of the above experimental function, what would be the overhead of using it on system resources, site speed, performance etc on a live site?
  7. Think I found a solution to the problem, thanks to this reply from ryan. To get the grandchildren of a specific page, here is what I did: <?php // Get a specific set of grandchildren pages under Projects page $parent_page = wire('pages')->get('/projects/'); // Define a variable and the parent page whose grandchildren you want to get, here I need it from the page 'projects' hence get('/projects/') foreach ($pages->find("template=photo_page_template, parent={$parent_page->children}, include=all, is_featured=1, sort=-date, limit=3") as $child) { // This part parent={$parent_page->children} is what does the magic here ?> <a href="<?php echo $child->link_to_page->url; ?>"> <img src="<?php echo $child->my_image->url; ?>" alt="<?php echo $child->image_desc; ?>"/> </a> <?php }; ?> It works! Tested with newly created 'Events' page in my project structured as follows: Home |- Projects // name=projects |- Nature |- Featured Image // Hidden & Unpublished, has a checkbox 'featured', checked |- Another Image // Hidden & Unpublished, has a checkbox 'featured', unchecked |- Another Image //Hidden & Unpublished, has a checkbox 'featured', unchecked |- People |- Featured Image // Hidden & Unpublished, has a checkbox 'featured', checked |- Another Image // Hidden & Unpublished, has a checkbox 'featured', unchecked . . . |- Events // name=events |- Photography 101 |- Featured Image // Hidden & Unpublished, has a checkbox 'featured', checked |- Another Image // Hidden & Unpublished, has a checkbox 'featured', unchecked |- Another Image //Hidden & Unpublished, has a checkbox 'featured', unchecked |- Advanced Photography |- Featured Image // Hidden & Unpublished, has a checkbox 'featured', checked |- Another Image // Hidden & Unpublished, has a checkbox 'featured', unchecked . . . By changing: $parent_page = wire('pages')->get('/projects/'); to $parent_page = wire('pages')->get('/events/'); only the results from the 'events' page were returned I have worked up a rough function to get the grandchildren of a page, it works perfectly in my case, excuse the code-smell. Usage in comments. <?php /* * Function to get grandchildren of any page * * Required arguments are $parent_page and $template_name * * NoteToSelf: REMEMBER: This function returns grandchildren of a page a.k.a children of children, so * if you specify '/' as parent and 'some_template' as template, it will return an array of 2nd level pages * i.e. Home->sub-page->sub-page * * Argument passing example: * For $parent_page pass as '/' for Home/root or '/somepage/' for some other page * For $template_name pass as 'template_name' * [OPTIONAL] For $include pass as 'all' or 'hidden' etc [read ProcessWire Docs for more] * [OPTIONAL] For $sort pass as 'date' or '-date' etc [read ProcessWire Docs for more] * [OPTIONAL] For $limit pass as '0' or '10' or '999999999' etc [read ProcessWire Docs for more] * [OPTIONAL] For $custom_field_check pass as 'field=value' or 'is_featured=1' etc [read ProcessWire Docs for more] * * To skip optional arguments pass null * * Usage example using null: * * $gc = getGrandChildren('/', 'my_template', null, '-date', '3', null); * if ($gc) { * foreach ($gc as $child) { * echo $child->title . '<br />' . $child->name . '<br />'; * } * } * * Usage example using all arguments passed to the function: * * $gc = getGrandChildren('/projects/', 'photo_page_template', 'all', '-date', '3', 'is_featured=1'); * if ($gc) { * foreach ($gc as $child) { * echo $child->title . '<br />' . $child->name . '<br />'; * } * } * * Kay Lohn | https://processwire.com/talk/user/2149-kay-lohn/ * */ function getGrandChildren($parent_page, $template_name, $include = null, $sort = null, $limit = null, $custom_field_check = null) { // Declare variables $parent_page = wire('pages')->get("{$parent_page}"); // Initialize variable as a new PageArray() $grandchildren = new PageArray(); // Initialize a blank variable to store query $query = ""; // Check and process required values if (!$template_name) // Throw exception if not present throw new Exception('Template name is required'); if ($template_name) // Add to $query if present $query .= 'template=' . $template_name; if (!$parent_page) // Throw exception if not present throw new Exception('Parent Page is required'); if ($parent_page) // Add to $query if present $query .= ', parent=' . $parent_page->children; // Check and process Optional values if ($include) // Add to $query if present $query .= ', include=' . $include; if ($sort) // Add to $query if present $query .= ', sort=' . $sort; if ($limit) // Add to $query if present $query .= ', limit=' . $limit; if ($custom_field_check) // Add to $query if present $query .= ', ' . $custom_field_check; // Run loop foreach (wire('pages')->find($query) as $result) { $grandchildren->add($result); // Add each result to PageArray } // Experimental if (!empty($grandchildren)) { return $grandchildren; // Return the page array } else { throw new Exception('No results :('); // Throw an error if PageArray was empty } } ?> I hope this proves useful.
  8. Yes you are right, the code below fails to give the expected results. <?php foreach ($pages->find("template=photo_page_template, parent=/events/, include=all, is_featured=1, sort=-date, limit=3") as $child) { ?> // Code returns the direct children of page 'events' NOT the grandchildren Your provided function gives an error: Missing argument 2 for getAllDescendants(), called in ... _functions.php on line ... and defined My function call is: $children = getAllDescendants($pages->get('/events/'))->find("template=photo_page_template, parent=/events/, include=all, is_featured=1, sort=-date, limit=3"); Your function as in my _functions.php file: function getAllDescendants ($root, $prependRoot) { $descentants = new PageArray(); foreach ($root->children() as $child) { $descentants->add($child); if ($child->numChildren()) { $descentants->add(getAllDescendants($child)); } } if ($prependRoot) $descentants->prepend($root); return $descentants; } What value should be passed for $prependRoot? What am I doing wrong? *stumped*
  9. @Macrura & @Ivan: Thank you very much for your kind help and support. It is highly appreciated. I have made the template without the php file merely as a placeholder for related fields *edit/* and a lot of additional fields for data besides the image field, */edit* and most of those are reused (ProcessWire News & Updates (November 18) :: Using ProcessWire fields efficiently) elsewhere on the site too. Also, I have not published the pages to avoid a 404 error (it would throw a 404 anyways as the templates does not have a php file associated, I just liked the strike-out, it gives me the satisfaction/ visual clue that these are just for internal use and private) and set hidden to avoid them getting listed in searches etc Your code works but is not specific to a page, I have taken the liberty to incorporate it into @Ivan's code as below: This piece of code below worked magic: <?php foreach ($pages->find("template=photo_page_template, include=all, is_featured=1, sort=-date, limit=3") as $child) { ?> // include=all because I have these pages unpublished AND hidden as include=hidden would not return the set of pages that are unpublished From what I understood from the code line above in essence, is that, I am asking ProcessWire/ Wire/ API to (kindly correct me if I am wrong) 'find me a set of three pages from all pages on the wire including hidden and unpublished pages whose template is photo_page_template and its is_featured value is true, sort them by latest first by date and return me the set/ results as a PageArray and then iterate over the array and give me each element as a variable, $child, so I can get the field values I want from that page' So far EXCELLENT! The code works! But... the problem with above is that it is not specific to a page whose children or grandchildren I require. Currently in my project I do not yet have any other pages under root (Home) except Projects, which have children AND/OR children of its children. What if there was another page or pages for e.g. let's say, for one, Events under root (Home) which had children just like the Projects page and had featured items but none of the featured from it are needed on the homepage? I have not yet tried it out but do you think by adding parent=/events/ (assuming the name of the page as events) would give me the proper required set as in the code below? <?php foreach ($pages->find("template=photo_page_template, parent=/events/, include=all, is_featured=1, sort=-date, limit=3") as $child) { ?> // trying to get a specific set of pages under Events page Thanks a lot Ivan for this. I haven't used it yet but will surely try it out and let you know. BTW, where can I find the _functions.php file? Thank you for the Delayed Output link, I have already adopted the strategy to some extent (to some extent because at times i need a quick and dirty output or I just feel lazy to add it to my includes) and adapted/ improvised in my current project, I will be adding your function to my own custom helper functions 'inc' file, it might come in handy. Once again, thank you very much for your kind help and support.
  10. Hi, I would highly appreciate some help with selecting/ displaying grandchildren or nested pages My page structure is: Home |- Projects |- Nature |- Featured Image // Hidden & Unpublished, has a checkbox 'featured', checked |- Another Image // Hidden & Unpublished, has a checkbox 'featured', unchecked |- Another Image //Hidden & Unpublished, has a checkbox 'featured', unchecked |- People |- Featured Image // Hidden & Unpublished, has a checkbox 'featured', checked |- Another Image // Hidden & Unpublished, has a checkbox 'featured', unchecked |- Another Image //Hidden & Unpublished, has a checkbox 'featured', unchecked |- Another Image // Hidden & Unpublished, has a checkbox 'featured', unchecked |- Another Image //Hidden & Unpublished, has a checkbox 'featured', unchecked |- Animals |- Featured Image // Hidden & Unpublished, has a checkbox 'featured', checked |- Another Image // Hidden & Unpublished, has a checkbox 'featured', unchecked |- Another Image //Hidden & Unpublished, has a checkbox 'featured', unchecked ...... and so on ... ...... |- About |- Contact I am having problems displaying recent 3 projects' Featured Images on the home page. <?php $parent_page = wire('pages')->get('/projects/'); // Have used $pages->get('/projects/'); too $parent_page_children = $parent_page->children(); Debugger::dump($parent_page_children); // Debugger::dump($parent_page_children); (Using Tracy) dumps 1 PageArray foreach ($parent_page_children as $children) { Debugger::dump($children); // Debugger::dump($children); (Using Tracy) dumps 6 pages if ($children->numChildren) { foreach ($children->children("is_featured=1, include=all, sort=-date, limit=3") as $child) { Debugger::dump($child); // Debugger::dump($child); (Using Tracy) dumps 6 pages, currently 6 checked as Featured (Need only three) ?> <a href="<?php echo $child->link_to_page->url; ?>"> <img src="<?php echo $child->my_image->url; ?>" alt="<?php echo $child->image_desc; ?>"/> </a> <?php }; }; }; ?> Currently I have 6 pages with featured checkbox checked for testing. The code above displays all 6 instead of limiting to recent 3 sorted by -date.. How can I get recent three to display on my homepage? Again, any help would be highly appreciated. Thanks.
  11. No. This doesn't work. Sorry, I omitted the PHP open/ close tags in my original post. The code is there for reference only. Thanks for the pointer, I'll add the tags to the Original post to avoid confusion.
  12. UPDATE: For the Home page I managed to get 3 unsorted results displayed from the Projects page (I am following the example of a photography project website as in my previous post): <?php $projects = $pages->get('/projects/')->my_items->getArray(); foreach ($projects as $key => $project) { ?> <div class="item"> <img src="<?php echo $project->my_image->url; ?>" /> <a href="<?php echo $project->my_link->name; ?>">View</a> </div> <?php if ($key == 2) { break;} }; ?> - How do I sort $projects array by date? -The 'Projects' should be recent three. - How do I limit get/ array? - If there be 1000 projects, the code would unnecessarily get an array of 1000 items whereas it only needs three items from the Projects page. - Is there a better way to do this? Any help would be highly appreciated. Thanks.
  13. Hi, Can someone please help me with the scenario below, I am trying to build a Project portfolio sort of a site. I have a homepage which displays three recent items from a Repeater field named my_items (3 RepeaterReady fields are created for the home page and filled with data), example code below: <?php foreach ($page->my_items as $item) { ?> // display item values, for example: <div class="item"> <img src="<?php echo $item->my_image->url; ?>" /> <a href="<?php echo $item->my_link->name; ?>">View</a> </div> <?php }; ?> I have a page called my-items with repeater 'my_items' field for displaying the same as above. The page 'my-items' has children, each of which have 'my_items' Repeater field for displaying data respective to that page using the same code as above. The page/ site hierarchy is like: Home - uses Repeater 'my_items' |- My Items - uses Repeater 'my_items' |- Item 1 - uses Repeater 'my_items' |- Item 2 - uses Repeater 'my_items' |- Item 3 - uses Repeater 'my_items' |- ... |- My Items 2 |- Item 1 - uses Repeater 'my_items' |- Item 2 - uses Repeater 'my_items' |- Item 3 - uses Repeater 'my_items' |- ... and so on. What I am doing as a starter with PW is: - Entering same data for in Repeater for Home for some item, lets call this 'A'. - Entering the same for My Items the data for 'A' - Entering the same for Item 1 the data for 'A' Therefore, I am adding 'A' thrice in three places, violating the DRY principle. What I have tried and failed is: - To get 'A' to display in My Items page fetched from Item 1 and then, - the Home to display 'A' fetched from My Items so that I only enter data for 'A' in Item 1 and not in every page that displays it. Caveats: - The homepage displays 3 recent only from page My Items - Page My Items displays one from each children - Item 1 has multiple Repeater blocks/ pages for displaying data (e.g. My Item #1, My Item #2, My Item #3 .......... My Item #n) Another way I think to explain it would be a photography site with structure as: Home - display 3 recent photo projects |- Projects - displays each child project with the 1st photo and its description |- Nature - Has many photos with description |- Animals - Has many photos with description |- People - Has many photos with description I hope I had explained properly. Any help or suggestion would be highly appreciated.
  14. To ProcessWire or Not to ProcessWire? That is the question.

  15. Running into exactly the same problem. Deleting the ModuleManager.cache file did not help neither did removing and re-installing. Developing locally on XAMPP on Windows 7.
×
×
  • Create New...