Jump to content

bernhard

Members
  • Posts

    6,671
  • Joined

  • Last visited

  • Days Won

    366

Everything posted by bernhard

  1. If you hook the render() method, the $event->return contains the markup. Therefore you need to modify $event->return to modify the markup:
  2. Glad you solved it. Beside what I've already mentioned regarding isProductTemplate I have another suggestion: I think you could avoid neting IFs and make the code even more readable: public function checkSKUUnique(HookEvent $event) { $snipwire = $this->wire('snipwire'); if (!$snipwire) return; // only check snip products $page = $event->arguments(0); if(!$page->isSnipProduct) return; // exit if no changes if(!$page->isChanged('snipcart_item_id')) return; // don't check unsaved pages $field = $page->getField('snipcart_item_id'); $sku = $page->snipcart_item_id; // SKU field value if(!$sku) return; // exit if value is unique $exists = $this->wire('pages')->get([ ['snipcart_item_id', '=', $sku], ['id', '!=', $page], ['status', '<', Page::statusTrash], ]); if(!$exists->id) return; // value is not unique, show errors $error = $this->_('SKU must be unique'); $page->getInputfield($field)->error($error); $exception = sprintf($this->_('SKU [%s] is already in use'), $sku); throw new WireException($exception); // Prevent saving of non-unique value! }
  3. This works for me: // To test, add this to your sites/ready.php wire()->addHookAfter('Pages::saveReady', 'checkiconUnique'); function checkiconUnique(HookEvent $event) { $page = $event->arguments(0); $field = $page->getField('icon'); $icon = $page->icon; // preset icon // before the page is saved for the first time it does not have an id if(!$page->id) { $page->icon = "Preset icon " . uniqid(); } else { if(!$page->isChanged('icon')) return; $exists = wire('pages')->get("id!=$page,icon=$icon"); if($exists->id) { // value is not unique! $error = __('icon must be unique'); $exception = sprintf( __('icon [%s] is already in use'), $icon ); $inputfield = $page->getInputfield($field); $inputfield->error($error); throw new WireException($exception); // Prevent saving of non-unique value! } } } Not sure if that works in all circumstances - that's why we build modules for such things. And not sure if a module by Ryan is really a 3rd party module that one should not include in his module ? But maybe it helps nonetheless... I tested it with an "icon" field. You can simple find&replace that string to your fieldname. I made all occurrences lowercase to make that easier.
  4. Hi @Gadgetto the screencast was meant to show that I did search for the "unique" feature in the blog but didn't find relevant results ? It was not meant as something useful for your problem. To your problem... Such things are a little hard when one is not directly in the code! Could you please setup two example hooks for the BASIC-PAGE template and the TITLE field. Almost everybody has those available for testing so we could just copy&paste your hooks into site/ready.php and provide tested solutions. One thing that I saw in your code that could be slightly improved: // your version: if ($snipwire->isProductTemplate($page->template)) { ... // better: if($page->isSnipProduct) { ... $wire->addHookProperty("Page::isSnipProduct", function($event) { $snipwire = ... $event->return = $snipwire->isProductTemplate($event->object->template); } That's just a little detail but IMHO overall that makes code more readable... Another option would be to use the new Page Classes, but I guess that's no option for you because you because of the version requirement ?
  5. Ugh... that was really ugly ? Here's a better version: In your autoload module: $this->addHookProperty("User::isCustomer", function(HookEvent $event) { $event->return = $this->user->isSuperuser(); if($this->user->hasRole('manager')) $event->return = true; if($this->user->hasRole('customer')) $event->return = true; }); $this->addHookProperty("User::isManager", function(HookEvent $event) { $event->return = $this->user->isSuperuser(); if($this->user->hasRole('manager')) $event->return = true; }); In your processModule: public function checkAccess() { // if user is neither customer nor manager we redirect if(!$this->user->isCustomer AND !$this->user->isManager) { $this->session->redirect('/your/admin/url/to/no-access-page'); return; } } public function executeDNS { $this->checkAccess(); $user = $this->wire('user'); $out = "<div>Hello $user, here are your dns settings...</div>"; if($user->isManager) $out .= "<div>You are a Manager, so you can edit all settings!</div>"; ... return $out; } A lot better. Welcome to PW greatness ?
  6. Hey @adrian I've just started using live.js because I'm doing some work in the backend where I need JS/LESS a lot. After getting tired of reloading the browser I thought I'd try browsersync that I recently discovered but it did not work. Then I found live.js and added this to my module: if(!$this->user->isSuperuser()) return; if(!$this->config->debug) return; $this->config->scripts->add('http://livejs.com/live.js'); And this just works!! The great thing about it is that it even recognizes when my style.less file changed. The great thing here is that it does not reload the browser but only replaces the CSS file (which is crazy because the CSS is parsed via RockLESS on demand, so the server must somehow be involved. Anyway... the style gets updated without reloading the page as soon as I change my LESS file ? I thought this would be very easy to include in Tracy. For the frontend it would need to add the whole script tag instead of doing $config->styles->add(), but that would also be simple. What do you think? Edit: I understand now how the less-part works. On every poll of live.js the server is involved, so live.js results in lots of requests to your server! I don't think that's a very good option...
  7. I knew there was something added lately, but then I didn't find it and thought maybe I was dreaming ?
  8. Hi, @AswinC and welcome to the forum! Sounds like a fun project ? I'd suggest something like this Permissions: dns-view dns-edit licensing-view licensing-edit Roles: customer (dns-view, licensing-view) manager (dns-edit, licensing-edit) Then you build ProcessModules for those management interfaces and simply check for the role: public function checkAccess() { // $su is true for superuser $su = $this->user->isSuperuser(); // set user info object // for superusers all properties will be true $u = (object)[ 'isCustomer' => $su ?: $this->user->hasRole('customer'), 'isManager' => $su ?: $this->user->hasRole('manager'), ]; // if user is neither customer nor manager we redirect if(!$u->isCustomer AND !$u->isManager) { $this->session->redirect('/your/admin/url/to/no-access-page'); return; } // user has access, return info object return $u; } public function executeDNS { $u = $this->checkAccess(); $user = $this->wire('user'); $out = "<div>Hello $user, here are your dns settings...</div>"; if($u->isManager) $out .= "<div>You are a Manager, so you can edit all settings!</div>"; ... return $out; } That was really quickly typed here in the browser. Maybe even more elegant would be to add $user->isCustomer and $user->isManager to your user object in an autoload module, then you'd have it available in all your API ? See You can then also prevent editing of pages via simple hooks that check if the user is a customer or manager. Then you can simply build your own logic like customers can only edit their own dns pages etc.; I'd really do that using ProcessModules and not via the page tree. The page tree has big problems hiding/showing stuff based on access related things (see AdminRestrictBranch and its limitations).
  9. Sounds like a job for https://modules.processwire.com/modules/fieldtype-text-unique/ ?
  10. It's impossible to know all features of Tracy ? Just getting used to the API Explorer. So useful!! Thx (over and over) again!
  11. I'd try to remove as much as possible and then add features step by step and see when things start breaking. After each action check Site Frontend (different pages, does the problem occur on one special page or on all?) Site Backend (sometimes I had problems with my own code, so the frontend was broken but the backend worked) This is what I'd do: Add a "return;" on top of ready.php, init.php and maybe other files that you load on every request; Remove all modules (eg by renaming the /site/modules folder to /site/modules_tmp The frontend will likely break if your modules are not available, but the backend should work. If your site works again, move over the modules 1-by-1 from modules_tmp to modules and see what happens.
  12. I haven't used frontend editing for ages (always had issues with images fields or css conflicts or the like), but it might be worth to try disabling AOS if you have that installed and you are using the language switcher? I had some problems lately with that feature of AOS...
  13. Sorry for the double post, but could you guys please add your support to my explanation of this PR on github? https://github.com/processwire/processwire/pull/169#issuecomment-604474639 Especially @Gadgetto @Robin S @adrian @wbmnfktr @horst @teppo @kongondo Thank you!
  14. Hello @Inxentas have you had a look at these topics? https://www.google.com/search?q=site:processwire.com+change+default+language
  15. Are you thinking about any in particular? Lots of them seem to the the same as my code intellisense already does...
  16. v1.0.7 has some snippet improvements and this new snippet: pwire: Add wired object
  17. Better late than never... I've just created a fix for the annoying inputfield bug ? https://github.com/processwire/processwire/pull/169
  18. Now it shows this, so I guess it was somehow messing up with some cached values ? Wow, this even works for my own $theme API variable ?
  19. v0.0.3 fixes a bug where cssUrl returned the path and not the relative url due to windows directory separator. Not sure why this was no issue until now... And another reason why I hope that we get $files->url() and $files->path() soon ?
  20. Hi @Ivan Gretsky Which links are broken? Hm... Nette Forms... I really like one thing: Synched frontend + backend form validation. But on the other hand I really miss one thing: ProcessWire ? Especially hooks. In Nette there's nothing like hooks as far as I know. They do everything by extending base classes. This can be great for more complex modifications (that's why we now have the new Page classes in ProcessWire), but modifying just one single Inputfield of a Nette form was hard to impossible for me when I played around with it some time (years?) ago... That experience might be a little different know that I made quite some progress in OOP. One thing I could also not get working was CSRF protection via Nette. So I implemented it using ProcessWire's internal feature: Yes... it's obviously been 2 years since I worked on that ? I need some forms on a new project, so I might have a look into a new Forms Module using the new RockNette concept and Nette v3.0
  21. Hi @LuisM Thank you, that's a known issue https://github.com/BernhardBaumrock/RockDatetime/issues/1 and thx to @Jan Romero we already know the reason. I have some more important stuff to do at the moment and I think I'll refactor my module to use the https://carbon.nesbot.com/ library for handling single timestamps as it has everything already done and is well tested and maintained ?
  22. I guess that tool was not exactly built for mocking up a ProcessWire template/field-setup ? But I think this could be quite helpful - also for asking/explaining questions here on the forum! https://dbdiagram.io/
      • 10
      • Like
  23. v1.0.2 is out ? pwinputfield: Add Inputfield Boilerplate
  24. Ok that's quite different from VSCode, but I guess you could nonetheless try to convert the VSCode JSON to the PHP Format... https://github.com/BernhardBaumrock/pwsnippets/blob/master/snippets/php.json
  25. Thx ? Does PhpStorm offer a similar feature? Maybe we can port it easily...
×
×
  • Create New...