/php/forms/box_external_repository_manager_form.class.php
PHP | 155 lines | 118 code | 33 blank | 4 comment | 10 complexity | b6a2737098093337d0033521c1bae261 MD5 | raw file
1<?php 2namespace common\extensions\external_repository_manager\implementation\box; 3 4use common\libraries\Translation; 5use common\libraries\Utilities; 6use common\libraries\StringUtilities; 7use common\libraries\FormValidator; 8use common\libraries\Request; 9 10use common\extensions\external_repository_manager\ExternalRepositoryObjectDisplay; 11/** 12 * $Id: boxexternal_repository_manager_form.class.php 224 2009-11-13 14:40:30Z kariboe $ 13 * @package 14 */ 15 16class BoxExternalRepositoryManagerForm extends FormValidator 17{ 18 19 const TYPE_CREATE = 1; 20 const TYPE_EDIT = 2; 21 const TYPE_NEW_FOLDER = 3; 22 23 const PREVIEW = 'preview'; 24 const FILE = 'file'; 25 26 private $application; 27 private $form_type; 28 private $external_repository_object; 29 30 function __construct($form_type, $action, $application) 31 { 32 parent :: __construct(Utilities :: get_classname_from_object($this, true), 'post', $action); 33 34 $this->application = $application; 35 36 $this->form_type = $form_type; 37 38 if ($this->form_type == self :: TYPE_EDIT) 39 { 40 $this->build_editing_form(); 41 } 42 elseif ($this->form_type == self :: TYPE_CREATE) 43 { 44 $this->build_creation_form(); 45 } 46 elseif ($this->form_type == self :: TYPE_NEW_FOLDER) 47 { 48 $this->build_newfolder_form(); 49 } 50 51 $this->setDefaults(); 52 } 53 54 public function set_external_repository_object(BoxExternalRepositoryObject $external_repository_object) 55 { 56 $this->external_repository_object = $external_repository_object; 57 58 $defaults[BoxExternalRepositoryObject :: PROPERTY_ID] = $external_repository_object->get_id(); 59 60 $display = ExternalRepositoryObjectDisplay :: factory($external_repository_object); 61 $defaults[self :: PREVIEW] = $display->get_preview(); 62 63 parent :: setDefaults($defaults); 64 } 65 66 function build_basic_form() 67 { 68 if ($this->form_type == self :: TYPE_EDIT) 69 { 70 $this->add_information_message('box_api_move', null, Translation :: get('BoxAPIMoveImpossible')); 71 } 72 } 73 74 function build_editing_form() 75 { 76 $this->addElement('static', self :: PREVIEW); 77 78 $this->build_basic_form(); 79 80 $this->addElement('hidden', BoxExternalRepositoryObject :: PROPERTY_ID); 81 82 $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Edit', null, Utilities :: COMMON_LIBRARIES), array( 83 'class' => 'positive update')); 84 $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES), array( 85 'class' => 'normal empty')); 86 87 $this->addGroup($buttons, 'buttons', null, ' ', false); 88 } 89 90 function update_file() 91 { 92 return $this->application->get_external_repository_manager_connector()->update_external_repository_object($this->exportValues()); 93 } 94 95 function upload_file() 96 { 97 if (StringUtilities :: has_value(($_FILES[self :: FILE]['name']))) 98 { 99 if ($this->application->get_external_repository_manager_connector()->create_external_repository_object($_FILES[self :: FILE])) 100 return true; 101 } 102 else 103 { 104 return false; 105 } 106 } 107 108 function build_creation_form() 109 { 110 $this->build_basic_form(); 111 112 $this->addElement('file', self :: FILE, Translation :: get('FileName')); 113 114 $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Create', null, Utilities :: COMMON_LIBRARIES), array( 115 'class' => 'positive')); 116 $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES), array( 117 'class' => 'normal empty')); 118 119 $this->addGroup($buttons, 'buttons', null, ' ', false); 120 } 121 122 function build_newfolder_form() 123 { 124 $this->addElement('text', 'foldername', 'Name of new folder', array('size' => '50')); 125 $this->addRule('foldername', Translation :: get('ThisFieldIsRequired', null, Utilities :: COMMON_LIBRARIES), 'required'); 126 127 $this->addElement('hidden', 'folder'); 128 $this->setDefaults(array('folder' => Request :: get('folder'))); 129 130 $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Create', null, Utilities :: COMMON_LIBRARIES), array( 131 'class' => 'positive')); 132 $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES), array( 133 'class' => 'normal empty')); 134 135 $this->addGroup($buttons, 'buttons', null, ' ', false); 136 } 137 138 function create_folder($folder) 139 { 140 if (empty($_POST['folder'])) 141 { 142 $folder = 0; 143 } 144 else 145 $folder = $_POST['folder']; 146 147 if (! is_null($_POST['foldername'])) 148 { 149 return $this->application->get_external_repository_manager_connector()->create_external_repository_folder($_POST['foldername'], $folder); 150 } 151 else 152 return null; 153 } 154} 155?>