Usage without DB I/O Polymorphism

From Achievo/ATK Wiki

Jump to: navigation, search

ATK Howto: Usage without DB I/O Polymorphism

Complexity: Easy
Author: BERT

List of other Howto's

Contents

Intro

If you want to use the power of any attribute without having database I/O involved either in a normal node or maybe in the admin screen, like what the dummy attribute does. This how to will help you with that.


Creating a custom attribute without I/O

Create your attribute by extending the one you desire. In this case I'm extending the list or 'dropdown box'. You then need to change the loadType and storeType to return a value of 0 so it doesn't interface with the database.

useattrib("atklistattribute");
class listNoIO extends atkListAttribute
{
   function listNoIO($name, $optionArray, $valueArray="", $flags=0, $size=0  )
   {
       $this->atkListAttribute($name, $optionArray, $valueArray, $flags, $size );
       // this attribute will only be visible in Add edit screen:
       $this->addFlag(AF_HIDE); $this->removeFlag(AF_HIDE_ADD);
   }
 
   function loadType()
   {
     // this makes sure this attribute does not partake in the query that loads
     // data. Since we aren't a real relationship, we don't have anything to load.
     return NOLOAD;
   }
 
   function storageType()
   {
      return NOSTORE;
   }
}

Using that attribute in your node

Add it to your node. Upon Saving via the Edit or Add the $record will contain these values and can be used in places like validate or postUpdate

 useattrib("yourmodule.listnoio");
 
 class yournode extends atkNode {
   function yournode () {
    $attr = &$this->add(new listNoIO( "adminview",
                              array( "All","Not_All") ,
                              array( "A","N"), AF_LIST_NO_NULL_ITEM ));
 }
 
 function postUpdate(&$record) 
     {     
     if ( $record['adminview'] == 'N' ) {
        $this->doSomethingSpecial( $record );
     }
     return true;
   }  
 }

Using it to polymorphize your admin list

Lets put this list attribute in the Header area and use it to change our columns. We will assume there are other attributes in our node, some which appear on the admin page others which aren't.

 useattrib("yourmodule.listnoio");
 
 class yournode extends atkNode {
   function yournode () {
    GLOBAL $ATK_VARS;
    // change it to always be hidden
    $attr = &$this->add(new listNoIO( "adminview",
                              array( "All","Not_All") ,
                              array( "A","N"), AF_HIDE|AF_LIST_NO_NULL_ITEM ));
 
      // make it happen 'onChange' of the value and pass back the one selected
      $attr->addOnChangeHandler( " window.location='".str_replace("'","\'",$_SERVER['REQUEST_URI']).
"&chgadminview='+newvalue;");
        
      // See if we already have an adminview value set which we have stored in our session variables     
      $session = &atkSessionManager::getSession();
      $adminView = $session['adminview'];
  
      // check to see if our adminview dropdown was changed
      if ( isset( $ATK_VARS['chgadminview'])){      
           $adminView = $ATK_VARS['chgadminview'] ;           
           // save our admin value on our session vars so we can reference it when we
           // come back to the admin screen
           $session['adminview'] = $adminView;    
      }             
      
      // if the adminView exists and we are in admin mode then test to see if we need to add more
      // fields to our list
      if ( isset($adminView) && $ATK_VARS['atkaction'] == 'admin' ) {   
          $attr->setSelected( $adminView );   // set the selected one as default
           if    ( $adminView == 'N' ) {
               // lets say we have 2 attributes which are normally not on the 
               // admin screen and we want to put them there
              foreach ( array('Attr1','Attr2') as $var ) {
                $attr = &$this->getAttribute( $var );
                $attr->removeFlag( AF_HIDE_LIST );
              }
               
           } 
 }
 
 // put the attribute in the Header area
 function adminHeader(){
       // setup adminview list box for viewing just All and Not_All 
       $attr=&$this->getAttribute("adminview");              
       // please note: $rec is blank here
       $help_text =  '<tr><td align="left">'."VIEW: ".$attr->edit( $rec, 'edit') . '</td></tr>';
   return $help_text;
 }
}
Personal tools
Navigation