1<?php
2/**
3 * This script is used to create dynamic albums from a search.
4 * @package core
5 */
6// force UTF-8 Ø
7
8define('OFFSET_PATH', 1);
9require_once(dirname(__FILE__) . '/admin-globals.php');
10require_once(dirname(__FILE__) . '/template-functions.php');
11
12admin_securityChecks(ALBUM_RIGHTS, $return = currentRelativeURL());
13
14$_imagelist = array();
15
16function getSubalbumImages($folder) {
17	global $_imagelist, $_zp_gallery;
18	$album = newAlbum($folder);
19	if ($album->isDynamic())
20		return;
21	$images = $album->getImages();
22	foreach ($images as $image) {
23		$_imagelist[] = '/' . $folder . '/' . $image;
24	}
25	$albums = $album->getAlbums();
26	foreach ($albums as $folder) {
27		getSubalbumImages($folder);
28	}
29}
30
31$search = new SearchEngine(true);
32if (isset($_POST['savealbum'])) {
33	XSRFdefender('savealbum');
34	$albumname = sanitize($_POST['album']);
35	if ($album = sanitize($_POST['albumselect'])) {
36		$albumobj = newAlbum($album);
37		$allow = $albumobj->isMyItem(ALBUM_RIGHTS);
38	} else {
39		$allow = zp_loggedin(MANAGE_ALL_ALBUM_RIGHTS);
40	}
41	if (!$allow) {
42		if (!zp_apply_filter('admin_managed_albums_access', false, $return)) {
43			zp_error(gettext("You do not have edit rights on this album."));
44		}
45	}
46	if ($_POST['create_tagged'] == 'static') {
47		$return_unpublished = isset($_POST['return_unpublished']);
48		$words = sanitize($_POST['album_tag']);
49		$searchfields[] = 'tags_exact';
50		// now tag each element
51		if (isset($_POST['return_albums'])) {
52			$subalbums = $search->getAlbums(0);
53			foreach ($subalbums as $analbum) {
54				$albumobj = newAlbum($analbum);
55				if ($return_unpublished || $albumobj->isPublished()) {
56					$tags = array_unique(array_merge($albumobj->getTags(), array($words)));
57					$albumobj->setTags($tags);
58					$albumobj->setLastChangeUser($_zp_current_admin_obj->getUser());
59					$albumobj->save();
60				}
61			}
62		}
63		if (isset($_POST['return_images'])) {
64			$images = $search->getImages();
65			foreach ($images as $animage) {
66				$image = newImage(newAlbum($animage['folder']), $animage['filename']);
67				if ($return_unpublished || $image->isPublished()) {
68					$tags = array_unique(array_merge($image->getTags(), array($words)));
69					$image->setTags($tags);
70					$image->setLastChangeUser($_zp_current_admin_obj->getUser());
71					$image->save();
72				}
73			}
74		}
75	} else {
76		$searchfields = array();
77		foreach ($_POST as $key => $value) {
78			if (strpos($key, 'SEARCH_') !== false) {
79				$searchfields[] = sanitize(str_replace('SEARCH_', '', postIndexDecode($key)));
80			}
81		}
82		$words = sanitize($_POST['words']);
83
84	}
85	if (isset($_POST['thumb'])) {
86		$thumb = sanitize($_POST['thumb']);
87	} else {
88		$thumb = '';
89	}
90	$constraints = "\nCONSTRAINTS=" . 'inalbums=' . ((int) (isset($_POST['return_albums']))) . '&inimages=' . ((int) (isset($_POST['return_images']))) . '&unpublished=' . ((int) (isset($_POST['return_unpublished'])));
91	$redirect = $album . '/' . $albumname . '.alb';
92
93	if (!empty($albumname)) {
94		$f = fopen(internalToFilesystem(ALBUM_FOLDER_SERVERPATH . $redirect), 'w');
95		if ($f !== false) {
96			fwrite($f, "WORDS=$words\nTHUMB=$thumb\nFIELDS=" . implode(',', $searchfields) . $constraints . "\n");
97			fclose($f);
98			clearstatcache();
99			// redirct to edit of this album
100			redirectURL(FULLWEBPATH . "/" . ZENFOLDER . "/admin-edit.php?page=edit&album=" . pathurlencode($redirect));
101		}
102	}
103}
104$_GET['page'] = 'edit'; // pretend to be the edit page.
105printAdminHeader('edit', gettext('dynamic'));
106echo "\n</head>";
107echo "\n<body>";
108printLogoAndLinks();
109echo "\n" . '<div id="main">';
110printTabs();
111echo "\n" . '<div id="content">';
112zp_apply_filter('admin_note', 'albums', 'dynamic');
113echo "<h1>" . gettext("Create Dynamic Album") . "</h1>\n";
114
115if (isset($_POST['savealbum'])) { // we fell through, some kind of error
116	echo "<div class=\"errorbox space\">";
117	echo "<h2>" . gettext("Failed to save the album file") . "</h2>";
118	echo "</div>\n";
119}
120
121$albumlist = $_zp_gallery->getAllAlbumsFromDB();
122$fields = $search->fieldList;
123$albumname = $search->getSearchWords();
124$words = $search->codifySearchString();
125$images = $search->getImages(0);
126foreach ($images as $image) {
127	$folder = $image['folder'];
128	$filename = $image['filename'];
129	$_imagelist[] = '/' . $folder . '/' . $filename;
130}
131$subalbums = $search->getAlbums(0);
132foreach ($subalbums as $folder) {
133	getSubalbumImages($folder);
134}
135$albumname = sanitize_path($albumname);
136$albumname = seoFriendly($albumname);
137$old = '';
138while ($old != $albumname) {
139	$old = $albumname;
140	$albumname = str_replace('--', '-', $albumname);
141}
142?>
143<form class="dirty-check" action="?savealbum" method="post" autocomplete="off">
144	<?php XSRFToken('savealbum'); ?>
145	<input type="hidden" name="savealbum" value="yes" />
146	<table>
147		<tr>
148			<td><?php echo gettext("Album name:"); ?></td>
149			<td>
150				<input type="text" size="40" name="album" value="<?php echo html_encode($albumname) ?>" />
151			</td>
152		</tr>
153		<tr>
154			<td><?php echo gettext("Create in:"); ?></td>
155			<td>
156				<select id="albumselectmenu" name="albumselect">
157					<?php
158					if (accessAllAlbums(UPLOAD_RIGHTS)) {
159						?>
160						<option value="" style="font-weight: bold;">/</option>
161						<?php
162					}
163     $parentalbum = '';
164    	if(isset($_GET['folder'])) {
165     		$parentalbum = sanitize($_GET['folder']);
166     }
167					foreach ($albumlist as $fullfolder => $albumtitle) {
168						$singlefolder = $fullfolder;
169						$saprefix = "";
170						$salevel = 0;
171						// Get rid of the slashes in the subalbum, while also making a subalbum prefix for the menu.
172						while (strstr($singlefolder, '/') !== false) {
173							$singlefolder = substr(strstr($singlefolder, '/'), 1);
174							$saprefix = "–&nbsp;" . $saprefix;
175							$salevel++;
176						}
177      $selected = '';
178      if($parentalbum == $fullfolder) {
179        $selected = ' selected="selected"';
180      }
181						echo '<option value="' . $fullfolder . '"'.$selected.'>' . $saprefix . $singlefolder . ' (' . $albumtitle .')' . '</option>\n';
182					}
183					?>
184				</select>
185			</td>
186		</tr>
187		<tr>
188			<td><?php echo gettext("Thumbnail:"); ?></td>
189			<td>
190				<select id="thumb" name="thumb">
191					<?php
192					$selections = array();
193					foreach ($_zp_albumthumb_selector as $key => $selection) {
194						$selections[$selection['desc']] = $key;
195					}
196					generateListFromArray(array(getOption('AlbumThumbSelect')), $selections, false, true);
197					$showThumb = $_zp_gallery->getThumbSelectImages();
198					foreach ($_imagelist as $imagepath) {
199						$pieces = explode('/', $imagepath);
200						$filename = array_pop($pieces);
201						$folder = implode('/', $pieces);
202						$albumx = newAlbum($folder);
203						$image = newImage($albumx, $filename);
204						if (isImagePhoto($image) || !is_null($image->objectsThumb)) {
205							echo "\n<option class=\"thumboption\"";
206							if ($showThumb) {
207								echo " style=\"background-image: url(" . html_encode($image->getSizedImage(80)) .
208								"); background-repeat: no-repeat;\"";
209							}
210							echo " value=\"" . $imagepath . "\"";
211							echo ">" . $image->getTitle();
212							echo " ($imagepath)";
213							echo "</option>";
214						}
215					}
216					?>
217				</select>
218			</td>
219		</tr>
220		<tr>
221			<td><?php echo gettext("Search criteria:"); ?></td>
222			<td>
223				<input type="text" size="60" name="words" value="<?php echo html_encode($words); ?>" />
224				<label><input type="checkbox" name="return_albums" value="1"<?php if (!getOption('search_no_albums')) echo ' checked="checked"' ?> /><?php echo gettext('Return albums found') ?></label>
225				<label><input type="checkbox" name="return_images" value="1"<?php if (!getOption('search_no_images')) echo ' checked="checked"' ?> /><?php echo gettext('Return images found') ?></label>
226				<label><input type="checkbox" name="return_unpublished" value="1" /><?php echo gettext('Return unpublished items') ?></label>
227			</td>
228		</tr>
229
230		<script type="text/javascript">
231			// <!-- <![CDATA[
232			function setTagged(state) {
233				if (state) {
234					$('#album_tag').removeAttr('disabled');
235					$('.searchchecklist').attr('disabled', 'disabled');
236				} else {
237					$('.searchchecklist').removeAttr('disabled');
238					$('#album_tag').attr('disabled', 'disabled');
239				}
240			}
241			// ]]> -->
242		</script>
243
244		<tr>
245			<td>
246				<label>
247					<input type="radio" name="create_tagged" value="dynamic" onchange="setTagged(false)" checked="checked" /><?php echo gettext('dynamic'); ?>
248				</label>
249				<label>
250					<input type="radio" name="create_tagged" value="static" onchange="setTagged(true)"/><?php echo gettext('tagged'); ?>
251				</label>
252			</td>
253			<td>
254			</td>
255		</tr>
256		<tr>
257			<td><?php echo gettext('Album <em>Tag</em>'); ?></td>
258			<td>
259				<input type="text" size="40" name="album_tag" id="album_tag" value="<?php echo html_encode($albumname); ?>" disabled="disabled" />
260				<?php echo gettext('Select <em>tagged</em> to tag the current search results with this <em>tag</em> and use as the album criteria.'); ?>
261			</td>
262		</tr>
263		<tr>
264			<td><?php echo gettext("Search fields:"); ?></td>
265			<td>
266				<?php
267				echo '<ul class="searchchecklist">' . "\n";
268				$selected_fields = array();
269				$engine = new SearchEngine(true);
270				$available_fields = $engine->allowedSearchFields();
271				if (count($fields) == 0) {
272					$selected_fields = $available_fields;
273				} else {
274					foreach ($available_fields as $display => $key) {
275						if (in_array($key, $fields)) {
276							$selected_fields[$display] = $key;
277						}
278					}
279				}
280				generateUnorderedListFromArray($selected_fields, $available_fields, 'SEARCH_', false, true, true);
281				echo '</ul>';
282				?>
283			</td>
284		</tr>
285
286	</table>
287
288	<input type="submit" value="<?php echo addslashes(gettext('Create the album')); ?>" class="button" />
289</form>
290
291<?php
292echo "\n" . '</div>';
293echo "\n" . '</div>';
294
295printAdminFooter();
296
297echo "\n</body>";
298echo "\n</html>";
299?>
300
301