How would I do this?

Permalink 1 user found helpful
I'm trying to write a blogging application that allows users to share items based on a select attribute. It's working to display pages in a page list block, but what I'd like to do is alias the page to multiple locations based on which items in the select list you add.

So I have a form that creates the blog post, and it displays an attribute form as an array of checkboxes, the user can't add to the list. They submit the form, the page is created and the attribute is saved against the page.

What should happen next is all aliases of the page should be removed, I'm not sure if there's a shorthand way to do that, remove aliases of a particular page site wide but keep the original page.

What I would like to happen after that is to create a page list, filtered to a particular page type (location/blog/list view) that have selected that same attribute value. Once I have that filtered list, the page would be aliased underneath each page that has matching attributes.

Is this even possible? It seems like it should be but I'm kind of drawing a blank on going from post variables to a filtered page list, I don't know how to get the values from the IDs that I'd have in the post array.

Any insight on how to do this would be appreciated. I'm trying to alias the pages so that I can use Scott C's Item Calendar to display all pages of a particular type underneath a particular page on the calendar. I need a way to add and remove page aliases underneath other pages based on a matching attribute selection. It seems like this should be doable, but I can't figure it out. Otherwise I can just do it as blog posts with no calendar integration but I really like the idea of incorporating the calendar into the posting, just add the calendar fields and add the page.

hereNT
 
12345j replied on at Permalink Reply
12345j
I think you can delete an aliases with this:
Page::getCurrentPage()->removeThisAlias();

but i'm not sure. so maybe something like this?
$db=loader::db();
$aliasedPages=$db->getAll('SELECT cID FROM Pages WHERE cPointerID=?',array(Page::getCurrentPage->getCollectionID());
foreach($aliasedPages as $cID){
    Page::getByID($cID)->removeThisAlias();
}

and then I would add 2 new arguments to Page::addCollectionAlias for the page type handle you want and the parent page id, and remove lines 463,472,473. I'd do an array_search to check for all the values, and then based on what returns true determine your two arguments. something like this
$attributeValues=array('announcement'=>'announcement_page_type,79','feature'=>'feature,81');//this should be an array of the possible blog post attribute values that map to a page type and then the collection id of the parent page the page should be aliased under if it has the attribute value selected.
//so for announcement, if the value announcement was selected in the post attributes then the page type for the alias should be announcement_page_type, and the parent page id would be 79
foreach($attributeValues as $key=>$value){
    if(array_search($key,$_POST['selectAttributeValues'])){//you've got a positive result
        $vals=explode(',',$value);
        Page::addCollectionAlias($page,$vals[0],$vals[1])//$page here should be the unaliased page you're adding via the blog post, and then I'm saying the page type as the next arg, and the new parent page id as the second.
    }
}

I also found this, which may help some.
http://blog.ideaday.de/max/2010/04/adding-aliases-to-concrete5-page...
does this make any sense?
hereNT replied on at Permalink Reply
hereNT
I need something much cleaner for aliasing the pages, I'm not going to know the IDs of every page that meets the criteria. So if the new page has attribute id x selected and the new parent page has attribute id x selected then an alias is created. The parent pages are all always going to be of the same type and there will only be one type of page, it's basically a 'blog list' view page.

Once I can figure out how to make the aliases then I'll try your code for removing them, that looks like it should work.
12345j replied on at Permalink Reply
12345j
I'm not sure I understand what you're trying to do then. I thought that the flow was
add a blog post, then alias the blog post page under parent pages depending on the select options selected. The aliased page should have the same page type as the parent.
or is that not it?
hereNT replied on at Permalink Reply
hereNT
The aliased page would be of type user_blog_post and the parent page would be of type location. They would both have a attribute "sharing_location" that is a select attribute type. If the same attribute options are selected for both the blog post and the location, then alias the blog post under the location.
12345j replied on at Permalink Reply
12345j
try this then. untested, but I think I understand what you're saying. point to this function with the on_start event.
public function checkBlogPosts($current){
   if($page->getCollectionTypeHandle()=='location'){
      Loader::model('page_list');
      loader::model('collection_type');
      $pl = new PageList();
      $pl->filterByCollectionTypeID(CollectionType::getByHandle('user_blog_post')->getCollectionTypeID());
      $pages = $pl->get(10000);
      $vals=$current->getAttribute('selectOptionValues');
      $num=count($vals);
      foreach($pages as $page){
         $options=$page->getAttribute('selectOptionValues');
         $i=0;
         while($i<$num&&!$bad)
            if(!array_search($vals[$i], $options)){
               $bad=true;
hereNT replied on at Permalink Best Answer Reply
hereNT
This is what I ended up going with. Inside of the add function from my single page that is adding the user blog post:

$this->saveData($p);
Loader::model('page_list');
$pl = new PageList();
$pl->filterByCollectionTypeHandle('location');
$ak = CollectionAttributeKey::getByHandle('greaserag_location');
$location_id = $_POST['akID']['36']['atSelectOptionID']['0'];
foreach ($pl->get() as $locationPage){
   $locationPageLocation = $locationPage->getAttribute('greaserag_location');
   foreach($locationPageLocation as $location){
      if ($location->ID == $location_id){
         $aliasCID = $p->addCollectionAlias($locationPage);
         $aliasPage = Page::getByID($aliasCID);
         $aliasPage->setAttribute("exclude_nav", true);
      }
   }


I had to hard code in the form value for ID, because it is a newly created page I couldn't use get attribute on the $p object because it returns an empty array until after the code creating it is done executing. I've never figured out a way around that, but this seems to work.

Then inside of the "Edit" function, this is what I put:

$db = Loader::db();
$aliasedPages = $db->getAll('SELECT cID FROM Pages WHERE cPointerID=?', array($p->getCollectionID()));
foreach ($aliasedPages as $cID){
   Page::getByID($cID)->removeThisAlias();
}
$this->saveData($p);
Loader::model('page_list');
$pl = new PageList();
$pl->filterByCollectionTypeHandle('location');
$p_location = $p->getAttribute('greaserag_location');
foreach ($pl->get() as $locationPage){
   $locationPageLocation = $locationPage->getAttribute('greaserag_location');
   foreach($locationPageLocation as $location){
      if ($p_location->contains($location)){
         $aliasCID = $p->addCollectionAlias($locationPage);


For some reason in this code I can just call $p->getAttribute and it returns the newly saved value. I think it has something to do with the page being a newly created page so getAttribute is looking at a blank object.

At any rate, it seems to be working, aliases are added to the pages properly and thus show up on the proper calendars in the right spots, I guess this really was a little easier than I made it sound trying to figure it out.