Advertisement
Guest User

IPS 4: Node Example

a guest
Dec 10th, 2013
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.57 KB | None | 0 0
  1. <?php
  2.  
  3. namespace IPS\downloads;
  4.  
  5. /**
  6.  * Category Node
  7.  */
  8. class _Category extends \IPS\Node\Model implements \IPS\Node\Permissions
  9. {
  10.     /**
  11.      * @brief   [ActiveRecord] Multiton Store
  12.      */
  13.     protected static $multitons;
  14.    
  15.     /**
  16.      * @brief   [ActiveRecord] Default Values
  17.      */
  18.     protected static $defaultValues = NULL;
  19.    
  20.     /**
  21.      * @brief   [ActiveRecord] Database Table
  22.      */
  23.     public static $databaseTable = 'downloads_categories';
  24.    
  25.     /**
  26.      * @brief   [ActiveRecord] Database Prefix
  27.      */
  28.     public static $databasePrefix = 'c';
  29.        
  30.     /**
  31.      * @brief   [Node] Order Database Column
  32.      */
  33.     public static $databaseColumnOrder = 'position';
  34.    
  35.     /**
  36.      * @brief   [Node] Parent ID Database Column
  37.      */
  38.     public static $databaseColumnParent = 'parent';
  39.    
  40.     /**
  41.      * @brief   [Node] Node Title
  42.      */
  43.     public static $nodeTitle = 'categories';
  44.            
  45.     /**
  46.      * @brief   [Node] ACP Restrictions
  47.      * @code
  48.         array(
  49.             'app'       => 'core',              // The application key which holds the restrictrions
  50.             'module'    => 'foo',               // The module key which holds the restrictions
  51.             'map'       => array(               // [Optional] The key for each restriction - can alternatively use "prefix"
  52.                 'add'           => 'foo_add',
  53.                 'edit'          => 'foo_edit',
  54.                 'permissions'   => 'foo_perms',
  55.                 'delete'        => 'foo_delete'
  56.             ),
  57.             'all'       => 'foo_manage',        // [Optional] The key to use for any restriction not provided in the map (only needed if not providing all 4)
  58.             'prefix'    => 'foo_',              // [Optional] Rather than specifying each  key in the map, you can specify a prefix, and it will automatically look for restrictions with the key "[prefix]_add/edit/permissions/delete"
  59.      * @encode
  60.      */
  61.     protected static $restrictions = array(
  62.         'app'       => 'downloads',
  63.         'module'    => 'downloads',
  64.         'prefix' => 'categories_'
  65.     );
  66.    
  67.     /**
  68.      * @brief   [Node] App for permission index
  69.      */
  70.     public static $permApp = 'downloads';
  71.    
  72.     /**
  73.      * @brief   [Node] Type for permission index
  74.      */
  75.     public static $permType = 'category';
  76.    
  77.     /**
  78.      * @brief   The map of permission columns
  79.      */
  80.     public static $permissionMap = array(
  81.         'view'              => 'view',
  82.         'read'              => 2,
  83.         'add'               => 3,
  84.         'download'          => 4,
  85.         'reply'             => 5,
  86.         'review'            => 6
  87.     );
  88.    
  89.     /**
  90.      * @brief   Bitwise values for members_bitoptions field
  91.      */
  92.     public static $bitOptions = array(
  93.         'bitoptions' => array(
  94.             'bitoptions' => array(
  95.                 'moderation'            => 1,   // Require files to be approved?
  96.                 'comment_moderation'    => 2,   // Require comments to be approved?
  97.                 'reviews_mod'           => 4,   // Reviews must be approved?
  98.             )
  99.         )
  100.     );
  101.  
  102.     /**
  103.      * @brief   [Node] Title search prefix.  If specified, searches for '_title' will be done against the language pack.
  104.      */
  105.     public static $titleSearchPrefix = 'downloads_category_';
  106.    
  107.     /**
  108.      * @brief   [Node] Moderator Permission
  109.      */
  110.     public static $modPerm = 'download_categories';
  111.    
  112.     /**
  113.      * @brief   Follow Area Key
  114.      */
  115.     public static $followArea = 'category';
  116.    
  117.     /**
  118.      * [Node] Get title
  119.      *
  120.      * @return  string
  121.      */
  122.     protected function get__title()
  123.     {
  124.         return \IPS\Member::loggedIn()->language()->get("downloads_category_{$this->id}");
  125.     }
  126.    
  127.     /**
  128.      * [Node] Get whether or not this node is enabled
  129.      *
  130.      * @note    Return value NULL indicates the node cannot be enabled/disabled
  131.      * @return  bool|null
  132.      */
  133.     protected function get__enabled()
  134.     {
  135.         return $this->open;
  136.     }
  137.    
  138.     /**
  139.      * [Node] Set whether or not this node is enabled
  140.      *
  141.      * @param   bool|int    $enabled    Whether to set it enabled or disabled
  142.      * @return  void
  143.      */
  144.     protected function set__enabled( $enabled )
  145.     {
  146.         $this->open = $enabled;
  147.     }
  148.    
  149.     /**
  150.      * [Node] Add/Edit Form
  151.      *
  152.      * @param   \IPS\Helpers\Form   $form   The form
  153.      * @return  void
  154.      */
  155.     public function form( &$form )
  156.     {
  157.         $form->add( new \IPS\Helpers\Form\Translatable( 'cname', NULL, TRUE, array( 'app' => 'downloads', 'key' => ( $this->id ? "downloads_category_{$this->id}" : NULL ) ) ) );
  158.         $form->add( new \IPS\Helpers\Form\Translatable( 'cdesc', NULL, FALSE, array( 'app' => 'downloads', 'key' => ( $this->id ? "downloads_category_{$this->id}_desc" : NULL ), 'editor' => array( 'app' => 'downloads', 'key' => 'Categories', 'autoSaveKey' => ( $this->id ? "downloads-cat-{$this->id}" : "downloads-new-cat" ), 'attachIds' => $this->id ? array( $this->id, NULL, 'description' ) : NULL, 'minimize' => 'cdesc_placeholder' ) ) ) );
  159.  
  160.         $form->add( new \IPS\Helpers\Form\YesNo( 'cbitoptions_moderation', $this->bitoptions['moderation'] ) );
  161.         $form->add( new \IPS\Helpers\Form\YesNo( 'cbitoptions_comment_moderation', $this->bitoptions['comment_moderation'] ) );
  162.         $form->add( new \IPS\Helpers\Form\YesNo( 'cbitoptions_reviews_mod', $this->bitoptions['reviews_mod'] ) );
  163.  
  164.         // etc...
  165.     }
  166.    
  167.     /**
  168.      * [Node] Save Add/Edit Form
  169.      *
  170.      * @param   array   $values Values from the form
  171.      * @return  void
  172.      */
  173.     public function saveForm( $values )
  174.     {
  175.         if ( !$this->id )
  176.         {
  177.             $this->save();
  178.         }
  179.                
  180.         foreach ( array( 'cname' => "downloads_category_{$this->id}", 'cdesc' => "downloads_category_{$this->id}_desc" ) as $fieldKey => $langKey )
  181.         {
  182.             \IPS\Lang::saveCustom( 'downloads', $langKey, $values[ $fieldKey ] );
  183.             unset( $values[ $fieldKey ] );
  184.         }
  185.        
  186.         foreach ( array( 'moderation', 'comment_moderation', 'reviews_mod' ) as $k )
  187.         {
  188.             $this->bitoptions[ $k ] = $values["cbitoptions_{$k}"];
  189.             unset( $values["cbitoptions_{$k}"] );
  190.         }
  191.        
  192.         parent::saveForm( $values );
  193.     }
  194.    
  195.     /**
  196.      * Get URL
  197.      *
  198.      * @return  \IPS\Http\Url
  199.      */
  200.     public function url()
  201.     {
  202.         return \IPS\Http\Url::internal( "app=downloads&module=downloads&controller=browse&id={$this->_id}", 'front', 'downloads_cat', $this->name_furl );
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement