/**
 * Backups $app to a subfolder in $path
 * 
 * @param type $app app to backup
 * @param type $path in this folder a subfolder for the app will be created
 */
function backup_app($app, $path, $downloadFiles)
{
    $path_app = $path . '/' . fixDirName($app->config['name']);
    global $verbose;
    if ($verbose) {
        echo "App: " . $app->config['name'] . "\n";
        echo "debug: MEMORY: " . memory_get_usage(true) . " | " . memory_get_usage(false) . "\n";
    }
    mkdir($path_app);
    $appFile = "";
    $appFiles = array();
    $files_in_app_html = "<html><head><title>Files in app: " . $app->config['name'] . "</title></head><body>" . "<table border=1><tr><th>name</th><th>link</th><th>context</th></tr>";
    try {
        #$appFiles = PodioFile::get_for_app($app->app_id, array('attached_to' => 'item'));
        $appFiles = PodioFetchAll::iterateApiCall('PodioFile::get_for_app', $app->app_id, array(), FILE_GET_FOR_APP_LIMIT);
        #var_dump($appFiles);
        PodioFetchAll::flattenObjectsArray($appFiles, PodioFetchAll::podioElements(array('file_id' => null, 'name' => null, 'link' => null, 'hosted_by' => null, 'context' => array('id' => NULL, 'type' => null, 'title' => null))));
        if ($verbose) {
            echo "fetched information for " . sizeof($appFiles) . " files in app.\n";
        }
    } catch (PodioError $e) {
        show_error($e);
    }
    try {
        $allitems = PodioFetchAll::iterateApiCall('PodioItem::filter', $app->app_id, array(), ITEM_FILTER_LIMIT, 'items');
        echo "app contains " . sizeof($allitems) . " items.\n";
        for ($i = 0; $i < sizeof($allitems); $i += ITEM_XLSX_LIMIT) {
            $itemFile = PodioItem::xlsx($app->app_id, array("limit" => ITEM_XLSX_LIMIT, "offset" => $i));
            RateLimitChecker::preventTimeOut();
            file_put_contents($path_app . '/' . $app->config['name'] . '_' . $i . '.xlsx', $itemFile);
            unset($itemFile);
        }
        $before = time();
        gc_collect_cycles();
        echo "gc took : " . (time() - $before) . " seconds.\n";
        foreach ($allitems as $item) {
            if ($verbose) {
                echo " - " . $item->title . "\n";
            }
            $folder_item = fixDirName($item->item_id . '_' . $item->title);
            $path_item = $path_app . '/' . $folder_item;
            mkdir($path_item);
            unset($itemFile);
            $itemFile = HumanFormat::toHumanReadableString($item);
            if ($downloadFiles) {
                foreach ($appFiles as $file) {
                    if ($file->context['type'] == 'item' && $file->context['id'] == $item->item_id) {
                        $link = downloadFileIfHostedAtPodio($path_item, $file);
                        # $link is relative to $path_item (if downloaded):
                        if (!preg_match("/^http/i", $link)) {
                            $link = RelativePaths::getRelativePath($path_app, $path_item . '/' . $link);
                        }
                        $itemFile .= "File: {$link}\n";
                        $files_in_app_html .= "<tr><td>" . $file->name . "</td><td><a href=\"" . $link . "\">" . $link . "</a></td><td>" . $file->context['title'] . "</td></tr>";
                    }
                }
            }
            //TODO refactor to use less api calls: (not possible??!)
            if ($item->comment_count > 0) {
                #echo "comments.. (".$item->comment_count.")\n";
                $comments = PodioComment::get_for('item', $item->item_id);
                RateLimitChecker::preventTimeOut();
                $commentsFile = "\n\nComments\n--------\n\n";
                foreach ($comments as $comment) {
                    $commentsFile .= 'by ' . $comment->created_by->name . ' on ' . $comment->created_on->format('Y-m-d at H:i:s') . "\n----------------------------------------\n" . $comment->value . "\n\n\n";
                    if ($downloadFiles && isset($comment->files) && sizeof($comment->files) > 0) {
                        foreach ($comment->files as $file) {
                            $link = downloadFileIfHostedAtPodio($path_item, $file);
                            # $link is relative to $path_item (if downloaded):
                            if (!preg_match("/^http/i", $link)) {
                                $link = RelativePaths::getRelativePath($path_app, $path_item . '/' . $link);
                            }
                            $commentsFile .= "File: {$link}\n";
                            $files_in_app_html .= "<tr><td>" . $file->name . "</td><td><a href=\"" . $link . "\">" . $link . "</a></td><td>" . $file->context['title'] . "</td></tr>";
                        }
                    }
                }
            } else {
                $commentsFile = "\n\n[no comments]\n";
                #echo "no comments.. (".$item->comment_count.")\n";
            }
            file_put_contents($path_item . '/' . fixDirName($item->item_id . '-' . $item->title) . '.txt', $itemFile . $commentsFile);
            $appFile .= $itemFile . "\n\n";
        }
        //store non item/comment files:
        if ($verbose) {
            echo "storing non item/comment files..\n";
        }
        $app_files_folder = 'other_files';
        $path_app_files = $path_app . '/' . $app_files_folder;
        mkdir($path_app_files);
        $files_in_app_html .= "<tr><td><b>App Files</b></td><td><a href={$app_files_folder}>" . $app_files_folder . "</a></td><td></td></tr>";
        foreach ($appFiles as $file) {
            if ($file->context['type'] != 'item' && $file->context['type'] != 'comment') {
                echo "debug: downloading non item/comment file: {$file->name}\n";
                $link = downloadFileIfHostedAtPodio($path_app_files, $file);
                # $link is relative to $path_item (if downloaded):
                if (!preg_match("/^http/i", $link)) {
                    $link = RelativePaths::getRelativePath($path_app, $path_item . '/' . $link);
                }
                $files_in_app_html .= "<tr><td>" . $file->name . "</td><td><a href=\"" . $link . "\">" . $link . "</a></td><td>" . $file->context['title'] . "</td></tr>";
            }
        }
    } catch (PodioError $e) {
        show_error($e);
        $appFile .= "\n\nPodio Error:\n" . $e;
    }
    file_put_contents($path_app . '/all_items_summary.txt', $appFile);
    $files_in_app_html .= "</table></body></html>";
    file_put_contents($path_app . "/files_in_app.html", $files_in_app_html);
    unset($appFile);
    unset($files_in_app_html);
}
예제 #2
0
function getItemsSideBarBACKUP()
{
    //get all filters here for clients names
    $category_status_id = 62473690;
    $client_id = 62474165;
    //filtering by client name here
    $planning_collection = PodioItem::filter(APP_ID, array('filters' => array($category_status_id => array(1, 2, 3, 6, 7, 8, 9, 10, 11, 14)), 'limit' => 200, 'sort_by' => 'created_on'));
    foreach ($planning_collection as $item) {
        echo '<a class="item">';
        echo '<div class="lightgraybg">';
        $job = $item->fields["job"];
        //Job number
        echo '<h3 class="no-margin no-padding"><span id="jobnumber" class="text-align-left small">Job #:' . (double) $job->values . '</span>';
        //Title
        echo ' ' . ucfirst(strtolower($item->title)) . '<span id="status" class="text-green">';
        //Status
        foreach ($item->fields['category-status']->values as $option) {
            print ucfirst(strtolower($option['text']));
            //status
        }
        echo '</span></h3>';
        echo '<div class="ui divider mine"></div>';
        //due date here retrieved using external_id
        $due_date = $item->fields["due-date"];
        $date = $due_date->start;
        echo '<p class="no-padding no-margin">Due Date<span class="bold">: ' . date_format($date, 'm-d-Y') . '</span></p>';
        echo '<div class="description">';
        echo '<p><span class="bold">Last Comment: </span>';
        //get last 2 comments!! comments here
        $comments = PodioComment::get_for('item', $item->id, array('limit' => 50));
        $comment_count = 0;
        foreach ($comments as $comment) {
            $comment_count += 1;
        }
        $last_one = $comment_count - 1;
        //filter integer
        $comment_count = 0;
        foreach ($comments as $comment) {
            if ($comment_count >= $last_one) {
                echo substr($comment->value, 0, 100) . '...';
            }
            $comment_count += 1;
        }
        echo '</p>';
        //end last comment
        // ALL COMMENTS IN ACCORDION
        echo '<div class="ui accordion">
                    <div class="title labeled icon button"><i class="dropdown icon"></i>All Comments</div>
                        <div class="content">';
        foreach ($comments as $comment) {
            echo '<p>' . $comment->value . '</p>';
        }
        echo '<form class="ui form" action="http://kb-demos.com/podio/utilities/comment.php" method="post">';
        echo '<div class="ui action input">';
        echo '<input type="hidden" name="comment_id" placeholder="Comment..." value="' . $item->id . '">';
        echo '<input type="text" name="comment" placeholder="Comment...">';
        echo '<input type="submit" class="button ui blue" value="Submit">';
        echo '</div>';
        echo '</form>';
        echo '</div>';
        //closes content div
        echo '</div>';
        //closes accordion div
        echo '</div>';
        //closes description div
        echo '</div>';
        echo '</a>';
    }
    //    <a class="item">
    //            <div class="lightgraybg">
    //
    //                <h3><span id="jobnumber" class="text-align-left small">Job #: </span> &nbsp; Title <span id="status" class="text-green">Status</span></h3>
    //                <p class="no-padding">Due Date: <span class="text-bold">04/05/15</span></p>
    //                <div class="description">
    //                    <p><span class="bold">Notes: </span>This is some text.This is some text.This is some text.This is some text.</p>
    //                </div>
    //            </div>
    //        </a>
}