Jump to content

Deleting file field's files via API not working or timing out


hellomoto
 Share

Recommended Posts

<?php

class ProcessTracksterMultiUpload extends WireData implements Module {

	/**
	 * getModuleInfo is a module required by all modules to tell ProcessWire about them
	 *
	 * @return array
	 *
	 */
	public static function getModuleInfo() {

		return array(
			'title' => 'Process Trackster Multi-Upload', 
			'version' => 1, 
			'summary' => 'Creates a new page for each individual track',
			'href' => '',
			'singular' => true, 
			'autoload' => true, 
			'icon' => '', 
			);
	}

	/**
	 * Initialize the module
	 *
	 * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
	 * when ProcessWire's API is ready. As a result, this is a good place to attach hooks. 
	 *
	 */
	public function init() {
		$this->pages->addHookAfter('save', $this, 'addNewTracks');
	}

	/**
	 * Adds tracks
	 *
	 */
	protected function addNewTracks($event) {
		$page = $event->arguments[0]; 
		$pages = wire('pages');
		if($page->template != 'tracks') return;
		
		foreach($page->tracks_upload as $t) {
			$basename = str_replace(".{$t->ext}",'',$t->name);
			$p = new Page;
			$p->parent = $pages->get('/tracks/');
			$p->template = 'track';
			$p->title = $basename;
			$p->name = $p->id;
			$p->addStatus(Page::statusUnpublished)->save();
			$p->audio->add($t->filename);
			$p->save();
			$this->message("Added new track {$p->name} by {$p->createdUser}");
			//wire('pages')->get(1014)->tracks_upload->delete($t->filename);
			//$deleted = $t->name;
			//$files->delete($t);
			$this->message("Deleted temporary file $$t->filename");
			//$this->message("new $newname");
		}
		$pages->get($page->id)->tracks_upload->deleteAll();
		$pages->get($page->id)->save();
		//$page->tracks_upload->removeAll()->save();
		//$thisPage = $pages->get($page->id);
		//$thisPage->save();
		//$page->tracks_upload->deleteAll();
		//$page->save();
	}
	
}

I've tried $page->tracks_upload->deleteAll(), removeAll(), $page->delete($t), $page->delete($t->name) ($page ones in the foreach loop), all kinds of things. Nothing seems to work. Pages are created fine and all, I just need these files deleted too.

Link to comment
Share on other sites

This worked

	protected function addNewTracks($event) {
		$page = $event->arguments[0]; 
		$pages = wire('pages');
		if($page->template == 'tracks' && count($page->tracks_upload) > 0) {
			foreach($page->tracks_upload as $t) {
				$basename = str_replace(".{$t->ext}",'',$t->name);
				$p = new Page;
				$p->parent = $pages->get('/tracks/');
				$p->template = 'track';
				$p->title = $basename;
				$p->addStatus(Page::statusUnpublished)->save();
				$p->audio->add($t->filename);
				$p->save();
				$p->name = $p->id; $p->save();
				$this->message("Added new track {$p->title} by {$p->createdUser->name}");
			}
			$page->tracks_upload->removeAll();
			$page->save();
		}
	}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...