Jump to content

Soma

Moderators
  • Posts

    6,808
  • Joined

  • Last visited

  • Days Won

    159

Everything posted by Soma

  1. It also depends if output formatting for the pages is on or off. If it's OFF a $page->field may be an object or array but when ON the same might be a string or a single object instead of an array. Good luck.
  2. After all you can only fill an array with strings ro integers if you want to use array_count_values() later on. So if you deal with object or arrays for values you have to maybe (string) cast the value as you already found out. Then as Martijn points out the toString method comes into play and images or page arrays will end up like this as string: ["myimage1.jpg|someother.jpg"] or ["1203|1239|1233"] But then I don't know what suites in your case and what you really need, but you'll find out I'm sure.
  3. No it's returning array with values, not a page. PageArray::explode() return array with the values of the requested properties of all pages in the page array. Maybe aswell look it up what it does, if explaining and showing it here isn't enough for you. https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/core/WireArray.php#L1423 Yes PageArray is an array with object, but I looks like you don't get it I'm looping page array. In there I loop fields array and fill an array with $array[$field][$p->$field] Translates to array["fieldnameA"]["value1"] -> count array["fieldnameA"]["value2"] -> count $p->$field is returning the value of the field. $field is a variable of the fieldname so it's like $p->fieldnameA But in your case it seems you deal with fields of type page so the field value is an object if you will so Filling array keys with objects or array leads to illegal offsets.
  4. All my codes count distinct VALUES of those fields specified and not the field name. But you changed something there to $somePages->explode('name') and name would be the page names values, but you set them to the [$field] array so you mess things up. I don't see the need to add (string) as if you deal with strings there's no problem, but I guess you deal with objects or array as $p->$field and that's not allowed. After all I think you just need to adapt code to your needs and I provided various pseudo examples I think you can learn something. It's not meant to work for you as copy paste.
  5. WireArray::explode() is not to count values but to create an array of PageArray, much like the php explode(), array_count_values() is the one only working with strings ans integers. WireArray::explode() doesn't care if it's a string or integer. Page fields or file fields, any multiple value fields are objects or arrays.
  6. Well if you see this warning, then it's not a string or integer. It won't work with page fields, images fields etc.
  7. Ah only now I see the while(true) loop... but that is a cronjob? It runs forever and calls it all the time? Strange thing you do here.
  8. The echo should be in "..." not '...'
  9. Ah and just remembered that since PW 2.4 the simplest way to count values of certain keys/fields within a PageArray/WireArray is using the new WireAarray::explode() - combined with using PHP's array_count_values(array) you already used, it's down to this: $someFields = array("province", "network"); $somePages = $pages->find("template=child-template, category=xxx"); $resultArray = array(); foreach($someFields as $field) { $resultArray[$field] = array_count_values($somePages->explode($field)); } // that's it show array result foreach($resultArray as $field => $values) { foreach($values as $value => $count) { echo "<p>$field - $value: $count</p>"; } }
  10. To get something as a function, just for learning purposes, the code could be like this function getDistinctValues(PageArray $pageArray, array $fields){ $arr = array(); foreach($pageArray as $p) foreach($fields as $field) if(isset($resultArray[$field][$p->$field])) $arr[$field][$p->$field]++; else $arr[$field][$p->$field] = 1; return $arr; } $someFields = array("title", "headline"); $somePages = $pages->find("template=basic-page"); $resultArray = getDistinctValues($somePages, $someFields); foreach($resultArray as $field => $values) foreach($values as $value => $count) echo "<p>$field - $value: $count</p>"; And following is a way to add a new method to a PageArray using a method hook. // this is right in a template code, but could also be in a autoload helper module wire()->addHook("PageArray::getDistinctValues", null, function(HookEvent $event){ $pageArray = $event->object; // the page array getDistinctValues($fieldsArr) is called on $fieldsArray = $event->arguments(0); // the first argument $arr = array(); foreach($pageArray as $p) foreach($fieldsArray as $field) if(isset($arr[$field][$p->$field])) $arr[$field][$p->$field]++; else $arr[$field][$p->$field] = 1; $event->return = $arr; }); $someFields = array("title", "headline"); // And now you can do this on a page find result $resultArray = $pages->find("template=child-template")->getDistinctValues($someFields); foreach($resultArray as $field => $values) foreach($values as $value => $count) echo "<p>$field - $value: $count</p>";
  11. Hmm, I see. Now it's a little clearer. But how do you know all the values those fields can have? This is a critical point. If not known, you theres one option to loop all those pages and collect field -> values, while the nested array with the same values as the key will get incremented instead of added. I think the fields (their names) at least is something to be known. $someFields = array("title", "headline"); $somePages = $pages->find("template=basic-page, category=phones"); // just something $resultArray = array(); foreach($somePages as $p) foreach($someFields as $field) if(isset($resultArray[$field][$p->$field])) $resultArray[$field][$p->$field]++; else $resultArray[$field][$p->$field] = 1; print_r($resultArray); foreach($resultArray as $field => $values) foreach($values as $value => $count) echo "<p>$field - $value: $count</p>"; The amount of field values is the one of unique ones, while their values is the count found. There's no PW distinct selector for fields unfortunately to count pages like that without knowing all the values, though there would be ways to do it with raw sql query for sure. But then you'd have to account for a couple things like is the page published and such. This of course can be a a heavy method once you need to loop thousands of pages on every page request. So always good to be careful and try to find way to minimize the load. There's also ways to cache snippets and store things to pages to load from and update them frequently using cronjob or hooks.
  12. Looks good to me to uncacheAll pages to make sure you get newly fetched page. But I'm surprised this is needed at all here. But then I think I maybe don't really understand all context and what uncacheAll() does really. Are pages cached across other processes/requests and scripts?
  13. If you have the LanguageSupportPageNames module (looks like) installed, you can configure the /en/ part on the root page name for each language.
  14. Use parents() or closest(). If those color pages have a own template use that in the selector for closest() or parents(). Or parents()->eq(n) where n is the level of color pages.
  15. Even after reading your posts multiple times I don't understand a single thing. I doubt you'll find someone understanding what you want to do. Too abstract and not a comprehent case. After all it may help to understand if you actually tell the guys here what you need to achieve, rather than I need count field01 => valueX. It could be that you go completely wrong about doing something.
  16. I'm also in with a little inspection and think there might be solider ways to do it. But this is now sponsored?
  17. Nope there isn't. These days we don't use captcha's since a long time. What is there already in InputfieldForm is a CSRF token validation. For spam prevention we use a honeypot or a reverse honeypot.
  18. $user->isLoggedin() shouldn't be necessary here as $page->editable() already checks for various permission etc. So maybe do you have the guest role given page edit access? What happens if you click the link? Well I'm waiting for a facepalm here cause it's not possible.
  19. $field->attr("disabled", "disabled");
  20. Nope. No idea. Is it cached? Although it wouldn't get cached when logged in. Any third party modules modifying Page::editable?
  21. @choppingblock You surely don't want to modify anything in there. I'm not sure where the subdomain is coming from. Do you have a $config->httpHosts with the subdomain in your config.php? Also not sure what you want to modify? It's all there [domain - Edit Page : pagetitle]
  22. What diogo said. If you really need a fieldtype to do ajax internally you best create a hidden admin page with a process module to handle all that. Not overkill but a well streamlined approach, and makes it solid and easy, also the admin URL that way is sure to be unique throughout the system. Gives an interface that is well defined. Process pages are meant for that and PW uses it also in some places like Page tree and fieldtypes. If you will you can of course always catch and bypass the requested url in a module init. Something like public function init() { $adminUrl = ltrim($this->config->urls->admin, "/"); if($this->input->get->it == $adminUrl . "testmodule/load"){ echo $this->executeLoad(); exit; } } protected function executeLoad(){ $id = $this->input->get->id; return $this->pages->get((int)$id)->title; } And a js ajax request might look like $(function(){ $.ajax({ type: 'get', url: "/processwire/testmodule/load", data: "id=1001", success: function(data){ alert(data); } }); }); But I don't know if that's really a good idea at all. Just because you can doesn't mean it's good.
  23. You would use a after Page::deleteReady hook to fetch the page that's going to be deleted. This hook is called when page is deletable and actually WILL get deleted right after. $this->addHook("Class:method", $this, "someMethod"); is to add a new method to a class with a hook. You would give it addHookAfter or addHookBefore.
  24. has_parent isn't supported for runtime filtering with a path, not("selector") is filtering the results from the returned WireArray, there "has_parent" isn't supported in the same way as in a find selector. There's a path is converted to the ID before running the query. Regarding performance and such your second example is the way to go.
  25. CSV import doesn't require a csv file, you can simply use the manual textfield ... enter title like by line... I found to be the fastest way to create pages.
×
×
  • Create New...