-
Posts
16,705 -
Joined
-
Last visited
-
Days Won
1,514
Everything posted by ryan
-
Two things to try: On your first server, check that .htaccess is working. You can tell by editing it and putting in some junk characters on the first line, like "alfkjeafkljaeglk". Then save. Load the site. If you get a 500 Internal Server Error, then your htaccess is being read by Apache. If you don't, then Apache isn't reading your .htaccess file. I'm guessing that's the case. To fix it, add this to the entry in your httpd.conf vhosts section: AllowOverride All OR, copy all of ProcessWire's .htaccess directives and just put them in your vhost section of httpd.conf. I recommend doing AllowOverride All so you don't have to go manually update your httpd.conf when you do upgrades. On the second server, we can find what the problem by editing /site/config.php and changing $config->debug = false, to $config->debug = true. Reload and it'll tell you what's up. Paste the error in here. Thanks, Ryan
-
It sounds like you might not have Apache mod_rewrite, or you maybe missing the /.htaccess file in your ProcessWire/site root dir. Also, check to make sure that you don't have an empty dir called "ProcessWire" off of your installation. If you do, just remove it. That can happen if you move all the files out of the default dir it unzips to, which I sometimes do... and because it's the same name as the default admin login URL, it can show you a 404 rather than the admin page. Let me know what you find.
-
Some questions (Plus tutorials on how to support multiple languages)
ryan replied to Snakehit's topic in General Support
The admin and error messages use language, and we will be getting more language packs for those in the future. For everything else, ProcessWire is language agnostic and doesn't use language. It doesn't currently provide any specific tools for handling multiple languages (though we are looking at that). But for now, I think there are a lot of approaches you could take, and Adam's approach looks like a good one. Here's another one below. One of the main differences from Adam's example is that this one keeps separate fields for each language on one page, rather than separate pages for each language. Which approach is better will depend on your needs. Using field names and URL segments to display different languages Use the $input->urlSegment API variable to check if the last segment of the URL is "es" or "fr" or "en", and have your template output the corresponding field. For example, lets say you had a page called /about/ and these are the language versions you supported (and their corresponding URL): /about/ - The default language page, we'll assume English /about/es/ - The Spanish About Page /about/fr/ - The French Language Page The /about/ page is used as an example, but this could be for any and/or all pages in your site. You could set it up for one template and use it site-wide. You would create fields corresponding to the languages that you want to support, and add them to your template. For instance, you might have a field called "body_en" for English, and then one called "body_es" for Spanish, and "body_fr" for French. If you are dealing with lots of fields, you may want to separate them with ProcessWire's FieldsetTab field type, which will place each language version on a separate tab, compartmentalizing each language on it's own screen. Then you would enable URL segments for your template(s) in the admin (Setup > Templates > Advanced). That ensures that your /about/ page won't show up as a 404 page when someone accesses /about/es/ (because the es/ doesn't actually exist). The API code in your template might look like this: <?php // other languages that we support in addition to English $languages = array('es', 'fr'); // check if the current page's URL ends with a language if(in_array($input->urlSegment1, $languages)) { // A known language was specified in the URL, so save it in our session. // Now that language will be the default even if URL doesn't have a language specified. $session->language = $input->urlSegment1; } if($session->language) { // Get the body field for our custom language $body = $page->get("body_" . $session->language); } else { // get the body field for our default language (English) $body = $page->body_en; } // Output the body text echo "<div id='bodycopy'>$body</div>"; That will ensure that the correct language is always displayed. They would only have to hit the /about/es/ page once (or any /es/ page), and the rest of the site would then assume it's displaying Spanish regardless of the URL. Likewise, if they hit /about/fr/ next, the site is now in French. If you preferred it, you could relegate this functionality to some gateway page where they had to select a language. But I prefer it this way because they can make language changes on the fly (not that people do that, hehe). Optional: Locking a language to a URL It may be that you want to lock your language setting to a URL so that English always displays at /about/ and never Spanish or French. Likewise, the only time you'd see Spanish is if you were at the /es/ version of a given page. The advantage of this approach is that you may get 3 different language versions of your site indexed in Google, and it will be possible for you to use template caching. (You wouldn't be able to use template caching in the previous example because it's session-dependent rather than URL-dependent). To do this, you would just need to pay attention to the language setting when you create your navigation, and you would update the example above to show the "body" field according to the validated $input->urlSegment1 and not $session->language. Here's how you might create language-aware navigation: <?php // determine our language (or reuse the other example) if(in_array($input->urlSegment1, array('es', 'fr'))) $language = $input->urlSegment1; else $language = 'en'; $topnav = $pages->find("parent=/"); foreach($topnav as $item) { $url = $item->url; // if language isn't English, add a language segment to the end of the URL if($language != 'en') $url .= $language; echo "<li><a href='$url'>{$item->title}</a></li>"; } Optional: Redirecting to the selected language If you didn't want to always consider language when drawing navigation, you could also just set your pages to redirect to the proper language version if they access the generic /about/ page when the language is Spanish. You'd want to store your language in $session->language again, like in the first example, and perhaps use that instead of the $language variable in the second example. <?php if($session->language && !$input->urlSegment1) { $session->redirect($page->url . $session->language); } I'm not exactly sure what you mean by indent lines. Can you attach a screenshot example or rough mockup of what you mean? While ProcessWire doesn't yet have a public-site form collection feature, making a contact form is a pretty easy thing to do. See this post for an example: http://processwire.com/talk/index.php/topic,22.0.html Though I think in most cases, you would probably want to email the submitted form rather than saving it to a page, i.e. mail("you@company.com", "Contact Form Submitted", $message); For an image gallery, Adam is right on. Also see this: http://processwire.com/talk/index.php/topic,16.0.html -
Good question. You can use the built-in append() and/or prepend() functions, i.e. $menu_items->prepend($home); I believe the unshift syntax will also work: $menu_items->unshift($home); You don't have to worry about modifying the site tree because you would have to actually save a $page in order to modify the site tree. In addition, none of what you are trying to do here is modifying any pages, so even if you did save a page, it wouldn't modify anything. PageArrays are runtime dynamic arrays, and aren't saved anywhere unless they are attached to something like the result of a Page reference Fieldtype. See /wire/core/Array.php and /wire/core/PageArray.php for all the traversal and modification methods (there are a lot). For the most part, they are patterned after jQuery traversal methods, but I included alternate names like unshift() and shift() for people that prefer PHP function names. Just for fun, here's your example all bundled on one line. foreach($pages->find("parent=/navigation/")->prepend($pages->get('/')) as $item) { ... Or even shorter: foreach($pages->find("parent=0|/navigation/") as $item) { ... The selector above is saying to find all pages that have no parent (i.e. "0") or have a parent called /navigation/. Both examples above return the same result. That won't work because the function only accepts one param (a selector). But because all the pages you are selecting there have the same parent, this would work: $pages->find("parent=/, name=navigation|page|item2");
- 1 reply
-
- 2
-
-
Adam, writing this up got me to thinking that I may be able to write a tool that would automate most of deployment, by creating an installation profile. The tool would be in the ProcessWire admin, and when you run it, it would create a installation profile of your site. Then, you would just upload the profile to your server, and then load it in your browser (which would initiate the installer). I kind of like this idea because it would handle any file permissions and export + import of the database automatically. Are you aware of any other CMSs that do something like this? (I'm not). I'll put more thought into this.
-
MIGRATING A SITE FROM DEV TO LIVE FOR THE FIRST TIME STEP 1: Copy all the files from dev to live Copy all the files from your dev installation to the live installation. Whether you use FTP, rsync, scp or some other tool doesn't matter. What does matter is that you copy everything and that you retain the file permissions–More specifically, make sure that /site/assets/ and everything in it is writable to the web server. Here are some sample commands that would do it (replace 'local-www' and 'remote-www' with your own directories): Using rsync: rsync --archive --rsh=/usr/bin/ssh --verbose /local-www/* user@somehost.com:remote-www/ Using scp: scp -r -p /local-www/* user@somehost.com:remote-www/ Using FTP/SFTP/FTPS: I think this will depend on the FTP client that you are using as to whether it will retain the permissions of the files you transfer. Hopefully it will do that by default, if not, check if there is a setting you can enable. If not, then you may have to adjust the permissions manually in /site/assets/ and the dirs/files in there. Make sure /.htaccess got copied over too Depending on how you copied the files in step 1, it may or may not have included the /.htaccess file. Make sure that gets copied over, as many copying tools will ignore hidden files by default. STEP 2: Transfer the MySQL Database from Dev to Live a. Export dev database Export ProcessWire's database on your development server to a MySQL dump file. PhpMyAdmin or mysqldump are the most common ways to do it. Personally I use PhpMyAdmin because it's so simple, but here is how you would do it with mysqldump: mysqldump -u[db_user] -p[db_pass] [db_name] > site.sql b. Create live database Create a new MySQL database and database user on the live server and make note of the DB name, DB host, DB user and DB pass, as you'll need them. c. Import dev database to live Import the MySQL dump file you exported from your dev server. Most web hosts have PhpMyAdmin, so that's what I use to import. If you have SSH access, you can also import with the mysql command line client, i.e. mysql -u[db_user] -p[db_pass] -h[db_host] [db_name] < site.sql d. Update /site/config.php On the live server, edit the /site/config.php file. At the bottom you will see the database settings. Update these settings to be consistent with the database/user you created, then save. Most likely you will only be updating these settings: $config->db_name = "database name"; $config->db_user = "database user name"; $config->db_pass = "database user password"; $config->db_host = "database host, most commonly localhost"; STEP 3: Test the Site Your site should now be functional when you load it in your browser. If not, then enable debug mode: a. Turn on debug mode (only if you get an error) Edit /site/config.php and look for $config->debug = false, and change it to $config->debug = true. Then load the site in your browser again and it should give you more details about what error occurred. If you aren't able to resolve it, contact Ryan. b. Browse the site Assuming your site is now functional, browse the site and the admin and make sure that everything looks as it should. If it looks like stylesheets aren't loading or images are missing, it may be displaying cached versions from your dev site. If that's the case, you need to clear your cache: c. Clear your cache (optional) Login to the ProcessWire admin and go to Modules > Page Render > and then check the box to "clear page render disk cache", then click save. This should resolve any display issues with the site's pages. d. Practice good housekeeping Make sure that you've removed /install.php and /site/install/, and check to make sure that /site/config.php is not writable. While this isn't absolutely necessary, it's good insurance.
-
Publish/Unpublish by date or by manual means.
ryan replied to moondawgy's topic in Wishlist & Roadmap
moondawgy, I expanded on your question with a better answer here: http://processwire.com/talk/index.php/topic,35.0.html -
This article introduces multiple concepts in ProcessWire, as goes far beyond the stated subject. The subject of "unpublishing a page" serves as an ideal context, so I encourage you to read the entire article (particularly strategy #2) as it introduces the concepts of role inheritance and API-level page access, among other things. To unpublish a page without permanently deleting it, use any one of the following 4 strategies (whatever suits your needs best). STRATEGY #1: MOVE THE PAGE TO THE TRASH This is the most straightforward strategy, but does involve moving the page. I don't permanently delete pages very often, so I use the trash as more of an archive. When you delete a page in the ProcessWire admin, it just moves the page to the Trash. You can still move it back out at any time (as long as you haven't emptied it first). Pages in the trash are not web accessible and don't appear in search results, so it's a good place to unpublish your page. To unpublish your page in this manner, just delete it by clicking it's 'delete' tab, checking the confirmation box, and saving it. The page will be waiting for you in the trash. You can also trash pages directly in the Page List view: 1. Go to the Page List view by clicking "Pages" in the top right corner. 2. Click the Trash page to open it. 3. Click the page you want to unpublish, you should see a 'move' link. 4. Click the 'move' link, and then drag the page into the trash. From the API you can trash pages by calling $pages->trash($page); STRATEGY #2: REMOVE THE GUEST ROLE FROM THE PAGE: This strategy includes a few factors to consider, but has the benefit of not having to move the page. First off, here's how you do it: 1. Edit the page and click on it's 'Settings' tab. 2. Locate the 'Roles' section near the bottom. 3. Uncheck the 'guest' role. 4. Consider checking the 'hidden' status too (see the section further down). 4. Save. The 'guest' role implies anonymous site users. If there are any other roles still checked, those are the only roles that can view the page. If no roles are checked, then only the 'superuser' role can view the page. Note that the page can still appear in API function results (like in your navigation), so you should also check the 'hidden' status too if you don't want that (see section further down) OR check access in the API (see the API section further down). Note about Role Inheritance This note is only applicable to a Page that has children. Page roles are inherited through the site tree, so unchecking a 'guest' role on /about/company/ unchecks it on any children and grandchildren, and so on. For example, if you removed the 'guest' role from /about/company/, then these page would also be inaccessible to 'guest': /about/company/staff/ /about/company/staff/mike/ But you can still turn the 'guest' role (or any other role) back ON at any of the descending pages. For instance, you could turn the 'guest' role back on at /about/company/staff/ and that page would be accessible, even if it's parent isn't. Because roles are inherited, enabling 'guest' at /about/company/staff/ also enables it for /about/company/staff/mike/, but the same rules about inheritance apply no matter where you are in the site structure. As a result, removing the 'guest' role from the homepage would unpublish the entire site from public access, unless any other pages had specifically set a different behavior. Note about Roles and the API In the ProcessWire API, the results of any page selection functions like get(), find(), children() and siblings() do not consider roles. If the page is in your site tree, you can still select it in the API. This is so that you can continue to use assets from a page in the API even if the page isn't a URL on your site. 'Page' is just an abstract term that doesn't have to refer to a page on your site... You might be using a page for storage of shared image assets, site configuration or other items that you don't want to be directly accessible via a URL, but you DO want to utilize in the API. As a result, if you don't want a page showing up in navigation or search results you can do one of 2 things: 1. Check the box for the 'hidden' status (described in the section further down). 2. Or, when generating your navigation, check if the page is viewable before outputting the link via $page->isViewable(). For example: <?php foreach($page->children() as $child) { if($child->isViewable()) echo "<a href='{$child->url'}'>{$child->title}</a> "; } I don't like having to check access from the API, so recommend doing #1 over #2. In the near future, automatic access checking will be an option you can optionally enable with API functions (should you want to). STRATEGY #3: USE THE HIDDEN STATUS Another alternative to unpublishing (via removing the 'guest' role) is to enable the 'hidden' status in the page editor. You may want to use this instead of–or in addition to–removing the 'guest' role, depending on your needs. This strategy doesn't technically unpublish the page unless you combine it with strategy #2. But this strategy is often a better solution then just unpublishing, as there is no danger of breaking any on-or-off site links. 1. Edit the page you want to hide and click on it's 'Settings' tab. 2. Locate the 'Status' section. 3. Check the box for 'Hidden'. 4. Save. A hidden page is excluded from the API find(), children() and siblings() results, which essentially excludes it from any of your dynamically generated navigation. It's not excluded from get() results, so the page can still be accessed by it's URL or loaded individually from the API. This means the page isn't going to show up in your navigation or search results, but loading the URL directly will still load the page. STRATEGY #4: JUST DON'T DO IT (IF YOU DON'T HAVE TO) As a best practice, I always encourage my clients not to delete or unpublish pages if they don't have to. For example, let old news items drift further down the list rather than deleting them... someone, somewhere may be linking to it. Every link to your site carries value, even if the page being linked to isn't recent. At least the user gets what they expected and can explore further in your site. And Google will still count that link as a vote in favor of your pagerank. Delivering a 404 page is less likely to result in that outcome for the user or your site. Of course, there are lots of good reasons to delete or unpublish a page, so I'm just speaking about the situations where you don't necessarily have to delete it. Another alternative is to setup 301 redirects for pages that you delete or unpublish, but I won't cover that here unless someone wants me to.
-
Publish/Unpublish by date or by manual means.
ryan replied to moondawgy's topic in Wishlist & Roadmap
To unpublish by manual means (without deleting or moving the page), edit a page, click on it's 'settings' tab and uncheck the 'guest' role from the access section. There isn't a tool in the CMS to automate unpublishing by date. Though it could easily be implemented with a little API code (let me know if I can provide an example?). However, I always encourage my clients not to delete or unpublish pages if they don't have to, unless they plan to setup 301 redirects. For example, let old news items drift further down the list rather than deleting them... someone, somewhere may be linking to it. Every link to your site carries value, even if the page being linked to isn't recent. At least the user gets what they expected and can explore further in your site. Delivering a 404 page is less likely to result in that outcome. Of course, there are lots of good reasons to delete or unpublish a page, so I'm just speaking about the situations where you don't necessarily have to delete it. Another alternative to unpublishing is to enable the "hidden" status in the page editor (also on the Settings tab). A hidden page is excluded from find(), children() and siblings() results, which essentially excludes it from any of your dynamically generated navigation. It's not excluded from get() results, so the page can still be accessed by it's URL or loaded individually from the API. -
I agree, and the system is designed for it, just haven't implemented yet. Stay tuned.
-
That's a good idea, though I'm not sure that I've found an ideal method. I should probably be using Git for this, but I don't yet know it well enough. When I referred to deployment in the other messages, I was talking about deploying the source code to Git, rather than deploying a staging to production site. But here's how I do it. For live sites, I use rsync to deploy my staging files to the server's files. i.e. at the OS X command line: rsync --archive --rsh=/usr/bin/ssh --verbose site/templates/* user@somehost.com:www/site/templates/ This is made easier if your SSH keys are setup so that you don't have to type your password every time you do this. I selectively rsync the /site/templates/ or /wire/ dirs, depending on what I'm upgrading. If I'm upgrading the version of ProcessWire, then I rsync the /wire/ dir. If I'm upgrading the site's functionality (usually via it's templates, stylesheets and scripts) then I'll rsync /site/templates/. I don't do full-site rsyncs because the version of data on my dev server is usually older than the version of data on the live site. Though I do daily backups, I don't mirror my dev copy's data with the server's... unless there's a reason to do so. My deployments are usually code related, not database related. So to safely deploy upgrades to a live site, I specifically avoid rsyncing any files in /site/assets/ or any database files. Instead, I just keep rotating backups of those and manually upgrade my dev site to the latest version of that data whenever it makes sense to do so. Even though I'm using rsync, which makes this all a lot nicer, quicker and more secure, there's not a major difference between this method and just FTPing the files. So you can see I don't have a perfect system for this. I'm interested to learn other people's methods.
-
Having multiple databases (hacking PW2 to work on two different servers)
ryan replied to Adam Kiss's topic in General Support
What you suggest should work just fine. Using your example, here's how I'd code it in your /site/config.php: Solution 1: <?php // If your dev/staging isn't 'localhost' then replace it with // whatever $_SERVER['HTTP_HOST'] is on your dev server: if($_SERVER['HTTP_HOST'] == 'localhost') { // staging or dev server $config->dbHost = 'localhost'; $config->dbName = 'pw2_blank'; $config->dbUser = 'root'; $config->dbPass = 'root'; $config->dbPort = '3306'; } else { // live server $config->dbHost = 'something'; $config->dbName = 'db_name'; $config->dbUser = 'db_user'; $config->dbPass = 'db_pass'; $config->dbPort = '3306'; } Solution 2: Another solution, and the one I use is to maintain a separate config file called /site/config-dev.php. ProcessWire looks for this file, and if it sees it, it'll use that rather than /site/config.php. If you just keep this file on your staging/dev server, and not on your live server, then that solves it for you. I also added that file to my .gitignore file so that it doesn't ever get deployed, while /site/config.php does. To get started with this method, just copy your /site/config.php to /site/config-dev.php (on your staging/dev server) and change the database settings in the config-dev.php version. -
Just wanted to add a couple more notes to this: Using a 'summary' field I think it's more common on this type of page that you would display a summary of the news story rather than the whole news story... and then link to the full news story. To display a summary, you could have a separate 'summary' field in your template, which would be just a regular textarea field where you would have a 1-2 sentence summary of the article. And you would display this 'summary' field rather than 'body' field in the news_index template. If you wanted to autogenerate a summary from the 'body' field (rather than creating a new 'summary' field), you could just grab the first sentence or paragraph of the body and use that as your summary. This is how I usually do something like that: <?php // make our own summary from the beginning of the body copy // grab the first 255 characters $summary = substr($story->body, 0, 255); // truncate it to the last period if possible if(($pos = strrpos($summary, ".")) !== false) { $summary = substr($summary, 0, $pos); } That's a really simple example, and you may want to go further to make sure you are really at the end of a sentence and not at an abbreviation like "Mr." In the example that Moondawgy posted, it makes sense to autogenerate a summary (if he needed it). But in other cases, the 'body' can be quite long (and take up a lot of memory), and it makes more sense to maintain a separate summary field if you have to keep a lot of pages loaded at once. This is really only an issue once you get into hundreds of pages loaded at a time. It's not an issue in these examples, but I just wanted to point it out. Autojoin Using the 'autojoin' optimization can increase performance on fields that get used a lot. Not using it can reduce the page's memory footprint. What is more desirable in each instance depends on your situation. In this news section example, the date and body fields would benefit from having 'autojoin' turned ON. See this page for an explanation: http://processwire.com/talk/index.php/topic,32.0.html
-
What does autojoin do? Using the 'autojoin' optimization can increase performance on fields that get used a lot. Not using it can reduce the page's memory footprint. What is more desirable in each instance depends on your situation. What sites should use autojoin? Autojoin is most applicable with larger sites. On smaller sites, there may be no benefit to using it or not using it. But it's good to know what it's for regardless. Where do you control autojoin? Autojoin is controlled per-field. You can turn it on by editing each field under Setup > Fields > [your field], and you'll see it under the 'Advanced' heading. When should you use autojoin? Autojoin causes the field's data to be loaded automatically with the page, whether you use it or not. This is an optimization for fields that you know will be used most of the time. Fields having their data loaded with the page can increase performance because ProcessWire grabs that data in the same query that it grabs the Page. Autojoin is a benefit for fields that are always used with the Page. This is best explained by an example. Lets say that you have a template for individual news stories called news_story. The news_story template has these fields: title date summary body sidebar We'll assume that when you view a page using the news_story template, all of the fields above are displayed. Fields that should have autojoin ON: Now consider a separate news_index template that displays ALL of the news stories together and links to them. But it only displays these fields from each news story: title* date summary In this case, the 3 fields above would be good to autojoin since they are used on both the news_index and news_story templates. If your title, date and summary fields didn't have autojoin turned on, then ProcessWire wouldn't go retrieve the value from the database until you asked for it it (via $page->summary, for example). Because the news_index template displays all the stories at once, and always uses the title, date and summary fields, it will perform better with title, date and summary having autojoin ON than with it OFF. In this case, it reduces the query load of the news_index template by 3 for each news story. To take that further, if it were displaying 20 news stories, that would mean 60 fewer queries, which could be significant. Fields that should have autojoin OFF: Now lets consider the body and sidebar fields, which are only used on the news_story template: body sidebar It would be desirable to leave autojoin OFF on those fields because there is no reason for the body and sidebar to be taking up space in memory when they are never used on the news_index template. While it might mean 2 fewer queries to view a news story, that is not significant and certainly not a worthwhile tradeoff for the increased memory footprint on the news_index template. Keeping autojoin OFF reduces a page's memory footprint. Conclusion Using the 'autojoin' optimization can increase performance on fields that get used a lot. Not using it can reduce the page's memory footprint. What is more desirable in each instance depends on your situation. But if your situation doesn't involve lots of pages or data, then you don't need to consider autojoin at all (and can generally just leave it off). Additional Notes Not all fields have autojoin capability. You won't see the option listed on fields that don't have the capability. *The title field has autojoin on by default, so you don't need to consider that one. It was included in the examples above because I thought it's omission might cause more confusion than it's inclusion. Be careful with multi-value fields that offer autojoin capability (page references and images, for example). Because MySQL limits the combined length of multiple values returned from a group in 1 query, autojoin will fail on multi-value fields that contain lots of values (combined length exceeding 1024 characters). If you experience strange behavior from a multi-value field that has autojoin ON, turn it OFF. If you want to play it safe, then don't use autojoin on multi-value fields like page references and images.
- 3 replies
-
- 20
-
-
I forgot to reply to your question about sorting by the "team" field. If that's the way you always want them to sort by default, then I would suggest just setting that as your sort field when you edit the page (in the Children tab). When you do that, they'll always be sorted that way on any children() call, unless you've specified a different sort. But if you want to keep them alphabetical in the admin, and sorted by Team followed by the person's full-name (stored in the 'title' field) on the front end, you can have ProcessWire sort them for you when you load the staff members: $staff = $pages->get("/about/staff/people/")->children("sort=team, sort=title"); If "team" is a page reference, like in the optional section in my previous message above, then your selector will look a little different: $staff = $pages->get("/about/staff/people/")->children("sort=team.name, sort=title"); The selectors above are saying to sort by team first, and the person's full-name (stored in the 'title' field) second. In the second example, "team" is a page reference. Since a page contains lots of different fields, we have to tell it which one, so we're telling it to sort by the "team" page's "name" field.
-
This example assumes that you've already setup your fields and added the pages just as they are in the link you posted. It further assumes that each staff member is a page living under /about/staff/. An example would be: /about/staff/jemma-weston/ I'm going to assume these are the field names: title (person's first/last name) image (person's photo) team (per your link) job_title (per your link) email (per your link) To output the staff listing, you would do something like this: <?php echo "<ul class='staff_listing'>"; foreach($page->children as $person) { if($person->image) { // make 150px wide thumbnail $image = $person->image->width(150); $img = "<img src='{$image->url}' alt='{$person->title}' />"; } else { // make some placeholder (optional) $img = "<span class='image_placeholder'>Image not available</span>"; } echo " <li> $img <ul class='staff_detail'> <li><strong>Name:</strong> {$person->title}</li> <li><strong>Team:</strong> {$person->team}</li> <li><strong>Job Title:</strong> {$person->job_title}</li> <li><strong>Email: {$person->email}</li> </ul> </li> "; } echo "</ul>"; Optional Another scenario might be where you want the "team" or "job_title" fields to be from a pre-selected list, and to themselves be pages that the user could click on to view everyone in the "Audit and Accounts" team (for example). The way you would set that up is to create a new structure of pages that has all of the Team types (and/or Job Titles), like this: /about/staff/teams/audit-and-accounts/ /about/staff/teams/business-services/ /about/staff/teams/practice-management/ ...and so on... I would use a new template for your team types, something like team_type.php. The only field it needs to have is a title field. At the same time, I would move your staff members into their own parent like /about/staff/people/. In your staff template, you would make the "team" field be of the field type called "Page". When you create it, it will ask you from what set of pages you want it to use, and you can tell us to use /about/staff/teams/. It'll ask you if you want it to hold one page or multiple pages (PageArray), and you'll want to choose Page (one page). Likewise, where it asks you to select what type of input field you want to use, choose a "Select" or "Radio Buttons" field (and not one of the multiple select types). Add that new "team" field to the staff template, and when you add/edit a staff member, you'll select a team rather than type one in. Your markup to output that field in the staff listing would instead be like this: <li><strong>Team:</strong> <a href='{$person->team->url}'>{$person->team->title}</a></li> Your team_type.php template would list the staff members just like your main /about/staff/ page did (it might even be good to include your staff_list template to do it for you, or convert it to a reusable function), except that it would load the staff members like this: <?php $staff = $pages->get("/about/staff/people/")->children("team=$page"); foreach($staff as $person) { // ... } The main thing to note in the example above is the "team=$page" portion, which is essentially telling ProcessWire to pull all staff members that have a "team" field that points to the current page. So if you are viewing the /about/staff/teams/audit-and-accounts/ page, then it's going to load all staff members that are in that team.
-
It looks like your news page example displays 4 stories per page, and it displays the full bodycopy for each of them. Assuming the same structure and fields of the pages you posted, you'll need to create two templates: 1. news_index.php 2. news_story.php You can call those templates above whatever you want, but just note that we'll use a separate template for news stories and news items. Your news_story template will contain the following fields: 1. title 2. date (date) 3. body (textarea) And it's markup might look like this (excluding headers, footers, etc.): /site/templates/news_story.php: <h1><?=$page->title?></h1> <div id='bodycopy'> <?=$page->body?> </div> Not much action there. Your news_index is where most of the action will happen. In Admin > Setup > Templates > news_index > Advanced, check the box for "Page Numbers" to turn them on. Save. Here is what the code in your news_index.php might look like: /site/templates/news_index.php: <h1><?=$page->title?></h1> <?php // start the news stories list echo "<ul>"; // get the stories for this page $stories = $page->children("limit=4, sort=-date"); // note if you set the stories to sort by date descending on the /news/ page // in the admin, then you can omit the "sort=-date" above. // cycle through each story and print it in a <li> foreach($stories as $story) { echo " <li><a href='{$story->url}'>{$story->title}</a> <p><strong>Date:</strong> {$story->date}</p> {$story->body} </li> "; } echo "</ul>"; // get values for our placemarker headline $start = $stories->getStart(); $end = $start + count($stories); $total = $stories->getTotal(); $num = $input->pageNum; $lastNum = ceil($total / $stories->getLimit()); // output the placemarker headline echo "<h4>Showing $start - $end of $total Article/s | Page $num of $lastNum</h4>"; // output pagination links echo $stories->renderPager(); Lets say that you don't want the pagination links that renderPager produces, you can always modify it's output by passing params to it. See the page about pagination here: http://processwire.com/api/modules/markup-pager-nav/ But if you want your literal "previous" and "next" buttons like on your site, then you'll want to insert your own logic to do that. Something like the next example. Note I'm reusing the vars I set in the previous example here for brevity. This snippet would replace the renderPager() method in the previous example. <?php // make the previous link if($num > 2) $prevLink = "./page" . ($num-1); else if($num == 2) $hrefLink = "./"; // page 1 else $prevLink = "./page" . $lastNum; // last page // make the next link if($num >= $lastNum) $nextLink = "./"; // page 1 else $nextLink = "./page" . ($num+1); // output the prev/next links: echo "<p><a href='$prevLink'>Previous</a> <a href='{$nextLink}'>Next</a></p>"; Disclaimer: the examples on this page are just written off the top of my head and are not actually tested examples. You'll likely have to tweak them. In particular, you may have to add or subtract 1 in a few places to get the right numbers. If you end up adapting this, please let me know of any errors I have here so that I can correct them.
-
I like the potential of selecting multiple items and then being able to manipulate them as a group. For instance, like in a file system where you draw a selection around multiple files and then double click them, or move them, delete, etc. Though I think of pages on a web site as very different from files on a filesystem, because moving (where the URL changes) or deleting pages should be discouraged. One could break a whole lot of on-or-off-site links very easily. Still, this would be really handy in some cases, so I will be thinking about ways to implement it, perhaps combined with the tool described below… Actually ProcessWire 1 had something like this, called the "find change pages tool". But it was very different in how you selected pages. The way it worked is that it presented a comprehensive search engine to you, which covered every possible field that a page could have (it would create a selector for $pages->find behind the scenes). Then it would present the results and give you the option of modifying any field on the group of found pages. That field could be internal (like template or parent) or could be any of your custom fields. I haven't yet built this in PW2 because it's need doesn't come up so often, and when it does, it is so simple to do from an API shell script. But I know that the majority of people would not want to go create a shell script to modify pages in bulk, so this tool will make a reappearance in PW2 at some point. Thanks, Ryan
-
See the guide and examples in the ProcessWire MarkupPagerNav module information at: http://processwire.com/api/modules/markup-pager-nav/
-
Creating a sitemap is fairly easy in ProcessWire. The strategy we use is to get the page where we want the sitemap to start (like the homepage), print out it's children, and perform the same action on any children that themselves have children. We do this with a recursive function. Below is the contents of the sitemap.php template which demonstrates this. This example is also included in the default ProcessWire installation, but we'll go into more detail here. /site/templates/sitemap.php <?php function sitemapListPage($page) { // create a list item & link to the given page, but don't close the <li> yet echo "<li><a href='{$page->url}'>{$page->title}</a> "; // check if the page has children, if so start a nested list if($page->numChildren) { // start a nested list echo "<ul>"; // loop through the children, recursively calling this function for each foreach($page->children as $child) sitemapListPage($child); // close the nested list echo "</ul>"; } // close the list item echo "</li>"; } // include site header markup include("./head.inc"); // start the sitemap unordered list echo "<ul class='sitemap'>"; // get the homepage and start the sitemap sitemapListPage($pages->get("/")); // close the unordered list echo "</ul>"; // include site footer markup include("./foot.inc"); The resulting markup will look something like this (for the small default ProcessWire site): <ul class='sitemap'> <li><a href='/'>Home</a> <ul> <li><a href='/about/'>About</a> <ul> <li><a href='/about/child1/'>Child page example 1</a> </li> <li><a href='/about/child2/'>Child page example 2</a> </li> </ul> </li> <li><a href='/templates/'>Templates</a> </li> <li><a href='/site-map/'>Site Map</a> </li> </ul> </li> </ul> Note: to make this site map appear indented with each level, you may need to update your stylesheet with something like this: ul.sitemap li { margin-left: 2em; } The above sitemap template works well for a simple site. But what if you have some pages that have a "hidden" status? They won't appear in the sitemap, nor will any of their children. If you want them to appear, then you would want to manually add them to the what is displayed. To do this, retrieve the hidden page and send it to the sitemapListPage() function just like you did with the homepage: <?php // get the homepage and start the sitemap // (this line is included here just for placement context) sitemapListPage($pages->get("/")); // get our hidden page and include it in the site map sitemapListPage($pages->get("/some-hidden-page/")); What if your sitemap has thousands of pages? If you have a very large site, this strategy above may produce a sitemap with thousands of items and take a second or two to generate. A page with thousands of links may not be the most helpful sitemap strategy to your users, so you may want to consider alternatives. However, if you've decided you want to proceed, here is how to manage dealing with this many pages in ProcessWire. 1. First off you probably don't want to regenerate this sitemap for every pageview. As a result, you should enable caching if your template in: Admin > Setup > Templates > Sitemap > Advanced > Cache Time. I recommend setting it to one day (86400 seconds). Once you save this setting, the template will be rendered from a cache when the user is not logged in. Note that when you view it while still logged in, it's not going to use the cache… and that's okay. 2. Secondly, consider adding limits to the number of child pages you retrieve in the sitemapListPage function. It may be that you only need to list the first hundred child pages, in which case you could add a "limit=100" selector to your $page->children call: <?php // this example takes place inside the sitemapListPage function. // loop through the children, recursively calling this function for each: foreach($page->children("limit=100") as $child) sitemapListPage($child); 3. Loading thousands of pages (especially with lots of autojoined fields) may cause you to approach the memory limit of what Apache will allow for the request. If you are hitting a memory limit, you'll know it because ProcessWire will generate an error. If that happens, you need to manage your memory by freeing groups of pages once you no longer need them. Here's one strategy to use at the end of the sitemapListPage function that helps to ensure the memory allocated to the child pages is freed, making room for another thousand pages. <?php function sitemapListPage($page) { // ... everything above omitted for brevity in this example ... // close the list item echo "</li>"; // release loaded pages by telling the $pages variable to uncache them. // this will only uncache pages that are out of scope, so it's safe to use. wire('pages')->uncacheAll(); }
-
Right now it doesn't have anything that does this automatically. I left it out because I didn't want to assume what action you would want to take with duplicate page names, or what format you would want to use in generating a unique name. But perhaps I should add it as an optional module.
-
The URL name ($page->name) has to be unique for all sibling pages (i.e. pages having the same parent). I don't usually bother with checking unless ProcessWire throws an error about it during the import. If it looks like there are going to be duplicates, then here's how you'd ensure uniqueness by adding a number to the end of the URL name and keep incrementing it until it's unique: Replace this: $skyscraper->name = $building; $skyscraper->save(); With this (overly verbose for explanation purposes): <?php // just to turn on the forum syntax highlighting // Converts "Sears Tower" to "sears-tower" when setting the name $skyscraper->name = $building; // Retrieve the URL name, which should be "sears-tower" $name = $skyscraper->name; // a counter incremented each time a duplicate is found $n = 0; // find the first non-duplicate name while(count($parent->children("name=$name")) > 0) { $n++; $name = $skyscraper->name . $n; // i.e. sears-tower1, sears-tower2, etc. } // set the page's name to be one we know is unique, i.e. sears-tower1 $skyscraper->name = $name; // now when ProcessWire checks uniqueness before saving, it won't throw an error. $skyscraper->save();
-
ProcessWire provides a 404 page that you can modify and customize like any other (and you'll see it in your Page List). ProcessWire will automatically display that 404 page when someone attempts to access a non-existent URL. If one of your templates is configured to show a 404 page when the user doesn't have access to a page (in Templates > Template > Advanced Settings), then it'll show your 404 page in that instance as well. There may be instances where you want to trigger 404 pages on your own from your templates. To do this, use the following PHP snippet: throw new PageNotFoundException(); Once you do that, processing of your template will stop and ProcessWire will send a 404 header and display the default 404 page instead (thereby passing control to your 404 template). Please note: you should throw the PageNotFoundException before outputting any content in your template.
-
I exported a CSV file from freebase.com that contained all the skyscraper fields I wanted to use. Then I created a simple shell script to import it from the CSV. Note that I only used a shell script for convenience, you could just as easily do this from a ProcessWire template file if you preferred it or needed to do this from Windows, etc. Below is a simplified example of how to do this. The example is fictional and doesn't line up with the actual structure of the skyscrapers site, nor does it attempt to create page relations or import images. If you are interested in how to do that, let me know and I'll keep expanding on the example in this thread. But I wanted to keep it fairly simple to start. First, here is the contents of a CSV file with each line having a skyscraper building name, city, and height. /skyscrapers/skyscrapers.csv (Building, City, Height): Sears Tower, Chicago, 1400 John Hancock Tower, Chicago, 1210 Empire State Building, New York City, 1100 IBM Building, Atlanta, 860 Westin Peachtree, Atlanta, 790 Next, create a new template in ProcessWire and call it "skyscraper". Create a text field for "city", and an integer field for "height" and add them to the skyscraper template. Create a page called "/skyscrapers/" in ProcessWire, that will serve as the parent page for the skyscrapers we'll be adding. Here is the command-line script to load the ProcessWire API, read the CSV data, and create the pages. As I mentioned above, this could just as easily be done from a template, where the only difference would be that you wouldn't need the shebang (#!/usr/local/bin/php -q) at the beginning, nor would you need to include ProcessWire's index.php file. /skyscrapers/import_skyscrapers.sh: #!/usr/local/bin/php -q <?php // include ProcessWire's index file for API access // (this isn't necessary if you are doing this from a template file) include("./index.php"); $fp = fopen("./skyscrapers.csv", "r"); $template = wire('templates')->get("skyscraper"); $parent = wire('pages')->get("/skyscrapers/"); while(($data = fgetcsv($fp)) !== FALSE) { // create the page and set template and parent $skyscraper = new Page(); $skyscraper->template = $template; $skyscraper->parent = $parent; // set the skyscraper fields from the CSV list($building, $city, $height) = $data; $skyscraper->title = $building; $skyscraper->city = $city; $skyscraper->height = $height; // set the URL name, i.e. Sears Tower becomes "sears-tower" automatically $skyscraper->name = $building; // save the skyscraper $skyscraper->save(); echo "Created skyscraper: {$skyscraper->url}\n"; } To do the import, make the script executable and then run it from the command line: chmod +x .import_skyscrapers.sh ./import_skyscrapers.sh OR, if you are doing this from a template file, then load the page (that is using this template) in your web browser, and that will execute it. The output should be: Created skyscraper: /skyscrapers/sears-tower/ Created skyscraper: /skyscrapers/john-hancock-tower/ Created skyscraper: /skyscrapers/empire-state-building/ Created skyscraper: /skyscrapers/ibm-building/ Created skyscraper: /skyscrapers/westin-peachtree/ If you go into the ProcessWire admin, you should see your skyscrapers. I used an example of CSV file for simplicity, but the same method applies regardless of where you are pulling the data from (web service feeds, etc). For the actual skyscrapers demo site, I used Freebase's web services feed to pull the data and images, etc.
-
ProcessWire's Database class is extended from PHP's mysqli, so when you access the database you are dealing with a mysqli connection and all the functions and info in the PHP manual applies. From all templates, the $db instance is scoped locally, so you can do this (example): $result = $db->query("SELECT id, name, data FROM some_table"); while($row = $result->fetch_array()) print_r($row); From modules or any class extended from Wire, the database is scoped via $this->db: $result = $this->db->query("SELECT id, name, data FROM some_table"); while($row = $result->fetch_array()) print_r($row); From outside ProcessWire classes or in any regular function or non-ProcessWire class, the database can be accessed from the wire(...) function: $result = wire('db')->query("SELECT id, name, data FROM some_table"); while($row = $result->fetch_array()) print_r($row); Intended for use outside of ProcessWire templates and classes, that wire() function also provides access to all of ProcessWire's API variables, like $page, $pages, and others... i.e. wire('pages')->find('selector'); Also I should mention that I've never needed to use the $db from my templates. While it's there and ready for you to use, it's always preferable (not to mention easier and safer) to use ProcessWire's API for accessing any of it's data. If you need to create a connection to another database, then make a new mysqli connection (or PDO, regular mysql, etc, according to your preference). ProcessWire does not try to replace PHP's database functions. Good question, I've written a reply, but going to paste in a new thread so that the subject line matches the content.