Example #1
0
function gallery_field_save_sort()
{
    $entity_id = $_POST['entity_id'];
    $field = $_POST['field'];
    $sort_images_list = $_POST['images'];
    $sort_ids = explode(",", $sort_images_list);
    $entity = get_entity($entity_id);
    if (false == $entity->canEdit()) {
        throw new Exception("can`t edit");
    }
    $current_image_ids = gallery_field_image_ids_from_value($entity->{$field});
    if (count($current_image_ids) == 0) {
        return;
    }
    /**
     * check if all values persist in both arrays
     */
    if (count(array_intersect($sort_ids, $current_image_ids)) != count($current_image_ids)) {
        throw new Exception("bad count");
    }
    $entity->{$field} = implode(",", $sort_ids);
    $entity->save();
}
Example #2
0
<?php

/**
 * Upload images action
 */
elgg_load_library('elgg:gallery_field');
$files_array = $_FILES['image'];
$entity_id = $_POST['entity_id'];
$entity_field = $_POST['entity_field'];
$entity = get_entity($entity_id);
if (false == $entity->canEdit()) {
    register_error(elgg_echo('gallery_field:cant_edit'));
    forward(REFERRER);
}
$image_ids = gallery_field_image_ids_from_value($entity->{$entity_field});
$count_added = 0;
for ($i = 0; $i < count($files_array['tmp_name']); $i++) {
    if (false == in_array($files_array['type'][$i], array("image/jpeg", "image/jpg"))) {
        if (strlen($files_array['type'][$i]) == 0) {
            register_error(elgg_echo("gallery_field:max_upload_exceed"));
        } else {
            register_error(elgg_echo("gallery_field:only_jpg"));
        }
        continue;
    }
    $file = \GalleryFieldImage::createFromFile($files_array['tmp_name'][$i], $files_array['type'][$i]);
    if ($file == null) {
        continue;
    }
    $file->entity_id = $entity_id;
    $file->save();
function gallery_field_get_entity_icon_file_id(ElggEntity $entity, $field = 'images')
{
    $image_ids = gallery_field_image_ids_from_value($entity->{$field});
    if (count($image_ids) == 0) {
        return null;
    }
    return $image_ids[0];
}