Jump to content

DrQuincy

Members
  • Posts

    210
  • Joined

  • Last visited

Posts posted by DrQuincy

  1. I'm sure this will be something really simple but I am adding repeaters items to a Pro Fields Matrix and each item gets added twice (they are identical). The code definitely only runs once and there are no duplicate items in $basketTable. Am I doing anything wrong?

    Thanks.

    (Irrelevant code has been removed)

    $p = new \ProcessWire\Page();
    
    $p->template = 'account-order';
    $p->name 	 = $orderReference;
    
    $p->of(false);
    
    foreach ($basketTable as $tableItem) {
    	
    	if (isset($tableItem['type']) === true) {
    		
    		if ($tableItem['type'] == 'typeface-trial') {
    			
    			$orderDownload  						= $p->orderDownloads->getNew();
    			
    			$orderDownload->repeater_matrix_type 	= 1;
    			$orderDownload->orderTypeface 			= $tableItem['id'];
    			
    			$orderDownload->save();
    			$p->orderDownloads->add($orderDownload);
    			
    		}
    		
    		if ($tableItem['type'] == 'typeface-weight') {
    			
    			$orderDownload  						= $p->orderDownloads->getNew();
    			
    			$orderDownload->repeater_matrix_type 	= 2;
    			$orderDownload->orderDownloadsWeight	= $tableItem['weight-id'];
    			$orderDownload->orderDownloadsLicense	= $tableItem['license-id'];
    			
    			$orderDownload->save();
    			$p->orderDownloads->add($orderDownload);
    			
    		}
    		
    		if ($tableItem['type'] == 'typeface-bundle') {
    			
    			$orderDownload  						= $p->orderDownloads->getNew();
    			
    			$orderDownload->repeater_matrix_type 	= 3;
    			$orderDownload->orderDownloadsBundle	= $tableItem['bundle-id'];
    			$orderDownload->orderDownloadsLicense	= $tableItem['license-id'];
    			
    			$orderDownload->save();
    			$p->orderDownloads->add($orderDownload);
    			
    		}
    		
    	}
    	
    }
    
    $p->save();

    I thought the process is to called getNew(), set the properties, save it and then add it to the page and save that — which I'm doing.

  2. I want to make it so that both user name and email are unique in template users. I've got this which issues a warning, which is better than nothing:

    wire()->addHookAfter('Pages::saveReady', function(\ProcessWire\HookEvent $event) {
    	
    	$page = $event->arguments(0);
    	
    	// Don't run on newly created pages
    	if ($page !== null && $page->created !== 0) {
    	
    		if ($page->template == 'user') {
    				
    			$pages = wire('pages')->find('template=user');
    			
    			foreach ($pages as $p) {
    				
    				// Don't compare to current page
    				if ($p->id != $page->id) {
    						
    					if ($p->email == $page->email) {
    						
    						wire('session')->warning('**' . $p->email . '** is already being used by [' . $p->name . '](/processwire/access/users/edit/?id=' . $p->id  . ') — it is highly recommended each account has a unique email address', \ProcessWire\Notice::allowMarkdown);
    						
    					}
    					
    				}
    				
    			}
    			
    		}
    		
    	}
    	
    });

    I am also using the EmailNewUser module. The reason I am issuing a warning as opposed to preventing the page being saved is regardless of what I do if you have the Send welcome email checkbox checked, EmailNewUser seems to always send the email out — and always to the duplicate email. I did try overriding the email to a random non-existent one so the email send would go nowhere — but EmailNewUser always emails the duplicate address, which is going to be confusing for the client.

    Is there any way to adapt the above code to prevent EmailNewUser from running if the email is a duplicate? Or is there a way to make EmailNewUser used an updated random email instead? I'm not sure how it works but however EmailNewUser is set up it seems to always use the old value.

    I hope that makes sense! Thanks.

  3. Thank you.

    I never knew about the relationship between the hook and the class and function name so that is a huge help. Studying the Page and Pages class shows me how different they are — so that makes sense! 🙂

    I'm still not 100% on the second point. From reading about hooks I thought the first condition in parenthesis in a hook was a selector and the second was a filter for fields. So to me, this should work since I want to filter out saves where the page has a specific template:

    \ProcessWire\wire()->addHookBefore('Pages(template=typeface)::saveReady', function(\ProcessWire\HookEvent $event) {
    
    	\ProcessWire\wire('session')->error('Hook called');
    	
    });

    But instead this is what works:

    \ProcessWire\wire()->addHookBefore('Pages::saveReady(template=typeface)', function(\ProcessWire\HookEvent $event) {
    
    	\ProcessWire\wire('session')->error('Hook called');
    	
    });

    Why is that? I'm sure I'm missing something really obvious!

  4. I am preventing pages with a specific template from being deleted. I have it working with Pages::trash(template=foo), Pages::delete(template=foo).

    I'm after a better understanding of why this works please.

    1. Why does it work with Pages and not Page, what's the difference?
    2. Why is it Pages::trash(template=foo) and not Pages(template=foo)::trash?

    I have read the docs page on hooks but still don't quite get it. 🥴

  5. Thanks to you both, some very good suggestions.

    From the template file @Robin S is right, you need to use return. Actually though, just this will work:

    return '<p>An error occurred</p>';

    What would be the advantage of:

    <p>An error occurred</p>
    <?php return $this->halt(); ?>

    What does halt() actually do?

    I am reluctant to use exit() as I have some code in finished.php to run so the above works well for me, thanks.

  6. I've searched for this but couldn't find anything but am maybe using the wrong terms.

    Is there a way to do something like exit('<p>an error occurred!</p>'); but still have ProcessWire “shutdown” normally? Specifically, I want to be able to not render the template but still run some logging code in finished.php.

    Is there a function for this?

    Thanks.

  7. In my recent question about utf8mb4, I was asking about columns being lowered from 255 to 250. I misunderstood since I've just realised for some time VARCHAR can go way beyond 250 anyway so none of them in the database dump would be affected. The 250 in that quote is referring to column_name values for indexes.

    Many of PW's tables for fields have indexes:

    data_exact, BTREE data(250)
    data, FULLTEXT, data

    I presume it just uses data or data_exact based on type of selector used (natural language or exact).

    I've never specified an index length when using BTREE though and didn't realise there was a limit (though databases aren't my strong point). Does this just mean that if you do an exact search it will only apply to the first 250 characters of what is in data? If so, why explicitly set a limit? Wouldn't MySQL just default it to 250 anyway — or if you're in strict mode does it cause errors?

    I guess when setting up a database myself I would almost always use BTREE on relations where it's just an int, so would never run into this limitation.

    Thanks!

     

  8. I have changed a database over to utfmb4 and it seems to work. I:

    • Exported the database
    • Replaced utf8 with utf8mb4
    • Imported SQL back in
    • Added $config->dbCharset = 'utf8mb4'; to config.php

    And now I can add Emojis via the CMS. Do I need to do anything else? I know that types like VARCHAR will no longer hold the same amount of characters now some characters take up four bytes but here Ryan says:

    Quote

    Because utf8mb4 uses more bytes per character, it places new limits on the length of indexes used by ProcessWire. ProcessWire has several 255-character index lengths, and the maximum allowed by utf8mb4 are 250 (250x4=1000). For this reason, we've updated all of our core Fieldtypes to use no more than 250 length indexes in order to support utf8mb4. However, it's possible that 3rd party modules might be using index lengths that aren't compatible with utf8mb4, so this is something to keep in mind.

    Modules-wise I am using mainly Pro Fields and core ones. I also notice studying the database schema that most fields would not be affected by this; some use ASCII, most use larger TEXT fields. The few UTF8 VARCHAR fields are system ones set to 250 as above, which from the sounds of it Ryan allocated for years ago. EDIT: I can see the above refers to index lengths and not VARCHAR lengths, the latter can be larger than 250. See my other post.

    Am I likely to run into any problems? Other than smaller indexes or a huge VARCHAR column being too big to convert over (highly unlikely) what is the worst that can happen?

  9. Ah, thanks — id=page.your_repeater_field_name works perfectly!

    Also, thanks for your explanation regarding point 2.

    Regarding point 1, I worked it out, thanks, per my previous post. I am just storing an array of repeaterID => quantity in my session as I wanted to pull the live data on each request.

  10. Ah, well, I've answered point 1.

    $item = $this->wire('pages')->get('template=repeater_options, id=1255');

    I'd love to know the answer to point 3 though. It would be extremely useful if that's an option. If I do a Page Reference field and set the selector string to template=repeater_options it would get every single item — whereas I'd only want the ones for the current page.

  11. I've got a repeater of product options within a single product page template.

    So template=product has a template file and has a field called options, which is a repeater with title and price.

    I want to store the repeater IDs in a session for the shopping basket and then read them. E.g. if ID 1255 is in the array, this gets me the repeater item:

    $item = $this->wire('pages')->get('template=product, options.id=1255')->options->get('id=1255');
    1. Is there a way to access the repeater directly? It just seems a bit clunky and inefficient to have to get the page and then the repeater by expressing the ID twice, when the ID should be able to get me to the repeater item with one call.
    2. I can run $pages->get(123) with a top-level page and it gets the page but when using get() from a repeater item I have to do get('id=123') — any reason for this? It doesn't really matter, I just wondered.
    3. Is it possible to have a Page Reference field that references a repeater for that page only? So, say I have options in a repeater as above: (Small, £10), (Large, £15), could I have a Page Reference field that let's me pick Small, Large, etc? The use case is specifying product bundles.

    Thanks!

  12. I'm using Pro fields table (support has expired but will renew it if needed).

    How do you mean a column field required in the settings (not the overall required setting — but individual columns in the table)? I've tried:

    required=1
    required=true
    required

    But you can still leave it blank. Any ideas?

    Thanks.

  13. On 1/27/2020 at 10:23 PM, Arcturus said:

    During a recent maintenance routine we found that our website's database (1,700+ pages) had thousands of instances of unnecessary, garbage code that had come with copied text from Word. Passages with margins expressed in points, cms and inches, and some that were wrapped in upwards of 7 spans were among the most easily identified crimes. Purging all of this dropped our database size by over 4%.

    A few of the code examples above nuke all inline styles, which will impact some important out-of-the-box functionality for PW3 and CkEditor (depending on your use); specifically with many of the options with tables and lists, such as setting a column width or changing the bullet styles within a nested list.

    To work around that, I made some changes to Ryan's code to target specific tags and to eliminate spans (which you can only add via Source view without pasting them in).

    $wire->addHookAfter('InputfieldCKEditor::processInput', function($event) {
      $inputfield = $event->object;
      $value = $inputfield->attr('value');
      if ((strpos($value, 'style=') === false) && (strpos($value, '<span>') === false)) return;
      $count = 0;
      $qty = 0;
    
      // Optional remove spans
      $value = preg_replace('/<span.*?>/i', '', $value, -1, $qty);
      $value = preg_replace('/<\/span.*?>/i', '', $value, -1);
      $count = $count + $qty;
    
      // Remove inline styles from specified tags
      $tags = array('p','h2','h3','h4','li');
      foreach ($tags as $tag){
        $value = preg_replace('/(<'.$tag.'[^>]*) style=("[^"]+"|\'[^\']+\')([^>]*>)/i', '$1$3', $value, -1, $qty);
    	$count = $count + $qty;	
      }
    
      if(!$count) return;
      $inputfield->attr('value', $value);
      $inputfield->trackChange('value');
      $inputfield->warning("Stripped $count style attribute(s) from field $inputfield->name");	
    });

     

    This is great, thanks!

    In my settings I am allowing spans with classes. Do you know how I might adapt your code to allow this? If I have HTML with <span class="foo">bar</span> but no empty spans or style assets, <span class="foo">bar</span> stays. But if I add in <span>foo</span>, <span>foo</span> changes to just foo (great!) but <span class="foo">bar</span> also change to bar.

    Also, by default, CKEditor seems to allow style="margin: " I assume this is related to indentation. Any idea how you can disable this? Extraneous spans are annoying but harmless. However, the margin styles mess the formatting up. I do not even have the indentation buttons visible in the toolbar.

    Thanks!

  14. Thanks for the explanation, Jan. And, yes, that makes sense as to the difference between roles and permissions.

    I have created the permission tasks-list-access and assigned it to the role content-manager. I have then added 'permission' => 'tasks-list-access' to my Process class but it hasn't made a difference. Am I missing something?

    I refreshed the module and it worked. Hoorah! Thanks very much for your help. 🙂

    • Like 1
  15. I have a simple Process installed that displays some dynamic HTML that I want to allow a logged in person to view who doesn't have the superuser role. Is this possible? I am assuming because a class that extends Process is used by the admin template this doesn't happen by default. How do I did it?

    I have tried adding  'permission' => 'content-manager' to getModuleinfo() by when I access the page as a content-manager user it just redirects to the site tree.

    Thanks. 🙂

×
×
  • Create New...