-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputfieldDropbox.module
143 lines (112 loc) · 4.84 KB
/
InputfieldDropbox.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php namespace ProcessWire;
use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxApp;
use Kunnu\Dropbox\DropboxFile;
class InputfieldDropbox extends InputfieldTextarea implements ConfigurableModule{
static public function getModuleInfo(){
return array(
'title' => 'Dropbox inputfield',
'author' => 'Ben Byford',
'version' => '0.0.1',
'summary' => 'Dropbox inputfield for textarea.',
'url' => 'http://www.benbyford.com',
'singular' => true,
'autoload' => true,
'requires' => 'DropboxAPI'
);
}
public function renderReady(Inputfield $parent = null, $renderValueMode = false) {
$class = $this->className();
$info = self::getModuleInfo();
$ver = $info['version'];
$this->config->scripts->add($this->config->urls->$class . "js/dropbox-input.jquery.min.js?v=$ver");
$this->config->styles->add($this->config->urls->$class . "css/dropbox-input.css?v=$ver");
return parent::renderReady($parent, $renderValueMode);
}
/*
* Process the value before it is saved to the database
*/
public function ___render() {
$attrs = $this->getAttributes();
$value = $attrs['value'];
/*
* dropbox start
*/
// render dropbox folder select
$drop = $this->modules->get('DropboxAPI');
//Configure Dropbox Application
$app = new DropboxApp($drop->app_key, $drop->app_secret, $drop->authorization_code);
//Configure Dropbox service
$dropbox = new Dropbox($app);
$folder_path = "/";
// get items from dropbox folder
try {
$listFolderContents = $dropbox->listFolder($folder_path); //root folder is the folder you nominated in the Dropbox App config. Refer doco
} catch (\Exception $e) {
$out = 'Folder does not exist or has moved, please contact Fabrix Capital to supply the folder location.';
}
if($listFolderContents){
// if folder found get them
$items = $listFolderContents->getItems();
$out = "<select id='dropbox_select' name='dropbox_folder_textarea'>";
// add bottom level folder
$out .= "<option value='/'>/</option>";
function listItems($items, $dropbox, $depth = "0", $value = ""){
++$depth;
foreach ($items as $item){
// get file name
$name = $item->getName();
// getPathLower
$path = $item->getpathLower();
$displayName = $item->getPathDisplay();
$htmlSafePath = htmlentities($displayName, ENT_QUOTES);
$thumbHasExt = strpos($path, '.');
// folder
if(!$thumbHasExt){
$optionName = strtolower(trim($htmlSafePath));
$selected = "";
if(strcmp($displayName, $value) == 0) $selected = "selected";
$out .= "<option {$selected} value='{$htmlSafePath}'>";
for ($i=0; $i < $depth; $i++) {
$out .= '- ';
}
$out .= "$name";
$out .= "</option>";
// iterate over this folder
$listSubFolderContents = $dropbox->listFolder($path);
$items = $listSubFolderContents->getItems();
// get list of files in subfolder
$out .= listItems($items, $dropbox, $depth, $value);
}
}
return $out;
}
// get depth 0 folder items
$out .= listItems($items, $dropbox, $depth, $value);
$out .= "</select>";
}
$return = "\n<input " . $this->getAttributesString() . " disabled />";
$return .= $out;
return $return;
}
/*
* Process the value before it is saved to the database
*/
public function ___processInput(WireInputData $input) {
parent::___processInput($input);
// use $this->value to access the field value and editing it
return $this;
}
/*
* remove config fields that are not applicable to the dropbox inputfield for textarea
*/
public function ___getConfigInputfields() {
$inputfields = parent::___getConfigInputfields();
$inputfields->remove($inputfields->getChildByName('pattern'));
$inputfields->remove($inputfields->getChildByName('placeholder'));
$inputfields->remove($inputfields->getChildByName('rows'));
$inputfields->remove($inputfields->getChildByName('counter'));
$inputfields->remove($inputfields->getChildByName('stripTags'));
return $inputfields;
}
}