Jump to content

Usage without DB I/O Polymorphism: Difference between revisions

From NusaATK
BERT (talk | contribs)
THIS IS DONE
 
 
(6 intermediate revisions by 2 users not shown)
Line 15: Line 15:
   {
   {
       $this->atkListAttribute($name, $optionArray, $valueArray, $flags, $size );
       $this->atkListAttribute($name, $optionArray, $valueArray, $flags, $size );
       // this attribute will only be visible in edit screens:
       // this attribute will only be visible in Add edit screen:
       $this->addFlag(AF_HIDE); $this->removeFlag(AF_HIDE_ADD);
       $this->addFlag(AF_HIDE); $this->removeFlag(AF_HIDE_ADD);
   }
   }
   function loadType()
   function loadType()
  {
  {
  // this makes sure this attribute does not partake in the query that loads
    // 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.
    // data. Since we aren't a real relationship, we don't have anything to load.
  return 0;
    return NOLOAD;
  }  
  }
   function storageType()
   function storageType()
   {
   {
       return 0;
       return NOSTORE;
   }
   }
}
}
</syntaxhighlight>
</syntaxhighlight>


== Using that attribute in your node ==
== Using that attribute in your node ==
Line 54: Line 55:
</syntaxhighlight>
</syntaxhighlight>


== Using it to polymorphism your admin list ==
== Using it to polymorphize your admin list ==
Lets put this list attribute in the Header area and use it to change our columns.
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.
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
  useattrib("yourmodule.listnoio");
  useattrib("yourmodule.listnoio");
Line 67: Line 68:
                               array( "A","N"), AF_HIDE|AF_LIST_NO_NULL_ITEM ));
                               array( "A","N"), AF_HIDE|AF_LIST_NO_NULL_ITEM ));


       // make it happen 'onChange' of the value and pass it back the one selected
       // make it happen 'onChange' of the value and pass back the one selected
       $attr->addOnChangeHandler( " window.location='".str_replace("'","\'",$_SERVER['REQUEST_URI'])."&chgadminview='+newvalue;");
       $attr->addOnChangeHandler( " window.location='".str_replace("'","\'",$_SERVER['REQUEST_URI']).
"&chgadminview='+newvalue;");
          
          
       // See if we already have an adminview value set      
       // See if we already have an adminview value set which we have stored in our session variables   
       $session = &atkSessionManager::getSession();
       $session = &atkSessionManager::getSession();
       $adminView = $session['adminview'];
       $adminView = $session['adminview'];
Line 77: Line 79:
       if ( isset( $ATK_VARS['chgadminview'])){       
       if ( isset( $ATK_VARS['chgadminview'])){       
           $adminView = $ATK_VARS['chgadminview'] ;           
           $adminView = $ATK_VARS['chgadminview'] ;           
           // save our admin value on our session stack so we can reference it when we
           // save our admin value on our session vars so we can reference it when we
           // come back to the admin screen
           // come back to the admin screen
           $session['adminview'] = $adminView;     
           $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' ) {   
       if ( isset($adminView) && $ATK_VARS['atkaction'] == 'admin' ) {   
           $attr->setSelected( $adminView );  // set the selected one as default
           $attr->setSelected( $adminView );  // set the selected one as default
Line 97: Line 101:
  // put the attribute in the Header area
  // put the attribute in the Header area
  function adminHeader(){
  function adminHeader(){
       // setup adminview list box for viewing just finals or final 4 
       // setup adminview list box for viewing just All and Not_All
       $attr=&$this->getAttribute("adminview");               
       $attr=&$this->getAttribute("adminview");               
       // please note: $rec is blank here
       // please note: $rec is blank here

Latest revision as of 07:55, 15 April 2008

ATK Howto: Usage without DB I/O Polymorphism

Complexity: Easy
Author: BERT

List of other Howto's

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;
 }
}