1. <?php
  2. /**
  3. * Creates the page to specify file role for specific files.
  4. *
  5. * @category   PEAR
  6. * @package    PEAR_PackageFileManager_Frontend_Web
  7. * @author     Laurent Laville <pear@laurent-laville.org>
  8. * @copyright  2005-2006 Laurent Laville
  9. * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
  10. * @since      File available since Release 0.1.0
  11. */
  12.  
  13. require_once 'PEAR/PackageFileManager/Frontend/Decorator/HTMLTable.php';
  14.  
  15. /**
  16. * Creates the page to specify file role for specific files.
  17. *
  18. * @category   PEAR
  19. * @package    PEAR_PackageFileManager_Frontend_Web
  20. * @author     Laurent Laville <pear@laurent-laville.org>
  21. * @copyright  2005-2006 Laurent Laville
  22. * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
  23. * @since      Class available since Release 0.1.0
  24. */
  25. class ExceptionsPage extends TabbedPage
  26. {
  27.     /**
  28.      * Builds the current form-page.
  29.      *
  30.      * @since  0.1.0
  31.      * @access public
  32.      */
  33.     function buildForm()
  34.     {
  35.         $this->buildTabs();
  36.         // tab caption
  37.         $this->addElement('header', null, 'Specify file role for specific files');
  38.  
  39.         $fe =& PEAR_PackageFileManager_Frontend::singleton();
  40.         $sess =& $fe->container();
  41.  
  42.         $selection = $this->getSubmitValue('files');
  43.         $selection_count = count($selection);
  44.         $fe->log('debug',
  45.             str_pad($this->getAttribute('id') .'('. __LINE__ .')', 20, '.') .
  46.             ' selection='. serialize($selection)
  47.         );
  48.  
  49.         list($page, $action) = $this->controller->getActionName();
  50.  
  51.         // selection list (false) or edit dialog frame (true)
  52.         if ($action == 'edit' && $selection_count == 1) {
  53.             $editDialog = true;
  54.         }elseif ($action == 'save') {
  55.             $editDialog = true;
  56.         } else {
  57.             $editDialog = false;
  58.         }
  59.  
  60.         if (!$editDialog) {
  61.  
  62.             $hdr = array('Path', 'Role');
  63.             $table = new HTML_Table(array('class' => 'tableone'));
  64.             $htmltableDecorator = new PEAR_PackageFileManager_Frontend_Decorator_HTMLTable($fe);
  65.             $htmltableDecorator->setHtmlTable($table);
  66.             $htmltableDecorator->getExceptionList($hdr);
  67.             // We need a simple static html area for maintainers list.
  68.             $this->addElement('static', 'exceptions', '', $htmltableDecorator->toHtml());
  69.  
  70.             $commands = array('edit', 'remove');
  71.             $nocmd    = array('commit');
  72.  
  73.         } else {
  74.  
  75.             // Role options list: (value => text, with value === text)
  76.             require_once 'PEAR/Installer/Role.php';
  77.             $pageName = $fe->getPageName('page1');
  78.             $releaseType = $fe->exportValue($pageName, 'packageType');
  79.             $roles = PEAR_Installer_Role::getValidRoles($releaseType);
  80.             $roles[] = '';
  81.             sort($roles, SORT_ASC);
  82.             $roles = array_combine($roles, $roles);
  83.  
  84.             $ePath =& $this->addElement('text', 'path', 'Path:');
  85.             $ePath->freeze();
  86.             $this->addElement('select', 'role', 'Role:', $roles);
  87.  
  88.             if ($selection_count == 0) {
  89.                 $key1 = -1;
  90.                 $def = array();
  91.             } else {
  92.                 $needle = array_keys($selection);
  93.                 $key1   = array_shift($needle);
  94.                 $def = array(
  95.                     'path' => $sess['files']['mapping'][$key1],
  96.                     'role' => $sess['files'][$key1]['role']
  97.                 );
  98.             }
  99.             $this->addElement('hidden', 'fileid', $key1);
  100.  
  101.             // applies new filters to the element values
  102.             $this->applyFilter('__ALL__', 'trim');
  103.  
  104.             // old values of edit user
  105.             $this->setDefaults($def);
  106.  
  107.             $commands = array('save', 'cancel');
  108.             $nocmd    = array('commit','reset');
  109.         }
  110.  
  111.         // Buttons of the wizard to do the job
  112.         $this->buildButtons($nocmd, $commands);
  113.     }
  114.  
  115.     /**
  116.      * Sets the default values for the exceptions page
  117.      *
  118.      * @return void
  119.      * @since  0.1.0
  120.      * @access public
  121.      * @see    HTML_QuickForm_Controller::applyDefaults()
  122.      */
  123.     function applyDefaults()
  124.     {
  125.         list($page, $action) = $this->controller->getActionName();
  126.         $fe =& PEAR_PackageFileManager_Frontend::singleton();
  127.         $fe->log('debug',
  128.             str_pad($this->getAttribute('id') .'('. __LINE__ .')', 20, '.') .
  129.             " applyDefaults ActionName=($page,$action)"
  130.         );
  131.  
  132.         if ($action == 'reset') {
  133.             $sess =& $fe->container();
  134.             foreach ($sess['files'] as $key1 => $file) {
  135.                 $sess['files'][$key1]['role'] = '';
  136.             }
  137.         }
  138.     }
  139. }
  140.  
  141. /**
  142. * Manage actions to add exception on roles mapping for specific files.
  143. *
  144. * @category   PEAR
  145. * @package    PEAR_PackageFileManager_Frontend_Web
  146. * @author     Laurent Laville <pear@laurent-laville.org>
  147. * @copyright  2005-2006 Laurent Laville
  148. * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
  149. * @since      Class available since Release 0.1.0
  150. */
  151. class ExceptionsPageAction extends HTML_QuickForm_Action
  152. {
  153.     /**
  154.      * Processes the request.
  155.      *
  156.      * @param  object   HTML_QuickForm_Page  the current form-page
  157.      * @param  string   Current action name, as one Action object can serve multiple actions
  158.      * @since  0.1.0
  159.      * @access public
  160.      */
  161.     function perform(&$page, $actionName)
  162.     {
  163.         $page->isFormBuilt() or $page->buildForm();
  164.         $pageName = $page->getAttribute('id');
  165.         $fe =& PEAR_PackageFileManager_Frontend::singleton();
  166.         $fe->log('debug',
  167.             str_pad($pageName .'('. __LINE__ .')', 20, '.') .
  168.             ' ActionProcess='. $actionName
  169.         );
  170.  
  171.         if ($actionName == 'edit' || $actionName == 'cancel') {
  172.             return $page->handle('display');
  173.         }
  174.  
  175.         // save the form values and validation status to the session
  176.         $sess =& $fe->container();
  177.         $sess['values'][$pageName] = $page->exportValues();
  178.         $sess['valid'][$pageName]  = $page->validate();
  179.  
  180.         if (isset($sess['valid'][$pageName]) && $sess['valid'][$pageName]) {
  181.  
  182.             switch ($actionName) {
  183.                 case 'save':
  184.                     $data = $page->exportValue('role');
  185.                     $key1 = $sess['values'][$pageName]['fileid'];
  186.                     $sess['files'][$key1]['role'] = $data;
  187.                     $fe->log('info',
  188.                         str_pad($pageName .'('. __LINE__ .')', 20, '.') .
  189.                         ' add exception: "'. $data .'" for "'. $sess['files'][$key1]['path'] .'"'
  190.                     );
  191.                     break;
  192.                 case 'remove':
  193.                     $selection = $page->getSubmitValue('files');
  194.                     if (is_array($selection)) {
  195.                         $keys = array_keys($selection);
  196.                         foreach ($keys as $k) {
  197.                             $sess['files'][$k]['role'] = '';
  198.                         }
  199.                     }
  200.                     break;
  201.             }
  202.             return $page->handle('jump');
  203.         }
  204.         return $page->handle('display');
  205.     }
  206. }
  207. ?>