1. <?php
  2. /**
  3. * Creates the page for gathering information about developers.
  4. *
  5. * The add maintainer page needs to get the developer's name,
  6. * handle, email address and role.
  7. *
  8. * @category   PEAR
  9. * @package    PEAR_PackageFileManager_Frontend_Web
  10. * @author     Laurent Laville <pear@laurent-laville.org>
  11. * @copyright  2005-2006 Laurent Laville
  12. * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
  13. * @since      File available since Release 0.1.0
  14. */
  15.  
  16. require_once 'PEAR/PackageFileManager/Frontend/Decorator/HTMLTable.php';
  17.  
  18. /**
  19. * Creates the page for gathering information about developers.
  20. *
  21. * @category   PEAR
  22. * @package    PEAR_PackageFileManager_Frontend_Web
  23. * @author     Laurent Laville <pear@laurent-laville.org>
  24. * @copyright  2005-2006 Laurent Laville
  25. * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
  26. * @since      Class available since Release 0.1.0
  27. */
  28. class MaintainersPage extends TabbedPage
  29. {
  30.     /**
  31.      * Builds the current form-page.
  32.      *
  33.      * @since  0.1.0
  34.      * @access public
  35.      */
  36.     function buildForm()
  37.     {
  38.         $this->buildTabs();
  39.         // tab caption
  40.         $this->addElement('header', null, 'Manage the list of maintainers');
  41.  
  42.         $fe =& PEAR_PackageFileManager_Frontend::singleton();
  43.         $sess =& $fe->container();
  44.  
  45.         $selection = $this->getSubmitValue('users');
  46.         $selection_count = count($selection);
  47.         $fe->log('debug',
  48.             str_pad($this->getAttribute('id') .'('. __LINE__ .')', 20, '.') .
  49.             ' selection='. serialize($selection)
  50.         );
  51.         list($page, $action) = $this->controller->getActionName();
  52.  
  53.         // selection list (false) or edit dialog frame (true)
  54.         if ($action == 'edit' && $selection_count == 1) {
  55.             $editDialog = true;
  56.         } elseif ($action == 'add' && $selection_count == 0) {
  57.             $editDialog = true;
  58.         }elseif ($action == 'save') {
  59.             $editDialog = true;
  60.         } else {
  61.             $editDialog = false;
  62.         }
  63.  
  64.         $leads = $fe->getMaintList('lead');
  65.         if ($leads !== false) {
  66.             $leads = true;
  67.         }
  68.         // at least a package lead is mandatory; used for form validation
  69.         $this->addElement('hidden', 'leads', $leads);
  70.         $this->setConstants($leads);
  71.         $this->addRule('leads', 'You must specify a lead', 'nonzero');
  72.         $maintainers = $fe->getMaintList();
  73.         // set default maintainers list used when we click on 'Reset' button
  74.         $fe->setDefaults('maintainers', $maintainers);
  75.  
  76.         if (!$editDialog) {
  77.  
  78.             $hdr = array('Handle', 'Name', 'Email', 'Role', 'Active');
  79.             $table = new HTML_Table(array('class' => 'tableone'));
  80.             $htmltableDecorator = new PEAR_PackageFileManager_Frontend_Decorator_HTMLTable($fe);
  81.             $htmltableDecorator->setHtmlTable($table);
  82.             $htmltableDecorator->getMaintList(null, $hdr);
  83.             // We need a simple static html area for maintainers list.
  84.             $this->addElement('static', 'maintainers', '', $htmltableDecorator->toHtml() );
  85.  
  86.             $commands = array('drop', 'edit', 'add');
  87.             $nocmd    = array('commit');
  88.  
  89.         } else {
  90.  
  91.             // Role options list: (value => text, with value === text)
  92.             $settings = $fe->getOption(array('settings', 'pfm'), false);
  93.             $roles = $settings['pfm']['maintainer_roles'];
  94.             sort($roles, SORT_ASC);
  95.             $roles = array_combine($roles, $roles);
  96.  
  97.             // Active options list: (value => text, with value === text)
  98.             $activ = array('yes', 'no');
  99.             $activ = array_combine($activ, $activ);
  100.  
  101.             $this->addElement('text', 'handle', 'Handle:', array('size' => 20));
  102.             $this->addElement('text', 'name'  , 'Name:'  , array('size' => 40));
  103.             $this->addElement('text', 'email' , 'Email:' , array('size' => 40));
  104.             $this->addElement('select', 'role', 'Role:', $roles);
  105.             $this->addElement('select', 'active', 'Active:', $activ);
  106.  
  107.             if ($selection_count == 0) {
  108.                 $key1 = -1;
  109.                 $def = array('role' => 'lead', 'active' => 'yes');
  110.             } else {
  111.                 $needle = array_keys($selection);
  112.                 $key1   = array_pop($needle);
  113.                 $def = array(
  114.                     'handle' => $maintainers[$key1]['handle'],
  115.                     'name'   => $maintainers[$key1]['name'],
  116.                     'email'  => $maintainers[$key1]['email'],
  117.                     'role'   => $maintainers[$key1]['role'],
  118.                     'active' => $maintainers[$key1]['active']
  119.                 );
  120.             }
  121.             $this->addElement('hidden', 'userid', $key1);
  122.  
  123.             // applies new filters to the element values
  124.             $this->applyFilter('__ALL__', 'trim');
  125.             // form rules
  126.             $this->addRule('handle','The handle of maintainer is required', 'required');
  127.             $this->addRule('name''The name of maintainer is required'  , 'required');
  128.             $this->addRule('email', 'The email of maintainer is required' , 'required');
  129.             $this->addRule('email', 'Wrong email format'                  , 'email');
  130.  
  131.             // old values of edit user
  132.             $this->setDefaults($def);
  133.  
  134.             $commands = array('save', 'cancel');
  135.             $nocmd    = array('commit','reset');
  136.         }
  137.  
  138.         // Buttons of the wizard to do the job
  139.         $this->buildButtons($nocmd, $commands);
  140.     }
  141.  
  142.     /**
  143.      * Sets the default values for the maintainers page
  144.      *
  145.      * @return void
  146.      * @since  0.1.0
  147.      * @access public
  148.      * @see    HTML_QuickForm_Controller::applyDefaults()
  149.      */
  150.     function applyDefaults()
  151.     {
  152.         list($page, $action) = $this->controller->getActionName();
  153.         $fe =& PEAR_PackageFileManager_Frontend::singleton();
  154.         $fe->log('debug',
  155.             str_pad($this->getAttribute('id') .'('. __LINE__ .')', 20, '.') .
  156.             " applyDefaults ActionName=($page,$action)"
  157.         );
  158.  
  159.         // apply only when in list mode,
  160.         if ($action == 'reset') {
  161.             $sess =& $fe->container();
  162.             if (isset($sess['defaults']['_maintainers']) && $sess['defaults']['_maintainers']) {
  163.                 $maintainers = $fe->getMaintList();
  164.                 if ($maintainers) {
  165.                     foreach($maintainers as $maintainer) {
  166.                         $fe->deleteMaintainer($maintainer['handle']);
  167.                     }
  168.                 }
  169.                 foreach($sess['defaults']['_maintainers'] as $maintainer) {
  170.                     extract($maintainer);
  171.                     $fe->addMaintainer($role, $handle, $name, $email, $active);
  172.                 }
  173.             }
  174.         }
  175.     }
  176. }
  177.  
  178. /**
  179. * Manage actions to add or edit information about developers.
  180. *
  181. * @category   PEAR
  182. * @package    PEAR_PackageFileManager_Frontend_Web
  183. * @author     Laurent Laville <pear@laurent-laville.org>
  184. * @copyright  2005-2006 Laurent Laville
  185. * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
  186. * @since      Class available since Release 0.1.0
  187. */
  188. class MaintainersPageAction extends HTML_QuickForm_Action
  189. {
  190.     /**
  191.      * Processes the request.
  192.      *
  193.      * @param  object   HTML_QuickForm_Page  the current form-page
  194.      * @param  string   Current action name, as one Action object can serve multiple actions
  195.      * @since  0.1.0
  196.      * @access public
  197.      */
  198.     function perform(&$page, $actionName)
  199.     {
  200.         $page->isFormBuilt() or $page->buildForm();
  201.         $pageName = $page->getAttribute('id');
  202.         $fe =& PEAR_PackageFileManager_Frontend::singleton();
  203.         $fe->log('debug',
  204.             str_pad($pageName .'('. __LINE__ .')', 20, '.') .
  205.             ' ActionProcess='. $actionName
  206.         );
  207.  
  208.         if ($actionName == 'add' || $actionName == 'edit' || $actionName == 'cancel') {
  209.             return $page->handle('display');
  210.         }
  211.  
  212.         // save the form values and validation status to the session
  213.         $sess =& $fe->container();
  214.         $sess['values'][$pageName] = $page->exportValues();
  215.         $sess['valid'][$pageName]  = $page->validate();
  216.  
  217.         if (isset($sess['valid'][$pageName]) && $sess['valid'][$pageName]) {
  218.  
  219.             switch ($actionName) {
  220.                 case 'drop':
  221.                     $selection = $page->getSubmitValue('users');
  222.                     if (is_array($selection)) {
  223.                         $maintainers = $fe->getMaintList();
  224.                         $keys = array_keys($selection);
  225.                         foreach ($keys as $key1) {
  226.                             $fe->log('info',
  227.                                 str_pad($pageName .'('. __LINE__ .')', 20, '.') .
  228.                                 ' drop maintainer: "'. $maintainers[$key1]['handle']
  229.                                 .'" ('. $maintainers[$key1]['role'] .')'
  230.                             );
  231.                             $fe->deleteMaintainer($maintainers[$key1]['handle']);
  232.                         }
  233.                     }
  234.                     break;
  235.                 case 'save':
  236.                     $data = $page->exportValues(array('handle','name','email','role','active'));
  237.                     extract($data);
  238.                     $key1 = $sess['values'][$pageName]['userid'];
  239.                     if ($key1 < 0) {
  240.                         $sess['pfm']->addMaintainer($role, $handle, $name, $email, $active);
  241.                         $fe->log('info',
  242.                              str_pad($pageName .'('. __LINE__ .')', 20, '.') .
  243.                              ' add maintainer: "'. $data['handle'] .'" ('. $data['role'] .')'
  244.                         );
  245.                     } else {
  246.                         $sess['pfm']->updateMaintainer($role, $handle, $name, $email, $active);
  247.                         $fe->log('info',
  248.                              str_pad($pageName .'('. __LINE__ .')', 20, '.') .
  249.                              ' edit maintainer: "'. $data['handle'] .'" ('. $data['role'] .')'
  250.                         );
  251.                     }
  252.                     break;
  253.             }
  254.             return $page->handle('jump');
  255.         }
  256.         return $page->handle('display');
  257.     }
  258. }
  259. ?>